簡體   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