在Matplotlib中移动图例

时间:2017-04-23 16:19:28

标签: python matplotlib

我试图用非常庞大的密钥制作图表:

    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")

我尝试编辑密钥,使其位于整个图表的右侧,列为here

我试图添加此代码

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

当我将该行添加到冗长的代码中时出现错误。有人可以告诉我在哪里放这个,所以我的传奇是在右边?

谢谢!

已编辑添加

使用位置特定语言运行代码:

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")

然后得到了这个警告/错误:

  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

2 个答案:

答案 0 :(得分:3)

错误来自labellist和new命令之间缺少的换行符。

但是,如果您想要一个单一的图例,则不应尝试添加两个图例。

所以在一个命令中,使用

git update-index --no-assume-unchanged Web.config

答案 1 :(得分:3)

引用matplotlib legend location numbers帖子只需在第一个图例创建功能中添加loc=2参数(不要添加图例两次)