2017-02-21 70 views
0

這裏是我的代碼:熊貓區地塊軸標籤

import pandas as pd 
import matplotlib.pyplot as plt 

df = pd.DataFrame({'col_1':[1,2,3], 'col_2': [3,4,5], 
    'date_col': ['2014-12-15','2015-11-07','2016-01-10']}) 
df['date_col'] = pd.to_datetime(df['date_col']).dt.date 
df.plot.area(x = 'date_col', y = ['col_1', 'col_2']) 
plt.show() 

它生產區地塊如預期,但也有更多的X軸標籤,然後我需要的。在數據框中我只需要3個標籤。有什麼建議麼? plot

回答

1

顯示之前添加xticks的情節:

import pandas as pd 
import matplotlib.pyplot as plt 

df = pd.DataFrame({'col_1':[1,2,3], 'col_2': [3,4,5], 
    'date_col': ['2014-12-15','2015-11-07','2016-01-10']}) 
df.plot.area(x = 'date_col', y = ['col_1', 'col_2']) 
plt.xticks(range(len(df.date_col)),df.date_col) 
plt.show() 

所需的輸出:

enter image description here

我希望這有助於。

+0

對不起,它不工作,所有的日期在一個地方,整個情節是扭曲的。 – user1700890

+0

你的意思是*所有日期都在一個地方*?如果日期靠得很近,那麼你很可能會被粘在一起的標籤結束。從你在這裏分享的數據中,我可以得到你所描述的所需輸出。 – Abdou

+0

appologies,您的代碼完美地工作。我忘了我的代碼中還有以下代碼:'df ['date_col'] = pd.to_datetime(df ['date_col'])。dt.date' – user1700890