繁体   English   中英

如何更改 pandas dataframe plot 的 x 轴上显示的值的数量?

[英]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()

这对我来说很好:我得到一个只有年份的条形图: 用pyplot制作的条形图

您仍然可以应用原始答案中的建议并使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM