How to make a more visually appealing bar plot with my data
How to make a more visually appealing bar plot with my data
I am trying to plot this dataframe using barh in matplotlib.
SAMPLE DF:
genre tracks_sold percentage
0 Rock 561 53.377735
1 Alternative & Punk 130 12.369172
2 Metal 124 11.798287
3 R&B/Soul 53 5.042816
4 Blues 36 3.425309
genre_sold_usa.plot.barh()
I am trying to make this plot a little more informative visually.
I would like to have my plot this similar way but more visually appealing:
Please guide me with what else I can use to make it visually appealing.
The most appealing change I see is switching to only the % basis and eliminating the columns below a certain % so that it's readable.
– mauve
Aug 23 at 17:33
1 Answer
1
Given df
as the DataFrame, I just tried to reproduce your provided example. In my opinion, it makes more sense to rotate the y
-labels. 'Visually appealing' is a subjective matter.
df
y
fig = plt.figure(figsize=(10, 6))
ax = df.plot.barh(y='tracks_sold', x='genre', color='mediumseagreen', ec='k', lw=1)
plt.xlim(0, 630)
plt.yticks(rotation=30)
plt.ylabel('')
plt.title('Top Selling Genres in the USA')
ax.legend_.remove()
# Way 1 of putting percentage values
for i in range(len(df)):
ax.text(tracks_sold[i] + 5, 0.98*i, str(int(percentage[i])) + '%', color='black', fontweight='bold')
# Way 2 of putting percentage values
# for i, p in enumerate(ax.patches):
# ax.annotate(str(int(percentage[i])) + '%', (p.get_width() * 1.02, i))
Output
can you explain a bit more on how you placed percentage on top of the bars? Could you explain a little more on the for loop part of the code?
– Sai Kumar
Aug 23 at 18:18
It is done using the
for
loop. You extract the patches (bars) and loop over them via ax.patches
. Since you have 5 patches in the plot, the i
will go from 0 to 4. p.get_width()
gives you the height (horizontal) of each bar. You multiply that with 1.02 just to shift it slightly to the right. str(int(percentage[i]))
just converts the percentage value to closest integer and then converts it to string. ax.annotate
then just puts this string percentage value at (x,y) value where x = p.get_width() * 1.02
and y=0, 1, 2, 3, 4
which is the centre of each bar on y axis– Bazingaa
Aug 23 at 18:21
for
ax.patches
i
p.get_width()
str(int(percentage[i]))
ax.annotate
x = p.get_width() * 1.02
y=0, 1, 2, 3, 4
enumerate
is a way to get the values of the list/array over which you are iterating together with the index of each element which starts at 0 and goes up to len(list)-1
. Feel free to accept the answer if it helps you.– Bazingaa
Aug 23 at 18:23
enumerate
len(list)-1
Can I know what is ec and lw mean? I couldn't find anything in the documentation page.
ax = df.plot.barh(y='tracks_sold', x='genre', color='mediumseagreen', ec='k', lw=1)
– Sai Kumar
Aug 23 at 21:08
ax = df.plot.barh(y='tracks_sold', x='genre', color='mediumseagreen', ec='k', lw=1)
I added the second method in my answer code above and the modified output plot. It is now more or less in the middle. You can control the position further by changing the factor
0.98
currently. But it's a minor difference– Bazingaa
Aug 23 at 21:27
0.98
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
This is kind of a subjective question. Maybe you can be done by switching the stylesheet? jakevdp.github.io/PythonDataScienceHandbook/…
– Felipe Lema
Aug 23 at 17:29