[英]How do I change the amount of values shown on the x-axis of a pandas dataframe plot?
我又一次陷入了 python:我找不到一种很好的方式来表示我的数据。
我有一堆放电,我想要 plot。 但是,当我这样做时,x 轴上的日期太拥挤了。 我怎样才能改变它只显示几年(或几个月)的表示。 我的代码如下,我当前结果的图是这样的: bargraph
提前致谢!
time = pd.Series(pd.period_range('1/1/1970',
freq='M', periods=12*12))
y = np.zeros(int(len(discharge)/3))
for i in range(int(len(discharge)/3)):
y[i] = discharge.sum(axis=1)[i*3]
qdi_bar = pd.DataFrame()
qdi_bar['sum_discharge'] = y
qdi_bar.index = time
qdi_bar.plot.bar()
编辑1:您可以将此代码与matplotlib
一起使用:
time = pd.Series(pd.period_range('1/1/1970',
freq='M', periods=12*12))
# Change to timestamps instead or period ranges (the latter cause a TypeError when used with pyplot)
time = time.apply(lambda x : x.to_timestamp())
y = np.zeros(int(len(discharge)/3))
for i in range(int(len(discharge)/3)):
y[i] = discharge.iloc[i*3].sum()
qdi_bar = pd.DataFrame()
qdi_bar['sum_discharge'] = y
qdi_bar.index = time
#Plot with matplotlib.pyplot instead of pandas
plt.bar(qdi_bar.index, qdi_bar["sum_discharge"], width = 15)
plt.show()
您仍然可以应用原始答案中的建议并使用matplotlib.dates
进一步自定义结果。
Original post: I don't know of any way to do this with pandas.DataFrame.plot()
, but if you are willing to use matplotlib
instead, then you can customize your x-axis as you like with matplotlib.dates .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.