0

I got these 2 simple csv data but when plotting the 'mon' line gone strange toward the end. When plotting one chart, it is fine but when the 2 charts plotted together the 'monarch' one goes strange.

Thanks in advance.

Here is the code

import pandas as pd 
from matplotlib import pyplot as plt

def run_plot1():
    df_ash = pd.read_csv('./data/ashburn.csv')
    df_mon = pd.read_csv('./data/monarch1bed.csv')

    plt.grid(True)

    plt.plot(df_ash['Date'], df_ash['Ash1bed'], label='Ashburn 1 bed')
    plt.plot(df_mon['Date'], df_mon['Mon1bed'], label='Monarch 1 bed')


    plt.xlabel("Date")
    plt.ylabel("Rate")

    plt.style.use("fivethirtyeight")
    plt.title("One Bed Comparison")
    plt.legend()
    plt.savefig('data/sample.png')
    plt.tight_layout()

    plt.show()

run_plot1()

enter image description here

and the csv datas:

Date,Ash1bed,Ash2bed,Ash3bed
08-01,306,402
22-01,181,286,349
05-02,176,281,336
19-02,188,293,369
04-03,201,306,402
18-03,209
01-04,217,394,492
15-04,209,354,455
29-04,197,302,387
13-05,205,326,414
27-05,217,362,473
10-06,390,532
08-07,415
22-07,415
05-08,415
19-08,415
15-09,290,452,594

and another :

Date,Mon1bed
08-01,230
05-02,160
19-02,160
04-03,190
18-03,190
01-04,260
15-04,260
29-04,260
13-05,300
27-05,330
10-06,330
24-06,350
08-07,350
22-07,350
05-08,350
19-08,350
02-09,350
1
  • If you have Date in the format YYYY-MM-DD and convert it to datetime you should have a nice plot. Commented Jan 2, 2020 at 17:56

2 Answers 2

2

The basic reason of erratic printout is that your Date columns in both DataFrames are of string type.

Convert them to datetime:

df_ash.Date = pd.to_datetime(df_ash.Date, format='%d-%m')
df_mon.Date = pd.to_datetime(df_mon.Date, format='%d-%m')

But to have "reader friendly" X-axis labels, a number of additional steps are required.

Start from necessary imports:

from pandas.plotting import register_matplotlib_converters
import matplotlib.dates as mdates

Then register matplotlib converters:

register_matplotlib_converters()

And to get proper printout, run:

fig, ax = plt.subplots()  # figsize=(10, 6)
ax.grid(True)
ax.plot(df_ash['Date'], df_ash['Ash1bed'], label='Ashburn 1 bed')
ax.plot(df_mon['Date'], df_mon['Mon1bed'], label='Monarch 1 bed')
plt.xlabel("Date")
plt.ylabel("Rate")
plt.style.use("fivethirtyeight")
plt.title("One Bed Comparison")
plt.legend()
dm_fmt = mdates.DateFormatter('%d-%m')
ax.xaxis.set_major_formatter(dm_fmt)
plt.xticks(rotation=45);

For your data I got:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

You should convert the date variable to a date format

df1.Date = pd.to_datetime(df1.Date, format='%d-%m')

df2.Date = pd.to_datetime(df2.Date, format='%d-%m')

plt.plot(df1.Date, df1.Ash1bed)
plt.plot(df2.Date, df2.Mon1bed)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.