简体   繁体   English

如何将 pandas dataframe plot 从垂直更改为水平并为轴标签添加行距?

[英]How to change pandas dataframe plot from vertical to horizontal and add line spacing for the axis labels?

I plotted a dataframe using the following command我使用以下命令绘制了 dataframe

df['col1'].value_counts().plot.bar(orientation='vertical')

在此处输入图像描述

The output is shown as above. output 如上图所示。 I would like to change two things:我想改变两点:

  1. swap the x and y axis so i can read the current x axis text from left to right (NOTE the current x axis labels contains long text and it is hard to read it vertically. Picture is truncated at the bottom for simplicity of this post)交换 x 和 y 轴,这样我就可以从左到右阅读当前的 x 轴文本(注意当前的 x 轴标签包含长文本,很难垂直阅读。为简单起见,图片在底部被截断)
  2. add line spacing between the labels for the current x axis so it helps reading (will be on the y axis if step 1 is done).在当前 x 轴的标签之间添加行距,以便有助于阅读(如果步骤 1 完成,将在 y 轴上)。 If increasing the size of the plot is needed, please suggest how to do so.如果需要增加 plot 的尺寸,请建议如何做。

I have tried a few things but none of them have any visible effects.我已经尝试了一些东西,但它们都没有任何明显的效果。 eg,例如,

plt.xticks(linespacing=1.5)
np.linspace(-1, 1, 10000)

You can plot horizontally directly.您可以直接水平 plot。 Also, consider setting a different font size in advance, eg:另外,请考虑提前设置不同的字体大小,例如:

plt.rcParams.update({'font.size': 6})  # Arbitrary
df['col1'].value_counts().plot(kind="barh", figsize=(5, 20))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM