How can I solve my show number overlap my bar-chart when I using matplotlib?
And this is my code below :
def generate_bar_chart(self, x_data, y_data, legend, pic_name):
n = len(x_data)
plt.bar(range(n), y_data, align='center', color='steelblue', alpha=0.8, label=legend)
plt.xticks(range(n), x_data, rotation=90)
for x, y in enumerate(y_data):
plt.text(x, y+100, '%s' % round(y, 1), ha='center', rotation=90, alpha=0.8)
plt.grid(axis='y', linestyle='-', alpha=0.8)
plt.legend()
plt.tight_layout()
pic_file = os.path.join(self.pic_path, pic_name)
plt.savefig(pic_file)
plt.close()
mayber there something wrong with enumerate() and plt.text, please give me some advice, thanks!
python matplotlib bar-chart
add a comment |
And this is my code below :
def generate_bar_chart(self, x_data, y_data, legend, pic_name):
n = len(x_data)
plt.bar(range(n), y_data, align='center', color='steelblue', alpha=0.8, label=legend)
plt.xticks(range(n), x_data, rotation=90)
for x, y in enumerate(y_data):
plt.text(x, y+100, '%s' % round(y, 1), ha='center', rotation=90, alpha=0.8)
plt.grid(axis='y', linestyle='-', alpha=0.8)
plt.legend()
plt.tight_layout()
pic_file = os.path.join(self.pic_path, pic_name)
plt.savefig(pic_file)
plt.close()
mayber there something wrong with enumerate() and plt.text, please give me some advice, thanks!
python matplotlib bar-chart
add a comment |
And this is my code below :
def generate_bar_chart(self, x_data, y_data, legend, pic_name):
n = len(x_data)
plt.bar(range(n), y_data, align='center', color='steelblue', alpha=0.8, label=legend)
plt.xticks(range(n), x_data, rotation=90)
for x, y in enumerate(y_data):
plt.text(x, y+100, '%s' % round(y, 1), ha='center', rotation=90, alpha=0.8)
plt.grid(axis='y', linestyle='-', alpha=0.8)
plt.legend()
plt.tight_layout()
pic_file = os.path.join(self.pic_path, pic_name)
plt.savefig(pic_file)
plt.close()
mayber there something wrong with enumerate() and plt.text, please give me some advice, thanks!
python matplotlib bar-chart
And this is my code below :
def generate_bar_chart(self, x_data, y_data, legend, pic_name):
n = len(x_data)
plt.bar(range(n), y_data, align='center', color='steelblue', alpha=0.8, label=legend)
plt.xticks(range(n), x_data, rotation=90)
for x, y in enumerate(y_data):
plt.text(x, y+100, '%s' % round(y, 1), ha='center', rotation=90, alpha=0.8)
plt.grid(axis='y', linestyle='-', alpha=0.8)
plt.legend()
plt.tight_layout()
pic_file = os.path.join(self.pic_path, pic_name)
plt.savefig(pic_file)
plt.close()
mayber there something wrong with enumerate() and plt.text, please give me some advice, thanks!
python matplotlib bar-chart
python matplotlib bar-chart
asked Nov 19 '18 at 7:43
SucySucy
198115
198115
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I would update 2 things in how you are plotting this:
set the text alignment -
verticalalignment='bottom'
(or the shorthandva
), which makes the label placement insensitive to the label length.use the
plt.annotate
instead ofplt.text
. For your use case, the parameters are very similar, but it is also robust to both the scale of the data and the current zoom level.
The xytext=(0,5)
makes the text start in the center of your bar, and 5 points above it (also include textcoords='offset points').
for x, y in enumerate(y_data):
plt.annotate('%s' % round(y, 1), xy=(x, y),
xytext=(0, 5), textcoords='offset points',
va='bottom', ha='center', rotation=90)
For reference, to just achieve #1 can be done with plt.text
:
for x, y in enumerate(y_data):
plt.text(x, y+300, '%s' % round(y, 1), ha='center',
va='bottom', rotation=90, alpha=0.8)
It is woks well, thanks so much, Futher, there multiple horizontal bars in one chart, how can I add number label above bars? like the answer of https://stackoverflow.com/questions/15201386/how-to-plot-multiple-horizontal-bars-in-one-chart-with-matplotlib, xytext(0,5),0 must be change?
– Sucy
Nov 20 '18 at 5:30
maybe I should change the position of x,y as xy=(y,x)?
– Sucy
Nov 20 '18 at 6:29
add a comment |
If I understand correctly, you want the numbers to be printed above the bars ? If so, then just modify the offset for y in the call to text(). Currently you add 100, but keep in mind this is expressed in axis coordinates, so 100 is still a small value considering the total range of your y axis. Try 500 for example.
In order to set a similar offset to all bars, you have to define your offset from a relative point of view. You could try 1.15 * y
instead of y+100
in the text() call.
thanks so lot , I have add 2000, and it work well.
– Sucy
Nov 19 '18 at 8:24
No worries, feel free to accept the answer.
– Patol75
Nov 19 '18 at 8:45
I have question again, different number may be set different offset. As above, the max number is 18129 and offset set 2000 is well, but if the max number is 100 the offset will be smaller than 2000, maybe 10 is enough. So how can I set fixed offset for different?
– Sucy
Nov 19 '18 at 8:59
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370283%2fhow-can-i-solve-my-show-number-overlap-my-bar-chart-when-i-using-matplotlib%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I would update 2 things in how you are plotting this:
set the text alignment -
verticalalignment='bottom'
(or the shorthandva
), which makes the label placement insensitive to the label length.use the
plt.annotate
instead ofplt.text
. For your use case, the parameters are very similar, but it is also robust to both the scale of the data and the current zoom level.
The xytext=(0,5)
makes the text start in the center of your bar, and 5 points above it (also include textcoords='offset points').
for x, y in enumerate(y_data):
plt.annotate('%s' % round(y, 1), xy=(x, y),
xytext=(0, 5), textcoords='offset points',
va='bottom', ha='center', rotation=90)
For reference, to just achieve #1 can be done with plt.text
:
for x, y in enumerate(y_data):
plt.text(x, y+300, '%s' % round(y, 1), ha='center',
va='bottom', rotation=90, alpha=0.8)
It is woks well, thanks so much, Futher, there multiple horizontal bars in one chart, how can I add number label above bars? like the answer of https://stackoverflow.com/questions/15201386/how-to-plot-multiple-horizontal-bars-in-one-chart-with-matplotlib, xytext(0,5),0 must be change?
– Sucy
Nov 20 '18 at 5:30
maybe I should change the position of x,y as xy=(y,x)?
– Sucy
Nov 20 '18 at 6:29
add a comment |
I would update 2 things in how you are plotting this:
set the text alignment -
verticalalignment='bottom'
(or the shorthandva
), which makes the label placement insensitive to the label length.use the
plt.annotate
instead ofplt.text
. For your use case, the parameters are very similar, but it is also robust to both the scale of the data and the current zoom level.
The xytext=(0,5)
makes the text start in the center of your bar, and 5 points above it (also include textcoords='offset points').
for x, y in enumerate(y_data):
plt.annotate('%s' % round(y, 1), xy=(x, y),
xytext=(0, 5), textcoords='offset points',
va='bottom', ha='center', rotation=90)
For reference, to just achieve #1 can be done with plt.text
:
for x, y in enumerate(y_data):
plt.text(x, y+300, '%s' % round(y, 1), ha='center',
va='bottom', rotation=90, alpha=0.8)
It is woks well, thanks so much, Futher, there multiple horizontal bars in one chart, how can I add number label above bars? like the answer of https://stackoverflow.com/questions/15201386/how-to-plot-multiple-horizontal-bars-in-one-chart-with-matplotlib, xytext(0,5),0 must be change?
– Sucy
Nov 20 '18 at 5:30
maybe I should change the position of x,y as xy=(y,x)?
– Sucy
Nov 20 '18 at 6:29
add a comment |
I would update 2 things in how you are plotting this:
set the text alignment -
verticalalignment='bottom'
(or the shorthandva
), which makes the label placement insensitive to the label length.use the
plt.annotate
instead ofplt.text
. For your use case, the parameters are very similar, but it is also robust to both the scale of the data and the current zoom level.
The xytext=(0,5)
makes the text start in the center of your bar, and 5 points above it (also include textcoords='offset points').
for x, y in enumerate(y_data):
plt.annotate('%s' % round(y, 1), xy=(x, y),
xytext=(0, 5), textcoords='offset points',
va='bottom', ha='center', rotation=90)
For reference, to just achieve #1 can be done with plt.text
:
for x, y in enumerate(y_data):
plt.text(x, y+300, '%s' % round(y, 1), ha='center',
va='bottom', rotation=90, alpha=0.8)
I would update 2 things in how you are plotting this:
set the text alignment -
verticalalignment='bottom'
(or the shorthandva
), which makes the label placement insensitive to the label length.use the
plt.annotate
instead ofplt.text
. For your use case, the parameters are very similar, but it is also robust to both the scale of the data and the current zoom level.
The xytext=(0,5)
makes the text start in the center of your bar, and 5 points above it (also include textcoords='offset points').
for x, y in enumerate(y_data):
plt.annotate('%s' % round(y, 1), xy=(x, y),
xytext=(0, 5), textcoords='offset points',
va='bottom', ha='center', rotation=90)
For reference, to just achieve #1 can be done with plt.text
:
for x, y in enumerate(y_data):
plt.text(x, y+300, '%s' % round(y, 1), ha='center',
va='bottom', rotation=90, alpha=0.8)
answered Nov 19 '18 at 9:30
BonlenfumBonlenfum
11.2k13041
11.2k13041
It is woks well, thanks so much, Futher, there multiple horizontal bars in one chart, how can I add number label above bars? like the answer of https://stackoverflow.com/questions/15201386/how-to-plot-multiple-horizontal-bars-in-one-chart-with-matplotlib, xytext(0,5),0 must be change?
– Sucy
Nov 20 '18 at 5:30
maybe I should change the position of x,y as xy=(y,x)?
– Sucy
Nov 20 '18 at 6:29
add a comment |
It is woks well, thanks so much, Futher, there multiple horizontal bars in one chart, how can I add number label above bars? like the answer of https://stackoverflow.com/questions/15201386/how-to-plot-multiple-horizontal-bars-in-one-chart-with-matplotlib, xytext(0,5),0 must be change?
– Sucy
Nov 20 '18 at 5:30
maybe I should change the position of x,y as xy=(y,x)?
– Sucy
Nov 20 '18 at 6:29
It is woks well, thanks so much, Futher, there multiple horizontal bars in one chart, how can I add number label above bars? like the answer of https://stackoverflow.com/questions/15201386/how-to-plot-multiple-horizontal-bars-in-one-chart-with-matplotlib, xytext(0,5),0 must be change?
– Sucy
Nov 20 '18 at 5:30
It is woks well, thanks so much, Futher, there multiple horizontal bars in one chart, how can I add number label above bars? like the answer of https://stackoverflow.com/questions/15201386/how-to-plot-multiple-horizontal-bars-in-one-chart-with-matplotlib, xytext(0,5),0 must be change?
– Sucy
Nov 20 '18 at 5:30
maybe I should change the position of x,y as xy=(y,x)?
– Sucy
Nov 20 '18 at 6:29
maybe I should change the position of x,y as xy=(y,x)?
– Sucy
Nov 20 '18 at 6:29
add a comment |
If I understand correctly, you want the numbers to be printed above the bars ? If so, then just modify the offset for y in the call to text(). Currently you add 100, but keep in mind this is expressed in axis coordinates, so 100 is still a small value considering the total range of your y axis. Try 500 for example.
In order to set a similar offset to all bars, you have to define your offset from a relative point of view. You could try 1.15 * y
instead of y+100
in the text() call.
thanks so lot , I have add 2000, and it work well.
– Sucy
Nov 19 '18 at 8:24
No worries, feel free to accept the answer.
– Patol75
Nov 19 '18 at 8:45
I have question again, different number may be set different offset. As above, the max number is 18129 and offset set 2000 is well, but if the max number is 100 the offset will be smaller than 2000, maybe 10 is enough. So how can I set fixed offset for different?
– Sucy
Nov 19 '18 at 8:59
add a comment |
If I understand correctly, you want the numbers to be printed above the bars ? If so, then just modify the offset for y in the call to text(). Currently you add 100, but keep in mind this is expressed in axis coordinates, so 100 is still a small value considering the total range of your y axis. Try 500 for example.
In order to set a similar offset to all bars, you have to define your offset from a relative point of view. You could try 1.15 * y
instead of y+100
in the text() call.
thanks so lot , I have add 2000, and it work well.
– Sucy
Nov 19 '18 at 8:24
No worries, feel free to accept the answer.
– Patol75
Nov 19 '18 at 8:45
I have question again, different number may be set different offset. As above, the max number is 18129 and offset set 2000 is well, but if the max number is 100 the offset will be smaller than 2000, maybe 10 is enough. So how can I set fixed offset for different?
– Sucy
Nov 19 '18 at 8:59
add a comment |
If I understand correctly, you want the numbers to be printed above the bars ? If so, then just modify the offset for y in the call to text(). Currently you add 100, but keep in mind this is expressed in axis coordinates, so 100 is still a small value considering the total range of your y axis. Try 500 for example.
In order to set a similar offset to all bars, you have to define your offset from a relative point of view. You could try 1.15 * y
instead of y+100
in the text() call.
If I understand correctly, you want the numbers to be printed above the bars ? If so, then just modify the offset for y in the call to text(). Currently you add 100, but keep in mind this is expressed in axis coordinates, so 100 is still a small value considering the total range of your y axis. Try 500 for example.
In order to set a similar offset to all bars, you have to define your offset from a relative point of view. You could try 1.15 * y
instead of y+100
in the text() call.
edited Nov 19 '18 at 9:02
answered Nov 19 '18 at 8:11
Patol75Patol75
6236
6236
thanks so lot , I have add 2000, and it work well.
– Sucy
Nov 19 '18 at 8:24
No worries, feel free to accept the answer.
– Patol75
Nov 19 '18 at 8:45
I have question again, different number may be set different offset. As above, the max number is 18129 and offset set 2000 is well, but if the max number is 100 the offset will be smaller than 2000, maybe 10 is enough. So how can I set fixed offset for different?
– Sucy
Nov 19 '18 at 8:59
add a comment |
thanks so lot , I have add 2000, and it work well.
– Sucy
Nov 19 '18 at 8:24
No worries, feel free to accept the answer.
– Patol75
Nov 19 '18 at 8:45
I have question again, different number may be set different offset. As above, the max number is 18129 and offset set 2000 is well, but if the max number is 100 the offset will be smaller than 2000, maybe 10 is enough. So how can I set fixed offset for different?
– Sucy
Nov 19 '18 at 8:59
thanks so lot , I have add 2000, and it work well.
– Sucy
Nov 19 '18 at 8:24
thanks so lot , I have add 2000, and it work well.
– Sucy
Nov 19 '18 at 8:24
No worries, feel free to accept the answer.
– Patol75
Nov 19 '18 at 8:45
No worries, feel free to accept the answer.
– Patol75
Nov 19 '18 at 8:45
I have question again, different number may be set different offset. As above, the max number is 18129 and offset set 2000 is well, but if the max number is 100 the offset will be smaller than 2000, maybe 10 is enough. So how can I set fixed offset for different?
– Sucy
Nov 19 '18 at 8:59
I have question again, different number may be set different offset. As above, the max number is 18129 and offset set 2000 is well, but if the max number is 100 the offset will be smaller than 2000, maybe 10 is enough. So how can I set fixed offset for different?
– Sucy
Nov 19 '18 at 8:59
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370283%2fhow-can-i-solve-my-show-number-overlap-my-bar-chart-when-i-using-matplotlib%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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