Matplotlib leged disappears using bbox_to_anchor
I'm trying to put my matplotlib legend in a custom location using the bbox_to_anchor command. I want the upper left hand corner to be at the coordinates x=40 and y=130 so my python code looks as follows:
plt.legend([o,h,n,k,l,a],
["data" , "data" , "data" , "data" , "data" , "data"] ,
scatterpoints = 1 , bbox_to_anchor=(40,130) , loc='upper left' )
When I actually go to plot though the legend is simply gone. If I remove the bbox_to_anchor option then the legend is on the graph. The rest of my code is as follows:
import matplotlib.pyplot as plt
from matplotlib import rc
#eu-152 data
eu152_10_1 = ([(10,1)])
#eu152_10_3 = ([(10,3)])
#eu152_10_30 = ([(10,30)])
eu152_10_60 = ([(10,60)])
eu152_10_120 = ([(10,120)])
#eu152_30_1 = ([(30,1)])
eu152_30_3 = ([(30,3)])
eu152_30_30 = ([(30,30)])
eu152_30_60 = ([(30,60)])
eu152_30_120 = ([(30,120)])
eu152_90_1 = ([(90,1)])
eu152_90_3 = ([(90,3)])
eu152_90_30 = ([(90,30)])
eu152_90_60 = ([(90,60)])
eu152_90_120 = ([(90,120)])
plt.rcParams['legend.loc'] = 'best'
plt.figure(1)
#plt.autoscale(enable=True, axis='y', tight=None)
#plt.autoscale(enable=True, axis='x', tight=None)
#extra = Rectangle((0, 0), 1, 1, fc="w", fill=False, edgecolor='none', linewidth=0)
a = plt.scatter(*zip(*eu152_10_1) , color='k', marker='o' , s=300)
#b = plt.scatter(*zip(*eu152_10_3) , color='r', marker='D' , s=300)
#c = plt.scatter(*zip(*eu152_10_30) , color='r', marker='D' , s=300)
d = plt.scatter(*zip(*eu152_10_60) , color='m', marker='o' , s=300)
e = plt.scatter(*zip(*eu152_10_120) , color='r', marker='o' , s=300)
#f = plt.scatter(*zip(*eu152_30_1) , color='r', marker='D' , s=300)
g = plt.scatter(*zip(*eu152_30_3) , color='b', marker='o', s=300)
h = plt.scatter(*zip(*eu152_30_30) , color='g', marker='o', s=300)
i = plt.scatter(*zip(*eu152_30_60) , color='k', marker='o', s=300)
j = plt.scatter(*zip(*eu152_30_120) , color='m', marker='o', s=300)
k = plt.scatter(*zip(*eu152_90_1) , color='c', marker='o', s=300)
l = plt.scatter(*zip(*eu152_90_3) , color='m', marker='o', s=300)
m = plt.scatter(*zip(*eu152_90_30) , color='c', marker='o', s=300)
n = plt.scatter(*zip(*eu152_90_60) , color='b', marker='o', s=300)
o = plt.scatter(*zip(*eu152_90_120) , color='r', marker='o', s=300)
rc('text', usetex=True)
plt.ylim(0, 150)
plt.xlim(0, 100)
plt.xlabel('Power Level (kW)', fontsize=26 , labelpad = 20)
plt.ylabel('Irradiation Time (min.)' , fontsize=26)
plt.tick_params(axis='x' , labelsize=20)
plt.tick_params(axis='y' , labelsize=20)
plt.legend([o,h,n,k,l,a],
["data" , "data" , "data" , "data" , "data" , "data"] ,
scatterpoints = 1 , bbox_to_anchor=(40,130) , loc='upper left' )
plt.xticks([0 , 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100])
plt.yticks([ 1 , 30 , 60 , 90 , 120 ])
plt.grid(True)
print('plots created')
plt.show()
python matplotlib legend
add a comment |
I'm trying to put my matplotlib legend in a custom location using the bbox_to_anchor command. I want the upper left hand corner to be at the coordinates x=40 and y=130 so my python code looks as follows:
plt.legend([o,h,n,k,l,a],
["data" , "data" , "data" , "data" , "data" , "data"] ,
scatterpoints = 1 , bbox_to_anchor=(40,130) , loc='upper left' )
When I actually go to plot though the legend is simply gone. If I remove the bbox_to_anchor option then the legend is on the graph. The rest of my code is as follows:
import matplotlib.pyplot as plt
from matplotlib import rc
#eu-152 data
eu152_10_1 = ([(10,1)])
#eu152_10_3 = ([(10,3)])
#eu152_10_30 = ([(10,30)])
eu152_10_60 = ([(10,60)])
eu152_10_120 = ([(10,120)])
#eu152_30_1 = ([(30,1)])
eu152_30_3 = ([(30,3)])
eu152_30_30 = ([(30,30)])
eu152_30_60 = ([(30,60)])
eu152_30_120 = ([(30,120)])
eu152_90_1 = ([(90,1)])
eu152_90_3 = ([(90,3)])
eu152_90_30 = ([(90,30)])
eu152_90_60 = ([(90,60)])
eu152_90_120 = ([(90,120)])
plt.rcParams['legend.loc'] = 'best'
plt.figure(1)
#plt.autoscale(enable=True, axis='y', tight=None)
#plt.autoscale(enable=True, axis='x', tight=None)
#extra = Rectangle((0, 0), 1, 1, fc="w", fill=False, edgecolor='none', linewidth=0)
a = plt.scatter(*zip(*eu152_10_1) , color='k', marker='o' , s=300)
#b = plt.scatter(*zip(*eu152_10_3) , color='r', marker='D' , s=300)
#c = plt.scatter(*zip(*eu152_10_30) , color='r', marker='D' , s=300)
d = plt.scatter(*zip(*eu152_10_60) , color='m', marker='o' , s=300)
e = plt.scatter(*zip(*eu152_10_120) , color='r', marker='o' , s=300)
#f = plt.scatter(*zip(*eu152_30_1) , color='r', marker='D' , s=300)
g = plt.scatter(*zip(*eu152_30_3) , color='b', marker='o', s=300)
h = plt.scatter(*zip(*eu152_30_30) , color='g', marker='o', s=300)
i = plt.scatter(*zip(*eu152_30_60) , color='k', marker='o', s=300)
j = plt.scatter(*zip(*eu152_30_120) , color='m', marker='o', s=300)
k = plt.scatter(*zip(*eu152_90_1) , color='c', marker='o', s=300)
l = plt.scatter(*zip(*eu152_90_3) , color='m', marker='o', s=300)
m = plt.scatter(*zip(*eu152_90_30) , color='c', marker='o', s=300)
n = plt.scatter(*zip(*eu152_90_60) , color='b', marker='o', s=300)
o = plt.scatter(*zip(*eu152_90_120) , color='r', marker='o', s=300)
rc('text', usetex=True)
plt.ylim(0, 150)
plt.xlim(0, 100)
plt.xlabel('Power Level (kW)', fontsize=26 , labelpad = 20)
plt.ylabel('Irradiation Time (min.)' , fontsize=26)
plt.tick_params(axis='x' , labelsize=20)
plt.tick_params(axis='y' , labelsize=20)
plt.legend([o,h,n,k,l,a],
["data" , "data" , "data" , "data" , "data" , "data"] ,
scatterpoints = 1 , bbox_to_anchor=(40,130) , loc='upper left' )
plt.xticks([0 , 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100])
plt.yticks([ 1 , 30 , 60 , 90 , 120 ])
plt.grid(True)
print('plots created')
plt.show()
python matplotlib legend
3
The coordinates are given in axes coordinates which range from 0 to 1 as mentioned in the documentation: matplotlib.org/1.3.0/api/… There it also says that "The legend location can be specified in other coordinate, by using the bbox_transform keyword."
– David Zwicker
Oct 21 '14 at 21:38
Please simplify this code to the minimun required to show the issue.
– tacaswell
Oct 22 '14 at 3:32
add a comment |
I'm trying to put my matplotlib legend in a custom location using the bbox_to_anchor command. I want the upper left hand corner to be at the coordinates x=40 and y=130 so my python code looks as follows:
plt.legend([o,h,n,k,l,a],
["data" , "data" , "data" , "data" , "data" , "data"] ,
scatterpoints = 1 , bbox_to_anchor=(40,130) , loc='upper left' )
When I actually go to plot though the legend is simply gone. If I remove the bbox_to_anchor option then the legend is on the graph. The rest of my code is as follows:
import matplotlib.pyplot as plt
from matplotlib import rc
#eu-152 data
eu152_10_1 = ([(10,1)])
#eu152_10_3 = ([(10,3)])
#eu152_10_30 = ([(10,30)])
eu152_10_60 = ([(10,60)])
eu152_10_120 = ([(10,120)])
#eu152_30_1 = ([(30,1)])
eu152_30_3 = ([(30,3)])
eu152_30_30 = ([(30,30)])
eu152_30_60 = ([(30,60)])
eu152_30_120 = ([(30,120)])
eu152_90_1 = ([(90,1)])
eu152_90_3 = ([(90,3)])
eu152_90_30 = ([(90,30)])
eu152_90_60 = ([(90,60)])
eu152_90_120 = ([(90,120)])
plt.rcParams['legend.loc'] = 'best'
plt.figure(1)
#plt.autoscale(enable=True, axis='y', tight=None)
#plt.autoscale(enable=True, axis='x', tight=None)
#extra = Rectangle((0, 0), 1, 1, fc="w", fill=False, edgecolor='none', linewidth=0)
a = plt.scatter(*zip(*eu152_10_1) , color='k', marker='o' , s=300)
#b = plt.scatter(*zip(*eu152_10_3) , color='r', marker='D' , s=300)
#c = plt.scatter(*zip(*eu152_10_30) , color='r', marker='D' , s=300)
d = plt.scatter(*zip(*eu152_10_60) , color='m', marker='o' , s=300)
e = plt.scatter(*zip(*eu152_10_120) , color='r', marker='o' , s=300)
#f = plt.scatter(*zip(*eu152_30_1) , color='r', marker='D' , s=300)
g = plt.scatter(*zip(*eu152_30_3) , color='b', marker='o', s=300)
h = plt.scatter(*zip(*eu152_30_30) , color='g', marker='o', s=300)
i = plt.scatter(*zip(*eu152_30_60) , color='k', marker='o', s=300)
j = plt.scatter(*zip(*eu152_30_120) , color='m', marker='o', s=300)
k = plt.scatter(*zip(*eu152_90_1) , color='c', marker='o', s=300)
l = plt.scatter(*zip(*eu152_90_3) , color='m', marker='o', s=300)
m = plt.scatter(*zip(*eu152_90_30) , color='c', marker='o', s=300)
n = plt.scatter(*zip(*eu152_90_60) , color='b', marker='o', s=300)
o = plt.scatter(*zip(*eu152_90_120) , color='r', marker='o', s=300)
rc('text', usetex=True)
plt.ylim(0, 150)
plt.xlim(0, 100)
plt.xlabel('Power Level (kW)', fontsize=26 , labelpad = 20)
plt.ylabel('Irradiation Time (min.)' , fontsize=26)
plt.tick_params(axis='x' , labelsize=20)
plt.tick_params(axis='y' , labelsize=20)
plt.legend([o,h,n,k,l,a],
["data" , "data" , "data" , "data" , "data" , "data"] ,
scatterpoints = 1 , bbox_to_anchor=(40,130) , loc='upper left' )
plt.xticks([0 , 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100])
plt.yticks([ 1 , 30 , 60 , 90 , 120 ])
plt.grid(True)
print('plots created')
plt.show()
python matplotlib legend
I'm trying to put my matplotlib legend in a custom location using the bbox_to_anchor command. I want the upper left hand corner to be at the coordinates x=40 and y=130 so my python code looks as follows:
plt.legend([o,h,n,k,l,a],
["data" , "data" , "data" , "data" , "data" , "data"] ,
scatterpoints = 1 , bbox_to_anchor=(40,130) , loc='upper left' )
When I actually go to plot though the legend is simply gone. If I remove the bbox_to_anchor option then the legend is on the graph. The rest of my code is as follows:
import matplotlib.pyplot as plt
from matplotlib import rc
#eu-152 data
eu152_10_1 = ([(10,1)])
#eu152_10_3 = ([(10,3)])
#eu152_10_30 = ([(10,30)])
eu152_10_60 = ([(10,60)])
eu152_10_120 = ([(10,120)])
#eu152_30_1 = ([(30,1)])
eu152_30_3 = ([(30,3)])
eu152_30_30 = ([(30,30)])
eu152_30_60 = ([(30,60)])
eu152_30_120 = ([(30,120)])
eu152_90_1 = ([(90,1)])
eu152_90_3 = ([(90,3)])
eu152_90_30 = ([(90,30)])
eu152_90_60 = ([(90,60)])
eu152_90_120 = ([(90,120)])
plt.rcParams['legend.loc'] = 'best'
plt.figure(1)
#plt.autoscale(enable=True, axis='y', tight=None)
#plt.autoscale(enable=True, axis='x', tight=None)
#extra = Rectangle((0, 0), 1, 1, fc="w", fill=False, edgecolor='none', linewidth=0)
a = plt.scatter(*zip(*eu152_10_1) , color='k', marker='o' , s=300)
#b = plt.scatter(*zip(*eu152_10_3) , color='r', marker='D' , s=300)
#c = plt.scatter(*zip(*eu152_10_30) , color='r', marker='D' , s=300)
d = plt.scatter(*zip(*eu152_10_60) , color='m', marker='o' , s=300)
e = plt.scatter(*zip(*eu152_10_120) , color='r', marker='o' , s=300)
#f = plt.scatter(*zip(*eu152_30_1) , color='r', marker='D' , s=300)
g = plt.scatter(*zip(*eu152_30_3) , color='b', marker='o', s=300)
h = plt.scatter(*zip(*eu152_30_30) , color='g', marker='o', s=300)
i = plt.scatter(*zip(*eu152_30_60) , color='k', marker='o', s=300)
j = plt.scatter(*zip(*eu152_30_120) , color='m', marker='o', s=300)
k = plt.scatter(*zip(*eu152_90_1) , color='c', marker='o', s=300)
l = plt.scatter(*zip(*eu152_90_3) , color='m', marker='o', s=300)
m = plt.scatter(*zip(*eu152_90_30) , color='c', marker='o', s=300)
n = plt.scatter(*zip(*eu152_90_60) , color='b', marker='o', s=300)
o = plt.scatter(*zip(*eu152_90_120) , color='r', marker='o', s=300)
rc('text', usetex=True)
plt.ylim(0, 150)
plt.xlim(0, 100)
plt.xlabel('Power Level (kW)', fontsize=26 , labelpad = 20)
plt.ylabel('Irradiation Time (min.)' , fontsize=26)
plt.tick_params(axis='x' , labelsize=20)
plt.tick_params(axis='y' , labelsize=20)
plt.legend([o,h,n,k,l,a],
["data" , "data" , "data" , "data" , "data" , "data"] ,
scatterpoints = 1 , bbox_to_anchor=(40,130) , loc='upper left' )
plt.xticks([0 , 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100])
plt.yticks([ 1 , 30 , 60 , 90 , 120 ])
plt.grid(True)
print('plots created')
plt.show()
python matplotlib legend
python matplotlib legend
edited Nov 13 '18 at 2:52
Cœur
17.6k9104145
17.6k9104145
asked Oct 21 '14 at 21:24
Bogdan JaniszewskiBogdan Janiszewski
6332615
6332615
3
The coordinates are given in axes coordinates which range from 0 to 1 as mentioned in the documentation: matplotlib.org/1.3.0/api/… There it also says that "The legend location can be specified in other coordinate, by using the bbox_transform keyword."
– David Zwicker
Oct 21 '14 at 21:38
Please simplify this code to the minimun required to show the issue.
– tacaswell
Oct 22 '14 at 3:32
add a comment |
3
The coordinates are given in axes coordinates which range from 0 to 1 as mentioned in the documentation: matplotlib.org/1.3.0/api/… There it also says that "The legend location can be specified in other coordinate, by using the bbox_transform keyword."
– David Zwicker
Oct 21 '14 at 21:38
Please simplify this code to the minimun required to show the issue.
– tacaswell
Oct 22 '14 at 3:32
3
3
The coordinates are given in axes coordinates which range from 0 to 1 as mentioned in the documentation: matplotlib.org/1.3.0/api/… There it also says that "The legend location can be specified in other coordinate, by using the bbox_transform keyword."
– David Zwicker
Oct 21 '14 at 21:38
The coordinates are given in axes coordinates which range from 0 to 1 as mentioned in the documentation: matplotlib.org/1.3.0/api/… There it also says that "The legend location can be specified in other coordinate, by using the bbox_transform keyword."
– David Zwicker
Oct 21 '14 at 21:38
Please simplify this code to the minimun required to show the issue.
– tacaswell
Oct 22 '14 at 3:32
Please simplify this code to the minimun required to show the issue.
– tacaswell
Oct 22 '14 at 3:32
add a comment |
0
active
oldest
votes
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%2f26496617%2fmatplotlib-leged-disappears-using-bbox-to-anchor%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f26496617%2fmatplotlib-leged-disappears-using-bbox-to-anchor%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
3
The coordinates are given in axes coordinates which range from 0 to 1 as mentioned in the documentation: matplotlib.org/1.3.0/api/… There it also says that "The legend location can be specified in other coordinate, by using the bbox_transform keyword."
– David Zwicker
Oct 21 '14 at 21:38
Please simplify this code to the minimun required to show the issue.
– tacaswell
Oct 22 '14 at 3:32