简单定制 matplotlib/pandas 条形图(标签、刻度等)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20600006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple customization of matplotlib/pandas bar chart (labels, ticks, etc.)
提问by Luis Miguel
I am new to matplotlib and I am trying to use it within pandas to plot some simple charts. I have a DataFrame that contains two labels "score" and "person", derived from another DF.
我是 matplotlib 的新手,我试图在 Pandas 中使用它来绘制一些简单的图表。我有一个 DataFrame,它包含两个标签“score”和“person”,它们来自另一个 DF。
df1 = DataFrame(df, columns=['score','person'])
Producing this output:
产生这个输出:


I am trying to create a simple bar chart, to show each person in different color, and this is what I have thus far:
我正在尝试创建一个简单的条形图,以不同的颜色显示每个人,这就是我迄今为止所拥有的:
df1.plot(kind='bar', title='Ranking')


How can I customize it so the chart shows the person names in the x axis with unique colors and remove the "frame" surrounding the figure? How can I make it a horizontal bar chart?
如何自定义它以便图表以独特的颜色显示 x 轴中的人名并删除图形周围的“框架”?如何使它成为水平条形图?
Thanks in advance for your help.
在此先感谢您的帮助。
回答by behzad.nouri
I guess this will give you the idea:
我想这会给你的想法:
df = pd.DataFrame({'score':np.random.randn(6),
'person':[x*3 for x in list('ABCDEF')]})
ax = plt.subplot(111)
df.score.plot(ax=ax, kind='barh', color=list('rgbkym'), title='ranking')
ax.axis('off')
for i, x in enumerate(df.person):
ax.text(0, i + .5, x, ha='right', fontsize='large')
for
为了
person score
0 AAA 1.79
1 BBB 0.31
2 CCC -0.52
3 DDD 1.59
4 EEE 0.59
5 FFF -1.03
you will get:
你会得到:


回答by MattDMo
Check out the docs for pd.DataFrame.plot()for more info. You'll also definitely want to read up on plotting with matplotlibas well as the matplotlibdocsthemselves.
查看文档以pd.DataFrame.plot()获取更多信息。您肯定还想阅读有关绘图matplotlib以及matplotlib文档本身的信息。
Getting a horizontal bar graph is easy, just use `kind='barh'.
获得水平条形图很容易,只需使用`kind='barh'。

