0

I am trying to make a seaborn scatterplot with legend entries that combine color and shape. Rather than extract color and assign that to the shape entries like here, I want to extract the shape entries and assign that these shapes to the color entries. Is there anyway to do this?

I am able to extract the color and assign them to shapes, but that makes the plot appear confusing (a lot of blue in it), so I'd rather switch these variables

#subscale_dict = {'Deccan Traps': 'CFB', 'Other fissure': 'Fissure', 'Experimental': 'Experimental', 'Stratovolcano': 'Stratovolcano',
#                'Columbia river basalts': 'CFB', 'Iceland': 'Fissure', 'Shield': 'Shield', 'Ferrar': 'CFB', 'Emeishan': 'CFB', 
#                'EquiMAP': 'CFB', 'Moon': 'Moon'}
subscale_dict = passframe.set_index("Shorthand")["Style"].to_dict()
passframe['Subscale'] = passframe['Shorthand'].map(subscale_dict)
passframe['Subscale'] = pd.Categorical(passframe['Subscale'])

print(subscale_dict)

passframe
cust = {'axes.labelsize': 24, 'xtick.labelsize':20, 'ytick.labelsize':20, 'legend.fontsize':20, 'figure.facecolor': 'white'}
sns.set(rc={'figure.figsize':(32,18)})
sns.set_theme(style = 'ticks', font = 'Calibri', rc = cust)
sns.set_context('poster')
colour = 'colorblind'


fig1 = sns.scatterplot(data = passframe, x = 'Slope1', y = 'Intercept1', style = 'Shorthand',
                   hue = 'Subscale',s = 400, palette = colour, legend = 'full')#, height = 12, aspect = 16/9)

handles, labels = fig1.get_legend_handles_labels()
index_item_title = labels.index('Shorthand')

color_dict = {label: handle.get_facecolor()
              for handle, label in zip(handles[1:index_item_title], labels[1:index_item_title])}

print(color_dict)
# loop through the items, assign color via the subscale of the item idem
for handle, label in zip(handles[index_item_title + 1:], labels[index_item_title + 1:]):
    handle.set_color(color_dict[subscale_dict[label]])
    
fig1.legend(handles[index_item_title + 1:], labels[index_item_title + 1:], title='Data',
          loc = 'best', markerscale = 2, labelspacing = 1.2, fontsize = 24, title_fontsize = 32)


fig1.set_xlabel('$Slope (cm^{-1})$')
fig1.set_ylabel('$Intercept (cm^{-4})$')
plt.xlim([min(passframe['Slope1']), 1500])
plt.ylim([min(passframe['Intercept1']-2), max(passframe['Intercept2'])+2])

I want to enter hue= 'Shorthand' and style = 'Subscale' so that I can extract the shapes and assign them to the color legend entries and combine them that way.

This is what I have achieved

3
  • Your code above is incomplete... name 'subscale_dict' is not defined
    – Redox
    Commented Jun 21, 2023 at 2:15
  • Sorry for the delay, but this is fixed now. Thanks! Commented Jun 29, 2023 at 0:39
  • Style is not defined
    – Redox
    Commented Jun 29, 2023 at 13:23

0