Plotting A Graph Using Arrays
Solution 1:
You did not prepare your x-y data in a way that matplotlib
can understand their relationship.
The easy "answer" would be to plot res
and df['HourOfDay'].value_counts()
directly against each other:
#.....
#range = (0,24)
#bins = 2
#plt.hist(df['DateTime'].dt.hour, bins, range)
plt.plot(res, df['HourOfDay'].value_counts())
plt.show()
But the sample output shows you the problem:
matplotlib
does not order the x
-values for you (that would misrepresent the data in a different context). So, we have to do this before plotting:
#.....
#range = (0,24)
#bins = 2
#plt.hist(df['DateTime'].dt.hour, bins, range)
xy=np.stack((res, df['HourOfDay'].value_counts()))
xy = xy[:, np.argsort(xy[0,:])]
plt.plot(*xy)
plt.show()
Now, the x
-values are in the correct order, and the y
-values have been sorted with them in the combined xy
array that we created for this purpose:
Obviously, it would be better to prepare res
and df['HourOfDay'].value_counts()
directly, so we don't have to create a combined array to sort them together. Since you did not provide an explanation what your code is supposed to do, we can only post-fix the problem the code created - you should structure it differently, so that this problem does not occur in the first place. But only you can do this (or people who understand the intention of your code - I don't).
I also suggest spending some time with the instructive matplotlib tutorials - this time is not wasted.
Update
It seems you try to create a subplot for each day and count the number of entries per hour. I would approach it like this (but I am sure, some panda experts have better ways for this):
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
#read your data and create datetime index
df= pd.read_csv('test1.txt', sep=";")
df.index = pd.to_datetime(df["Date"]+df["Time"].str[:-5], format="%Y:%m:%d %H:%M:%S")
#group by date and hour, count entries
dfcounts = df.groupby([df.index.date, df.index.hour]).size().reset_index()
dfcounts.columns = ["Date", "Hour", "Count"]
maxcount = dfcounts.Count.max()
#group by date for plotting
dfplot = dfcounts.groupby(dfcounts.Date)
#plot each day into its own subplot
fig, axs = plt.subplots(dfplot.ngroups, figsize=(6,8))
for i, groupdate in enumerate(dfplot.groups):
ax=axs[i]
#the marker is not really necessary but has been added in case there is just one entry per day
ax.plot(dfplot.get_group(groupdate).Hour, dfplot.get_group(groupdate).Count, color="blue", marker="o")
ax.set_title(str(groupdate))
ax.set_xlim(0, 24)
ax.set_ylim(0, maxcount * 1.1)
ax.xaxis.set_ticks(np.arange(0, 25, 2))
plt.tight_layout()
plt.show()
Update 2
To plot them into individual figures, you can modify the loop:
#...
dfplot = dfcounts.groupby(dfcounts.Date)
for groupdate in dfplot.groups:
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4))
fig.suptitle("Date:"+str(groupdate), fontsize=16)
#scaled for comparability among graphs
ax1.plot(dfplot.get_group(groupdate).Hour, dfplot.get_group(groupdate).Count, color="blue", marker="o")
ax1.set_xlim(0, 24)
ax1.xaxis.set_ticks(np.arange(0, 25, 2))
ax1.set_ylim(0, maxcount * 1.1)
ax1.set_title("comparable version")
#scaled to maximize visibility per day
ax2.plot(dfplot.get_group(groupdate).Hour, dfplot.get_group(groupdate).Count, color="red", marker="x")
ax2.set_xlim(0, 24)
ax2.xaxis.set_ticks(np.arange(0, 25, 2))
ax2.set_title("expanded version")
plt.tight_layout()
#save optionally
#plt.savefig("MyDataForDay"+str(groupdate)+".eps")
print("All figures generated")
plt.show()
Sample output for one of the days:
created with the following test data:
Date;Time
2020:02:13 ;12:39:02:913
2020:02:13 ;12:39:42:915
2020:02:13 ;13:06:20:718
2020:02:13 ;13:18:25:988
2020:02:13 ;13:34:02:835
2020:02:13 ;13:46:35:793
2020:02:13 ;13:59:10:659
2020:02:13 ;14:14:33:571
2020:02:13 ;14:25:36:381
2020:02:13 ;14:35:38:342
2020:02:13 ;14:46:04:006
2020:02:13 ;14:56:57:346
2020:02:13 ;15:07:39:752
2020:02:13 ;15:19:44:868
2020:02:13 ;15:32:31:438
2020:02:13 ;15:44:44:928
2020:02:13 ;15:56:54:453
2020:02:13 ;16:08:21:023
2020:02:13 ;16:19:17:620
2020:02:13 ;16:29:56:944
2020:02:13 ;16:40:11:132
2020:02:13 ;16:49:12:113
2020:02:13 ;16:57:26:652
2020:02:13 ;16:57:26:652
2020:02:13 ;17:04:22:092
2020:02:17 ;08:58:08:562
2020:02:17 ;08:58:42:545
2020:02:17 ;15:19:44:868
2020:02:17 ;17:32:31:438
2020:02:17 ;17:44:44:928
2020:02:17 ;17:56:54:453
2020:02:17 ;18:08:21:023
2020:03:19 ;06:19:17:620
2020:03:19 ;06:29:56:944
2020:03:19 ;06:40:11:132
2020:03:19 ;14:49:12:113
2020:03:19 ;16:57:26:652
2020:03:19 ;16:57:26:652
2020:03:19 ;17:04:22:092
2020:03:19 ;18:58:08:562
2020:03:19 ;18:58:42:545
Post a Comment for "Plotting A Graph Using Arrays"