2016-04-21 41 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