0

I have the following data and I would like to generate a graph with the legend of all the "States/UTs" shifted to the right or outside of the box that contains the graph. There are no errors with the code and the output is the graph that I am providing here. The graph is what I am asking about, see how the legend in the graph takes up part of the graph and it is not showing all the "States/UTs" because it is not centered correctly.

Data:

                                          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

code:

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

Graph:

enter image description here

Update:

enter image description here

The Singularity
  • 2,428
  • 3
  • 19
  • 48
Daniel
  • 61
  • 8
  • From my understanding, inserting this next piece of code should do it: plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.). It doesn't though – Daniel Sep 26 '21 at 01:45
  • I think if you try it for yourself, it will not work. – Daniel Sep 26 '21 at 02:11
  • I think we can change it to a scatter plot graph and add the number of columns specification to the legend specification in the comments. `sns.acatterplot(...);plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0., ncol=3)` – r-beginners Sep 26 '21 at 02:37
  • `plt.subplots(figsize=(20,20))` `sns.scatterplot(y='Discharged', x='Total_Cases', data=df, hue='State_UTs')` `plt.legend(loc='center right', bbox_to_anchor=(1.0, 1.05), ncol=8)` `plt.xticks(rotation='vertical')` `plt.show()` – Joe Ferndz Sep 02 '23 at 04:03

1 Answers1

2

You can probably try something like that:

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)

Demo:

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)

JointPlot

Corralien
  • 109,409
  • 8
  • 28
  • 52