1

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:

image

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.

Vitalizzare
  • 4,496
  • 7
  • 13
  • 32
  • Can you print out `test.Date`, `test.Precipitation`, `previsao.Date` and `previsao.Precipitation`, and show us the result? – Yulia V Oct 10 '22 at 19:53
  • sure, added on the main description – Matheus Alves Oct 10 '22 at 20:48
  • **[Don't Post Data Screenshots](https://meta.stackoverflow.com/questions/303812/)**. This question needs a [SSCCE](http://sscce.org/). **Always** provide a [mre], with **code, data, errors, current output, and expected output, as [formatted text](https://stackoverflow.com/help/formatting)**. It's likely the question will be down-voted and closed. You're discouraging assistance, as no one wants to retype data/code, and screenshots are often illegible. [edit] the question and **add text**. Plots are okay. See [How to provide a reproducible dataframe](https://stackoverflow.com/questions/52413246) – Trenton McKinney Oct 10 '22 at 20:59
  • Sure, I have changed the format for better view, will add more details as ss=oon as possible. – Matheus Alves Oct 10 '22 at 21:30

1 Answers1

1

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()

enter image description here

Redox
  • 9,321
  • 5
  • 9
  • 26
  • 1
    Hi Redox, thanks! The dtype was already datetime64, I added the bymonthday==-1and it worked as expected, I did not know that the end of the month would have a different formatting, so again, thanks! – Matheus Alves Oct 16 '22 at 23:50