2016-04-21 39 views
2

我得到這個數據幀DF,Python的大熊貓:條形圖X軸問題

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 

table = {'Count_Product': pd.Series([1,2,3]), 'Count_Transaction': pd.Series([1,1,2])} 
df = pd.DataFrame(table) 
df 

Count_Product Count_Transaction 
0 1 1 
1 2 1 
2 3 2 

我想做一個簡單的柱狀圖中,其中X軸是Count_Product和Y軸是Count_Transaction。我用下面的代碼來生成,

%matplotlib inline 

ax = df['Count_Product'].plot(kind='bar', figsize=(5,5), color='blue') 
ax.set_ylabel("Count_Transaction") 
ax.set_xlabel("Count_Product") 
patches, labels = ax.get_legend_handles_labels() 
ax.legend(patches, labels, loc='best') 

和圖形輸出,

enter image description here

我的問題是在x軸顯示0,1,2代替1,2, 3從Count_Product列。它看起來像是自動將第一列作爲x軸。

有誰知道我該如何編碼正確的列來使用Count_Product列作爲X軸?

感謝, Lobbie

回答

3

我認爲你從Count_Product柱首先需要set_index然後更改df['Count_Product'].plotdf['Count_Transaction'].plot

df = df.set_index('Count_Product') 

ax = df['Count_Transaction'].plot(kind='bar', figsize=(5,5), color='blue') 
ax.set_ylabel("Count_Transaction") 
ax.set_xlabel("Count_Product") 
patches, labels = ax.get_legend_handles_labels() 
ax.legend(patches, labels, loc='best') 

graph

+0

嗨Jezrael,太感謝你了!我在想我需要在某個地方設置索引,然後你給我看。再次感謝Lobbie。 – Lobbie

+0

是的,我還需要「將df ['Count_Product']。plot改爲df ['Count_Transaction']。plot」。我也注意到我不需要做df ['Count_Transaction'],只是使用df.plot也可以。再次感謝。 – Lobbie

+0

如果我的回答很有幫助,請不要忘記[接受](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)它。謝謝。 – jezrael