简体   繁体   中英

How do I fix the datetime x-axis on my Python Plot?

I'm trying to plot a graph of time x precipitation, however, the values on the x-axis don't match the values on the y-axis. The plot itself it's correct, but the x-axis is not. The context is that I created a Prophet model to predict monthly precipitation, after generating the values, I plotted the test against the prediction, and while the lines seem correct, the dates look shifted one 'unit' to the left:

Precipitation x Month:

图片

Test Values:

    Date    Precipitation
443 2020-12-31  273.2
444 2021-01-31  215.5
445 2021-02-28  180.6
446 2021-03-31  138.4
447 2021-04-30  54.4
448 2021-05-31  44.4
449 2021-06-30  16.2
450 2021-07-31  39.4
451 2021-08-31  44.4
452 2021-09-30  39.5
453 2021-10-31  91.9
454 2021-11-30  98.6
455 2021-12-31  127.3
456 2022-01-31  308.5

Prediction Values:

    Date    Precipitation
443 2020-12-31  133.7
444 2021-01-31  272.0
445 2021-02-28  222.0
446 2021-03-31  177.3
447 2021-04-30  75.9
448 2021-05-31  81.5
449 2021-06-30  31.9
450 2021-07-31  41.7
451 2021-08-31  28.9
452 2021-09-30  42.9
453 2021-10-31  111.4
454 2021-11-30  129.5
455 2021-12-31  126.2
456 2022-01-31  299.1

We can observe that the first value should be for 2020-12 but that's not the case.

fig = plt.figure(figsize=(12, 8))

plt.plot(test.Date, test.Precipitation, 's-r')
plt.plot(previsao.Date, previsao.Precipitation, 's-b')

plt.title('Precipitação por Mês na Cidade de São Paulo em $mm$', fontsize=20)

plt.ylabel('Precipitação ($mm$)', fontsize=12)
plt.xlabel('Ano')
plt.legend(['Real', 'Previsão']);
plt.show() 

Can anyone point out what I'm doing wrong here? I believe I'm doing something wrong when plotting the graph but I cannot figure it out.

There are a couple of things you will need to do. Firstly make sure that the date columns are in datetime format. You can check this using test.info() and previsao.info() and see that the dtype is datetime. If not, use pd.to_datetime() . The default format for dates displayed is the first day of the month. So, the dates you see will appear like it is shift by a month. But, as you have dates which are the last date of the month, you will need to change display to show the last date using bymonthday=-1 in the MonthLocator . Finally, I have used "YYYY-MM-DD" format for display so that you can see the full date and rotated the tick labels. You can edit it to suit your needs. The updated code is shown below. Hope this is what you are looking for.

fig = plt.figure(figsize=(12, 8))

## Use these if your dates are NOT already in datetime format
test['Date']=pd.to_datetime(test['Date'])
previsao['Date']=pd.to_datetime(previsao['Date'])

plt.plot(test.Date, test.Precipitation, 's-r')
plt.plot(previsao.Date, previsao.Precipitation, 's-b')

plt.title('Precipitação por Mês na Cidade de São Paulo em $mm$', fontsize=20)

plt.ylabel('Precipitação ($mm$)', fontsize=12)
plt.xlabel('Ano')
plt.legend(['Real', 'Previsão']);

## Added code here
import matplotlib.dates as mdates ## Import required library
months = mdates.MonthLocator(interval=1, bymonthday=-1)  ## 1 month apart & show last date
plt.gca().xaxis.set_major_locator(months) ## Set months as major locator
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) ##Display format - update here to change
plt.xticks(rotation=45, ha='right') ##Adjust angle and horizontal align right
plt.show()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM