简单定制matplotlib/pandas条形图(标签,刻度等) [英] Simple customization of matplotlib/pandas bar chart (labels, ticks, etc.)
问题描述
我是matplotlib的新手,我试图在熊猫中使用它来绘制一些简单的图表.我有一个DataFrame,其中包含两个标签"score"和"person",它们是从另一个DF派生的.
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.
df1 = DataFrame(df, columns=['score','person'])
产生此输出:
我正在尝试创建一个简单的条形图,以用不同的颜色显示每个人,这是我到目前为止所拥有的:
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')
如何对其进行自定义,以使图表在x轴上以唯一的颜色显示人员姓名,并删除图形周围的框架"?如何使其成为水平条形图?
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?
预先感谢您的帮助.
推荐答案
我想这会给你一个想法:
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')
为
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
您将得到:
这篇关于简单定制matplotlib/pandas条形图(标签,刻度等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!