Python 在Matplotlib中移动图例

Python 在Matplotlib中移动图例,python,matplotlib,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([

我试图用一个相当大的键来绘制一个图:

    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")
我试图编辑这个键,使它位于整个图表的右侧,如清单所示

我正在尝试添加此代码

#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
文件“”,第21行
“运输和材料搬运”、“视觉和表演艺术”]plt.图例(bbox_to_anchor=(1.05,1),loc=2,borderaxespad=0)
^
SyntaxError:无效语法

错误源于labellist和新命令之间缺少换行符

但是,如果希望有一个图例,则不应尝试添加两个图例

因此,在单个命令中,使用

ax.legend(labels=[..list of labels..], bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

错误源于labellist和新命令之间缺少换行符

但是,如果希望有一个图例,则不应尝试添加两个图例

因此,在单个命令中,使用

ax.legend(labels=[..list of labels..], bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

在报告错误时,请在您的第一个图例创建函数中添加
loc=2
参数(不要添加两次图例)

参考帖子在您的第一个图例创建函数中添加
loc=2
参数(不要添加两次图例)

,请确保包含错误及其回溯。抱歉!已经编辑过了!报告错误时,请确保包含错误及其回溯。对此,我深表歉意!已经编辑过了!成功了!非常感谢@ImportanceOfBeingErnest!!成功了!非常感谢@ImportanceOfBeingErnest
loc=2
正确地将图例放置在带有
bbox\u to\u锚定的绘图右侧,如此处所需。有关详细信息,请参阅
loc=5
会将其再次放置在绘图中。@ImportantanceOfBeingernest,谢谢,我已经更新了答案
loc=2
,因为图例位于绘图的右侧,带有
bbox\u to\u锚定
。有关详细信息,请参阅
loc=5
会将其再次放置在绘图中。@ImportanceOfBeingErnest,谢谢,我已经更新了答案