0

I'm trying to make a graph with a pretty massive key:

    my_plot = degrees.plot(kind='bar',stacked=True,title="% of Degrees by Field",fontsize=20,figsize=(24, 16))
my_plot.set_xlabel("Institution", fontsize=20)
my_plot.set_ylabel("% of Degrees by Field", fontsize=20)
my_plot.legend(["Agriculture, Agriculture Operations, and Related Sciences", "Architecture and Related Services", 
                "Area, Ethnic, Cultural, Gender, and Group Studies", "Biological and Biomedical Sciences", 
                "Business, Management, Marketing, and Related Support Services", 
                "Communication, Journalism, and Related Programs", 
                "Communications Technologies/Technicians and Support Services", 
                "Computer and Information Sciences and Support Services", "Construction Trades", "Education", 
                "Engineering Technologies and Engineering-Related Fields", "Engineering", 
                "English Language and Literature/Letters", "Family and Consumer Sciences/Human Sciences", 
                "Foreign Languages, Literatures, and Linguistics", "Health Professions and Related Programs", "History", 
                "Homeland Security, Law Enforcement, Firefighting and Related Protective Services", 
                "Legal Professions and Studies", "Liberal Arts and Sciences, General Studies and Humanities", 
                "Library Science", "Mathematics and Statistics", "Mechanic and Repair Technologies/Technicians", 
                "Military Technologies and Applied Sciences", "Multi/Interdisciplinary Studies", 
                "Natural Resources and Conservation", "Parks, Recreation, Leisure, and Fitness Studies", 
                "Personal and Culinary Services", "Philosophy and Religious Studies", "Physical Sciences", 
                "Precision Production", "Psychology", "Public Administration and Social Service Professions", 
                "Science Technologies/Technicians", "Social Sciences", "Theology and Religious Vocations", 
                "Transportation and Materials Moving", "Visual and Performing Arts"])
plt.savefig("Degrees by Field.png")

and I'm trying to edit the key so that it's on the right side of the entire graph as listed here.

I'm trying to add this code

#Place a legend to the right of this smaller subplot.
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

and I get errors when I add that line to my lengthy code. Can someone tell me where to put this so my legend is on the right?

THANK YOU!

Edited to add

Ran the code with location specific language:

my_plot = degrees.plot(kind='bar',stacked=True,title="% of Degrees by Field",fontsize=20,figsize=(24, 16))
my_plot.set_xlabel("Institution", fontsize=20)
my_plot.set_ylabel("% of Degrees by Field", fontsize=20)
my_plot.legend(["Agriculture, Agriculture Operations, and Related Sciences", "Architecture and Related Services", 
                "Area, Ethnic, Cultural, Gender, and Group Studies", "Biological and Biomedical Sciences", 
                "Business, Management, Marketing, and Related Support Services", 
                "Communication, Journalism, and Related Programs", 
                "Communications Technologies/Technicians and Support Services", 
                "Computer and Information Sciences and Support Services", "Construction Trades", "Education", 
                "Engineering Technologies and Engineering-Related Fields", "Engineering", 
                "English Language and Literature/Letters", "Family and Consumer Sciences/Human Sciences", 
                "Foreign Languages, Literatures, and Linguistics", "Health Professions and Related Programs", "History", 
                "Homeland Security, Law Enforcement, Firefighting and Related Protective Services", 
                "Legal Professions and Studies", "Liberal Arts and Sciences, General Studies and Humanities", 
                "Library Science", "Mathematics and Statistics", "Mechanic and Repair Technologies/Technicians", 
                "Military Technologies and Applied Sciences", "Multi/Interdisciplinary Studies", 
                "Natural Resources and Conservation", "Parks, Recreation, Leisure, and Fitness Studies", 
                "Personal and Culinary Services", "Philosophy and Religious Studies", "Physical Sciences", 
                "Precision Production", "Psychology", "Public Administration and Social Service Professions", 
                "Science Technologies/Technicians", "Social Sciences", "Theology and Religious Vocations", 
                "Transportation and Materials Moving", "Visual and Performing Arts"]plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.))
plt.savefig("Degrees by Field.png")

And then got this warning/error:

  File "<ipython-input-101-9066269a61aa>", line 21
    "Transportation and Materials Moving", "Visual and Performing Arts"]plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.))
                                                                          ^
SyntaxError: invalid syntax
tangerine7199
  • 443
  • 2
  • 8
  • 24

2 Answers2

3

The error comes from the missing linebreak between the labellist and the new command.

However, if you want to have one single legend, you should not try to add two legends.

So within a single command, use

ax.legend(labels=[..list of labels..], bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
3

refering to the matplotlib legend location numbers post just add loc=2 argument in your first legend creation function (do not add the legend twice)

Community
  • 1
  • 1
  • 1
    `loc=2` is correct for having the legend at the right side of the plot with `bbox_to_anchor`, as desired here. See [this answer](http://stackoverflow.com/a/43439132/4124317) for details. `loc=5` would place it such that is inside the plot again. – ImportanceOfBeingErnest Apr 23 '17 at 16:30
  • @ImportanceOfBeingErnest, thanks, i have updated the answer – John Johnson Apr 23 '17 at 16:36