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 | Show 7 more comments1 Answer
Reset to default 0The 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.
df['Date']
has different year then it may have different number of days - with 29Feb. - and you may have to use correct year indatetime.datetime()
. Or you may have to remove29Feb
fromdf
– furas Commented Mar 30 at 20:29print( days )
? Why it shows size(1,)
for days?. Did you checkprint( max(df['Date']) )
? Maybe it gives value1
andrange(0, max(df['Date']))
creates only one element? I don't understand why you usemax()
instead oflen(df)
-for i in range(len(df)):
– furas Commented Mar 30 at 20:34(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 useprint()
to seei
,days[i]
- – furas Commented Mar 30 at 22:09