在Matplotlib中移动图例

2024-06-17 12:08:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用一个很大的键做一个图:

    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所示。在

我在试着添加这个代码

^{pr2}$

当我得到冗长的代码时,我会加上我的错误。有人能告诉我把这个放在哪里,这样我的传奇就在右边吗?在

谢谢你!在

编辑后添加

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

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

Tags: andfieldsupportbyplotmydegreesrelated
2条回答

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

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

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

所以在一个命令中,使用

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

相关问题 更多 >