如何使用数据帧中的所有行名称标记轴 [英] how to label axis with all the row names from dataframe

查看:88
本文介绍了如何使用数据帧中的所有行名称标记轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据框包含大约430行:

I have the dataframe that contains about 430 rows:

     name         Right_Answers  Wrong_Answers
    Alice Ji      7                6
    Eleonora LI   2                5
    Mike The      6                5
    Helen Wo      5                3

为了可视化正确(红色)和错误(蓝色)答案的数量,我正在使用具有以下功能的matplotlib库:

for visualize the number of right (red) and wrong (blue) answers I'm using the matplotlib library with following functions:

g=df.plot(x='name', color=['b','r'], figsize=(100,50))  
ax.xaxis.set_major_locator(MultipleLocator(0.1))
labels = df.name.values[:]
ax = plt.gca()
ax.set_xticklabels(labels, rotation=90)

但是我在X轴上总是只有8个名称作为标签,而不是430(垂直旋转,因此有足够的空间容纳更多的行名称)!为什么会发生?我以为这种方法将所有行名放在轴labels = df.name.values[:]上,但显然没有. 您知道这个问题的其他解决方案吗? 我准备尝试使用seaborn或ggplot等其他库,即使我发现与matplotlib相比它们对dataframe来说都不是很舒服

but I have always only 8 names as labels on X axis, instead of 430 (rotated vertically, so there are enough of space for much more row names)! why it happens? I thought this method put all the row names on axis labels = df.name.values[:] but apparently not. do you know other solution for this problem? I'm ready to try another libraries as seaborn or ggplot, even if I haven't found them very comfortable for dataframe compared with matplotlib

推荐答案

假设数据在数据框中,则可以使用Pandas内置的绘图方法,例如:

Assuming your data is in a dataframe, you can use Pandas' built-in plotting methods, e.g.:

df.plot(kind='bar', color=['red', 'blue'])

这篇关于如何使用数据帧中的所有行名称标记轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆