【发布时间】:2020-11-23 19:35:33
【问题描述】:
我有gathered a code 可以根据多天的数据绘制图表。我有一个包含超过 40 天和 19k 时间戳的数据文件,我需要一个情节,每天一个。我希望 python 将它们生成为不同的图。
先生。 T 在提供代码方面帮助了我很多,但我无法管理代码以使其绘制单个图而不是全部在一个子图中。有人可以帮我解决这个问题吗?
图片显示当前输出:
我的代码:
import matplotlib.pyplot as plt
import numpy as np
#read your data and create datetime index
df= pd.read_csv('test-februari.csv', 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()
【问题讨论】:
标签: python pandas matplotlib