【问题标题】:Is there a way to move the seaborn graph's legend outside of the graph or to the right? [duplicate]有没有办法将 seaborn 图表的图例移到图表之外或向右? [复制]
【发布时间】:2021-11-18 15:11:20
【问题描述】:

我有以下数据,我想生成一个图表,其中所有“状态/UT”的图例移到包含图表的框的右侧或外部。代码没有错误,输出是我在此处提供的图表。图表就是我要问的,看看图表中的图例如何占据图表的一部分,它没有显示所有的“状态/UT”,因为它没有正确居中。

数据:

                                          Discharged  Total Cases
State/UTs                                                        
Andaman and Nicobar                             7437         7572
Andhra Pradesh                               1993589      2022064
Arunachal Pradesh                              52507        53408
Assam                                         580491       592616
Bihar                                         716048       725759
Chandigarh                                     64273        65122
Chhattisgarh                                  990757      1004668
Dadra and Nagar Haveli and Daman and Diu       10659        10665
Delhi                                        1412542      1437991
Goa                                           170391       174486
Gujarat                                       815275       825509
Haryana                                       760271       770573
Himachal Pradesh                              209420       214732
Jammu and Kashmir                             320337       326033
Jharkhand                                     342716       347975
Karnataka                                    2901299      2956137
Kerala                                       3966557      4227526
Ladakh                                         20327        20588
Lakshadweep                                    10288        10348
Madhya Pradesh                                781629       792270
Maharashtra                                  6300755      6489800
Manipur                                       110602       115584
Meghalaya                                      73711        77144
Mizoram                                        54056        65696
Nagaland                                       29045        30388
Odisha                                        997790      1012167
Puducherry                                    121452       124184
Punjab                                        584079       600849
Rajasthan                                     945097       954137
Sikkim                                         28968        30256
Tamil Nadu                                   2572942      2624234
Telengana                                     650453       659844
Tripura                                        81866        83360
Uttar Pradesh                                1686369      1709457
Uttarakhand                                   335358       343125
West Bengal                                  1525581      1552576

代码:

data = Table.read_table('IndiaStatus.csv')#.drop('Discharged', 'Discharge Ratio (%)','Total Cases','Active','Deaths')
data2 = data.to_df()

data3 = data2.set_index("State/UTs")

print(data3[["Discharged","Total Cases"]])

sns.jointplot(x = "Total Cases", y = "Death Ratio (%)" , data = data, hue = "State/UTs")

图表:

更新:

【问题讨论】:

  • 据我了解,插入下一段代码应该可以做到:plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)。虽然没有
  • 我想如果你自己尝试一下,它是行不通的。
  • 我认为我们可以将其更改为散点图,并将列数规范添加到 cmets 中的图例规范中。 sns.acatterplot(...);plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0., ncol=3)

标签: python pandas dataframe seaborn


【解决方案1】:

你也许可以尝试这样的事情:

g = sns.jointplot(x='Total Cases', y='Death Ratio (%)',
                  data=data, hue='State/UTs')

plt.subplots_adjust(right=0.75)

g.ax_joint.legend(bbox_to_anchor=(1.25,1), loc='upper left', borderaxespad=0)

演示:

import matplotlib.pyplot as plt
import seaborn as sns

penguins = sns.load_dataset('penguins')
g = sns.jointplot(x='bill_length_mm', y='bill_depth_mm',
                  data=penguins, hue='species')
plt.subplots_adjust(right=0.75)
g.ax_joint.legend(bbox_to_anchor=(1.25,1), loc='upper left', borderaxespad=0)

【讨论】:

    猜你喜欢
    • 2019-05-13
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 2020-02-24
    • 1970-01-01
    • 2019-10-29
    相关资源
    最近更新 更多