Display percentage above bar chart in matplotlib












1














The following are the pandas dataframe and the bar chart generated from it:



colors_list = ['#5cb85c','#5bc0de','#d9534f']
result.plot(kind='bar',figsize=(15,4),width = 0.8,color = colors_list,edgecolor=None)
plt.legend(labels=result.columns,fontsize= 14)
plt.title("Percentage of Respondents' Interest in Data Science Areas",fontsize= 16)

plt.xticks(fontsize=14)
for spine in plt.gca().spines.values():
spine.set_visible(False)
plt.yticks()


Dataframe



Bar Chart



I need to display the percentages of each interest category for the respective subject above their corresponding bar. I can create a list with the percentages, but I don't understand how to add it on top of the corresponding bar.










share|improve this question




















  • 1




    Please take a look at this link. This should give a decent idea on how to add link
    – Tejeshwar Gurram
    Aug 29 '18 at 15:23










  • How are you creating your list of percentages? can you add that code with list name?
    – Chris A
    Aug 29 '18 at 16:08
















1














The following are the pandas dataframe and the bar chart generated from it:



colors_list = ['#5cb85c','#5bc0de','#d9534f']
result.plot(kind='bar',figsize=(15,4),width = 0.8,color = colors_list,edgecolor=None)
plt.legend(labels=result.columns,fontsize= 14)
plt.title("Percentage of Respondents' Interest in Data Science Areas",fontsize= 16)

plt.xticks(fontsize=14)
for spine in plt.gca().spines.values():
spine.set_visible(False)
plt.yticks()


Dataframe



Bar Chart



I need to display the percentages of each interest category for the respective subject above their corresponding bar. I can create a list with the percentages, but I don't understand how to add it on top of the corresponding bar.










share|improve this question




















  • 1




    Please take a look at this link. This should give a decent idea on how to add link
    – Tejeshwar Gurram
    Aug 29 '18 at 15:23










  • How are you creating your list of percentages? can you add that code with list name?
    – Chris A
    Aug 29 '18 at 16:08














1












1








1


1





The following are the pandas dataframe and the bar chart generated from it:



colors_list = ['#5cb85c','#5bc0de','#d9534f']
result.plot(kind='bar',figsize=(15,4),width = 0.8,color = colors_list,edgecolor=None)
plt.legend(labels=result.columns,fontsize= 14)
plt.title("Percentage of Respondents' Interest in Data Science Areas",fontsize= 16)

plt.xticks(fontsize=14)
for spine in plt.gca().spines.values():
spine.set_visible(False)
plt.yticks()


Dataframe



Bar Chart



I need to display the percentages of each interest category for the respective subject above their corresponding bar. I can create a list with the percentages, but I don't understand how to add it on top of the corresponding bar.










share|improve this question















The following are the pandas dataframe and the bar chart generated from it:



colors_list = ['#5cb85c','#5bc0de','#d9534f']
result.plot(kind='bar',figsize=(15,4),width = 0.8,color = colors_list,edgecolor=None)
plt.legend(labels=result.columns,fontsize= 14)
plt.title("Percentage of Respondents' Interest in Data Science Areas",fontsize= 16)

plt.xticks(fontsize=14)
for spine in plt.gca().spines.values():
spine.set_visible(False)
plt.yticks()


Dataframe



Bar Chart



I need to display the percentages of each interest category for the respective subject above their corresponding bar. I can create a list with the percentages, but I don't understand how to add it on top of the corresponding bar.







python pandas matplotlib






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 29 '18 at 16:39









nnyby

2,14173782




2,14173782










asked Aug 29 '18 at 15:18









raniphoreraniphore

316




316








  • 1




    Please take a look at this link. This should give a decent idea on how to add link
    – Tejeshwar Gurram
    Aug 29 '18 at 15:23










  • How are you creating your list of percentages? can you add that code with list name?
    – Chris A
    Aug 29 '18 at 16:08














  • 1




    Please take a look at this link. This should give a decent idea on how to add link
    – Tejeshwar Gurram
    Aug 29 '18 at 15:23










  • How are you creating your list of percentages? can you add that code with list name?
    – Chris A
    Aug 29 '18 at 16:08








1




1




Please take a look at this link. This should give a decent idea on how to add link
– Tejeshwar Gurram
Aug 29 '18 at 15:23




Please take a look at this link. This should give a decent idea on how to add link
– Tejeshwar Gurram
Aug 29 '18 at 15:23












How are you creating your list of percentages? can you add that code with list name?
– Chris A
Aug 29 '18 at 16:08




How are you creating your list of percentages? can you add that code with list name?
– Chris A
Aug 29 '18 at 16:08












1 Answer
1






active

oldest

votes


















2














Try this way:



colors_list = ['#5cb85c','#5bc0de','#d9534f']

# Change this line to plot percentages instead of absolute values
ax = (result.div(result.sum(1), axis=0)).plot(kind='bar',figsize=(15,4),width = 0.8,color = colors_list,edgecolor=None)
plt.legend(labels=result.columns,fontsize= 14)
plt.title("Percentage of Respondents' Interest in Data Science Areas",fontsize= 16)

plt.xticks(fontsize=14)
for spine in plt.gca().spines.values():
spine.set_visible(False)
plt.yticks()

# Add this loop to add the annotations
for p in ax.patches:
width, height = p.get_width(), p.get_height()
x, y = p.get_xy()
ax.annotate('{:.0%}'.format(height), (p.get_x()+.15*width, p.get_y() + height + 0.01))


plot






share|improve this answer





















  • You can also have the percentages horizontally centered at respective bars center. Modify the ax.annotate expression to this: ax.annotate('{:.0%}'.format(height), (p.get_x()+.5*width, p.get_y() + height + 0.01), ha = 'center')
    – Alex Potapenko
    Dec 3 '18 at 19:19













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52080991%2fdisplay-percentage-above-bar-chart-in-matplotlib%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Try this way:



colors_list = ['#5cb85c','#5bc0de','#d9534f']

# Change this line to plot percentages instead of absolute values
ax = (result.div(result.sum(1), axis=0)).plot(kind='bar',figsize=(15,4),width = 0.8,color = colors_list,edgecolor=None)
plt.legend(labels=result.columns,fontsize= 14)
plt.title("Percentage of Respondents' Interest in Data Science Areas",fontsize= 16)

plt.xticks(fontsize=14)
for spine in plt.gca().spines.values():
spine.set_visible(False)
plt.yticks()

# Add this loop to add the annotations
for p in ax.patches:
width, height = p.get_width(), p.get_height()
x, y = p.get_xy()
ax.annotate('{:.0%}'.format(height), (p.get_x()+.15*width, p.get_y() + height + 0.01))


plot






share|improve this answer





















  • You can also have the percentages horizontally centered at respective bars center. Modify the ax.annotate expression to this: ax.annotate('{:.0%}'.format(height), (p.get_x()+.5*width, p.get_y() + height + 0.01), ha = 'center')
    – Alex Potapenko
    Dec 3 '18 at 19:19


















2














Try this way:



colors_list = ['#5cb85c','#5bc0de','#d9534f']

# Change this line to plot percentages instead of absolute values
ax = (result.div(result.sum(1), axis=0)).plot(kind='bar',figsize=(15,4),width = 0.8,color = colors_list,edgecolor=None)
plt.legend(labels=result.columns,fontsize= 14)
plt.title("Percentage of Respondents' Interest in Data Science Areas",fontsize= 16)

plt.xticks(fontsize=14)
for spine in plt.gca().spines.values():
spine.set_visible(False)
plt.yticks()

# Add this loop to add the annotations
for p in ax.patches:
width, height = p.get_width(), p.get_height()
x, y = p.get_xy()
ax.annotate('{:.0%}'.format(height), (p.get_x()+.15*width, p.get_y() + height + 0.01))


plot






share|improve this answer





















  • You can also have the percentages horizontally centered at respective bars center. Modify the ax.annotate expression to this: ax.annotate('{:.0%}'.format(height), (p.get_x()+.5*width, p.get_y() + height + 0.01), ha = 'center')
    – Alex Potapenko
    Dec 3 '18 at 19:19
















2












2








2






Try this way:



colors_list = ['#5cb85c','#5bc0de','#d9534f']

# Change this line to plot percentages instead of absolute values
ax = (result.div(result.sum(1), axis=0)).plot(kind='bar',figsize=(15,4),width = 0.8,color = colors_list,edgecolor=None)
plt.legend(labels=result.columns,fontsize= 14)
plt.title("Percentage of Respondents' Interest in Data Science Areas",fontsize= 16)

plt.xticks(fontsize=14)
for spine in plt.gca().spines.values():
spine.set_visible(False)
plt.yticks()

# Add this loop to add the annotations
for p in ax.patches:
width, height = p.get_width(), p.get_height()
x, y = p.get_xy()
ax.annotate('{:.0%}'.format(height), (p.get_x()+.15*width, p.get_y() + height + 0.01))


plot






share|improve this answer












Try this way:



colors_list = ['#5cb85c','#5bc0de','#d9534f']

# Change this line to plot percentages instead of absolute values
ax = (result.div(result.sum(1), axis=0)).plot(kind='bar',figsize=(15,4),width = 0.8,color = colors_list,edgecolor=None)
plt.legend(labels=result.columns,fontsize= 14)
plt.title("Percentage of Respondents' Interest in Data Science Areas",fontsize= 16)

plt.xticks(fontsize=14)
for spine in plt.gca().spines.values():
spine.set_visible(False)
plt.yticks()

# Add this loop to add the annotations
for p in ax.patches:
width, height = p.get_width(), p.get_height()
x, y = p.get_xy()
ax.annotate('{:.0%}'.format(height), (p.get_x()+.15*width, p.get_y() + height + 0.01))


plot







share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 29 '18 at 16:17









Chris AChris A

2,297315




2,297315












  • You can also have the percentages horizontally centered at respective bars center. Modify the ax.annotate expression to this: ax.annotate('{:.0%}'.format(height), (p.get_x()+.5*width, p.get_y() + height + 0.01), ha = 'center')
    – Alex Potapenko
    Dec 3 '18 at 19:19




















  • You can also have the percentages horizontally centered at respective bars center. Modify the ax.annotate expression to this: ax.annotate('{:.0%}'.format(height), (p.get_x()+.5*width, p.get_y() + height + 0.01), ha = 'center')
    – Alex Potapenko
    Dec 3 '18 at 19:19


















You can also have the percentages horizontally centered at respective bars center. Modify the ax.annotate expression to this: ax.annotate('{:.0%}'.format(height), (p.get_x()+.5*width, p.get_y() + height + 0.01), ha = 'center')
– Alex Potapenko
Dec 3 '18 at 19:19






You can also have the percentages horizontally centered at respective bars center. Modify the ax.annotate expression to this: ax.annotate('{:.0%}'.format(height), (p.get_x()+.5*width, p.get_y() + height + 0.01), ha = 'center')
– Alex Potapenko
Dec 3 '18 at 19:19




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52080991%2fdisplay-percentage-above-bar-chart-in-matplotlib%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)