'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
df['col1'].value_counts().plot.bar(orientation='vertical')
The output is shown as above. I would like to change two things:
- 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)
- 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). If increasing the size of the plot is needed, please suggest how to do so.
I have tried a few things but none of them have any visible effects. e.g.,
plt.xticks(linespacing=1.5)
np.linspace(-1, 1, 10000)
Solution 1:[1]
You can plot horizontally directly. Also, consider setting a different font size in advance, e.g.:
plt.rcParams.update({'font.size': 6}) # Arbitrary
df['col1'].value_counts().plot(kind="barh", figsize=(5, 20))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | fsl |

