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 inenumerate(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;Time2020:02:13 ;12:39:02:9132020:02:13 ;12:39:42:9152020:02:13 ;13:06:20:7182020:02:13 ;13:18:25:9882020:02:13 ;13:34:02:8352020:02:13 ;13:46:35:7932020:02:13 ;13:59:10:6592020:02:13 ;14:14:33:5712020:02:13 ;14:25:36:3812020:02:13 ;14:35:38:3422020:02:13 ;14:46:04:0062020:02:13 ;14:56:57:3462020:02:13 ;15:07:39:7522020:02:13 ;15:19:44:8682020:02:13 ;15:32:31:4382020:02:13 ;15:44:44:9282020:02:13 ;15:56:54:4532020:02:13 ;16:08:21:0232020:02:13 ;16:19:17:6202020:02:13 ;16:29:56:9442020:02:13 ;16:40:11:1322020:02:13 ;16:49:12:1132020:02:13 ;16:57:26:6522020:02:13 ;16:57:26:6522020:02:13 ;17:04:22:0922020:02:17 ;08:58:08:5622020:02:17 ;08:58:42:5452020:02:17 ;15:19:44:8682020:02:17 ;17:32:31:4382020:02:17 ;17:44:44:9282020:02:17 ;17:56:54:4532020:02:17 ;18:08:21:0232020:03:19 ;06:19:17:6202020:03:19 ;06:29:56:9442020:03:19 ;06:40:11:1322020:03:19 ;14:49:12:1132020:03:19 ;16:57:26:6522020:03:19 ;16:57:26:6522020:03:19 ;17:04:22:0922020:03:19 ;18:58:08:5622020:03:19 ;18:58:42:545
Post a Comment for "Plotting A Graph Using Arrays"