2

I have been trying to put the percentage values of each bar on the plot for the last 3 days with no use!!

enter image description here

df_ds.sort_values('Very interested', ascending = False)
df_dsp = ((100*df_ds / 2233).round(2).astype(str) + '%')

#PLOTTING
ax1 = df_ds.plot(kind = 'bar',
                figsize = (20,8),
                width = 0.8,
                color = ('#5cb85c', '#5bc0de', '#d9534f'),
                fontsize = 14)
ax1.set_title("Percentage of Respondents' Interest in Data Science", fontsize = 16)
ax1.legend(fontsize = 14)
ax1.spines['top'].set_visible(False)
ax1.spines['right'].set_visible(False)
ax1.spines['left'].set_visible(False)
James Z
  • 12,209
  • 10
  • 24
  • 44
Mina Makary
  • 29
  • 1
  • 3
  • Please don't post images of code/data/Tracebacks. Just copy the text, paste it in your question and format it as code. [You should not post code as an image because:](https://meta.stackoverflow.com/a/285557/2823755). We should be able to copy from your question, paste it into an editor and reproduce the problem. – wwii Aug 31 '19 at 14:40
  • Have you worked through the [Matplotlib Tutorials](https://matplotlib.org/tutorials/index.html) - at least the Usage Guide, Pyplot tutorial, and Artist tutorial - then the Text tutorials? – wwii Aug 31 '19 at 14:47
  • How/where are you trying to *put* that text? – wwii Aug 31 '19 at 14:48
  • 3
    This question is part of the test from _Data Visualization with Python_ course on Coursera. The code of conduct states that the submitter must present the original work of their own. Getting the question answered here and submitting it is a clear violation of rules. – Nikola Malešević Jun 08 '20 at 11:25

2 Answers2

5

You can use patches from matplotlib, and use get_x() and get_height() to obtain the coordinates for the percentage values. Assuming that we have imported your dataframe, your code thus becomes

import pandas as pd
import matplotlib.pyplot as plt

# Assuming that we imported df_ds...
df_ds.sort_values('Very interested', ascending = False)

# Formatting to a percentage will be done in the plotting
df_dsp = df_ds / 2233

#PLOTTING
ax1 = df_ds.plot(kind = 'bar',
                figsize = (20,8),
                width = 0.8,
                color = ('#5cb85c', '#5bc0de', '#d9534f'),
                fontsize = 14)
ax1.set_title("Percentage of Respondents' Interest in Data Science", 
             fontsize = 16)
ax1.legend(fontsize = 14)
ax1.spines['top'].set_visible(False)
ax1.spines['right'].set_visible(False)
ax1.spines['left'].set_visible(False)

# Adding the percentage values    
for p in ax1.patches:
    ax1.annotate("{:.2%}".format(p.get_height()),
                xy=(p.get_x()+0.02, p.get_height()+0.01))

where last two lines are the code for the percentage values. The result looks as follows:

enter image description here

RatonWasher
  • 250
  • 3
  • 9
1

Hope this example helps.

import pandas as pd
import matplotlib.pyplot as plt

raw_data = {'first_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], 'pre_score': [4, 24, 31, 2, 3]}
df = pd.DataFrame(raw_data, columns = ['first_name', 'pre_score'])

# Create a figure with a single subplot
f, ax = plt.subplots(1, figsize=(10,5))

# Set bar width at 1
bar_width = 1

# positions of the left bar-boundaries
bar_l = [i for i in range(len(df['pre_score']))] 
## positions of the right bar-boundaries
tick_pos = [i+(bar_width/2) for i in bar_l] 
pre_rel = [i / sum(df['pre_score']) * 100 for  i in df['pre_score'] ]
ax.bar(bar_l, pre_rel, width=bar_width)


plt.xticks(tick_pos, df['first_name'])
ax.set_ylabel("Percentage")

plt.xlim([min(tick_pos)-bar_width, max(tick_pos)+bar_width])
plt.ylim(0, 60)
# Adjust the position of first_name
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
plt.show()

enter image description here

ComplicatedPhenomenon
  • 4,055
  • 2
  • 18
  • 45