最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Plotting date on x-axis from integers converted to dates - Stack Overflow

programmeradmin1浏览0评论

In my python code, reproduced below, where I sought assistance here Results of Assistance with a simple python line plot, I am plotting days as integers against cumulative rainfall values. With assistance from here Convert integer to dates in python, I have been able to convert the day numbers to dates from 1st October. In my data generation, the number of days from 1st October to 31st March is taken as 183 (including leap year). In converting from integer to date, print(days) gives only 182 dates (29Feb excluded). In the plot function, I have used

plt.plot(days, df['Mean'], color='b', label='Mean') #days being integers converted to dates.

I get the error

ValueError: x and y must have same first dimension, but have shapes (1,) and (183,)

However, plotting with the function

plt.plot(df['Date'], df['Mean'], color='b', label='Mean') #Works fine

works fine.

The code

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import datetime

df = pd.read_csv('../RnOut/RAINFALL/Cumul/CHIPAT.txt')
fig, ax = plt.subplots(figsize=(10, 10))
x = np.array([min(df['Date']), max(df['Date'])])
y = np.array([min(df['Mean']), max(df['Mean'])])

#Convert day numbers to date
#***************************
days = {}
for i in range(0, max(df['Date'])):
    days[i] = (datetime.datetime(2000, 10, 1) + datetime.timedelta(days=i)).strftime("%d %b")

ax.set_title('Cumulative Rainfall: Oct-Mar\n *****************************')
plt.xticks(np.arange(0, max(df['Date']),step=10))
plt.xlabel("Days From 01 Oct")
plt.ylabel("Cumulative Rainfall (mm)")

plt.plot(days, df['Mean'], color='b', label='Mean')
plt.show()

Please assist, how do I use the generated dates?

In my python code, reproduced below, where I sought assistance here Results of Assistance with a simple python line plot, I am plotting days as integers against cumulative rainfall values. With assistance from here Convert integer to dates in python, I have been able to convert the day numbers to dates from 1st October. In my data generation, the number of days from 1st October to 31st March is taken as 183 (including leap year). In converting from integer to date, print(days) gives only 182 dates (29Feb excluded). In the plot function, I have used

plt.plot(days, df['Mean'], color='b', label='Mean') #days being integers converted to dates.

I get the error

ValueError: x and y must have same first dimension, but have shapes (1,) and (183,)

However, plotting with the function

plt.plot(df['Date'], df['Mean'], color='b', label='Mean') #Works fine

works fine.

The code

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import datetime

df = pd.read_csv('../RnOut/RAINFALL/Cumul/CHIPAT.txt')
fig, ax = plt.subplots(figsize=(10, 10))
x = np.array([min(df['Date']), max(df['Date'])])
y = np.array([min(df['Mean']), max(df['Mean'])])

#Convert day numbers to date
#***************************
days = {}
for i in range(0, max(df['Date'])):
    days[i] = (datetime.datetime(2000, 10, 1) + datetime.timedelta(days=i)).strftime("%d %b")

ax.set_title('Cumulative Rainfall: Oct-Mar\n *****************************')
plt.xticks(np.arange(0, max(df['Date']),step=10))
plt.xlabel("Days From 01 Oct")
plt.ylabel("Cumulative Rainfall (mm)")

plt.plot(days, df['Mean'], color='b', label='Mean')
plt.show()

Please assist, how do I use the generated dates?

Share Improve this question edited Mar 31 at 9:15 Zilore Mumba asked Mar 30 at 20:06 Zilore MumbaZilore Mumba 1,5484 gold badges26 silver badges35 bronze badges 12
  • do you have values from year 2000-2001 ? If df['Date'] has different year then it may have different number of days - with 29Feb. - and you may have to use correct year in datetime.datetime(). Or you may have to remove 29Feb from df – furas Commented Mar 30 at 20:29
  • did you check print( days ) ? Why it shows size (1,) for days?. Did you check print( max(df['Date']) )? Maybe it gives value 1 and range(0, max(df['Date'])) creates only one element? I don't understand why you use max() instead of len(df) - for i in range(len(df)): – furas Commented Mar 30 at 20:34
  • print(min(df['Date'])) gives 0, print(max(df['Date'])) gives 182, print(min(days) gives {0: '01 Oct', 1: '02 Oct', ... up to 181: '31 Mar'} but no 29 Feb. The data is not for a specific year but a mean over many years. The idea is to eventually plot the mean with some years superimposed. This works (mean with any number of years. I will recalculate the mean with leap years ignored. – Zilore Mumba Commented Mar 30 at 21:13
  • Incidentally "max(df['Date']))" in "for i in range(0, len(df['Date']))" gives 0-181 values to 31 Mar, "len(df['Date']))" gives 0-182 values to 01 Apr, but the plot error is the same. I think "days" to be tweaked to d ifferent form before use in the plot. That's what am trying to figure out.. – Zilore Mumba Commented Mar 30 at 21:25
  • you should check values in other variables. For some reason you create object with size (1,) and it means only one element - so it doesn't matter if you have 0-181 or 0-182 because it has bigger problem. Maybe inside for-loop you should use print() to see i, days[i] - – furas Commented Mar 30 at 22:09
 |  Show 7 more comments

1 Answer 1

Reset to default 0

The issue was, given a column of numbers, 1 to 183 (1 October to 31 March) and a column of corresponding values, how to plot the values on a line graph and label the x-axis points as dates, instead of numbers as given in column 1.

Sample data

Date,Mean
  0,   0.1
  1,   0.1
  2,   0.3
  3,   0.4
  4,   0.9
  5,   1.2

Dates generated from the first data column

{0: '01Oct', 1: '02Oct', 2: '03Oct', 3: '04Oct', 4: '05Oct'}

The problem, each date generated has 2 components, a number and the date.

The solution:

Put days generated in a pandas Dataframe, to be able to extract the date

dfd = pd.DataFrame(list(days.items()))

print(dfd) #to have a look at the generated dataframe

0 01Oct

1 02Oct

2 03Oct

3 04Oct

4 05Oct

Now x_ticks can be specified, using dates

plt.xticks(ticks = list(range(0, len(dfd[1]), 15)))

After this, values can be plotted with dates on the x-axis and vlues as y.

plt.plot(dfd[1], df['Mean'], color='b', label='Mean')

This is how the problem was solved, to have the figure as posted above. Thanks once more @furas. The interaction helped me go in the right direction.

发布评论

评论列表(0)

  1. 暂无评论