Seaborn legend italicize legend titles

6 days ago 11
ARTICLE AD BOX

I'm working on a scatterplot done with Seaborn; now, I'm pretty happy with the result achieved so far but there is one thing I wish to fix.

In my legend I have two type of entries: sample and genome, which are based on all_pca dataframe column type. What I would like is either for the legend titles label and type to be printed in italic or, alternatively, to show the dots associated with the genome as + symbols in the legend.

As I said I don't have much experience with this type of code and I'm unsure about the best option to pursue. Any help is much appreciated. Below, the code I'm using:

import json import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt fig, ax = plt.subplots(figsize=(20, 20)) sns.set_theme(style="white", palette=None, font_scale=2) sns.set_style("ticks") for spine in ax.spines.values(): spine.set_linewidth(4) palette = {"AJ": "#5170d7", "AFR": "#a8ff04", "EAS": "#ff7855", "AMR": "#fed0fc", "EUR": "#1805db", "RPE1": "#1805db", "SAS": "#ff9408", "MENA": "#9d0759", "FIN": "#0485d1", "H9": "#000011", "HG002": "#5170d7", "YAO": "#ff7855", "CAS": "#f4320c", "AAC": "#019529"} markers = ['o', 'P'] sns.scatterplot( x="PC1", y="PC2", hue="label", data=all_pca, palette=palette, s=200, alpha=0.75, edgecolor="white", linewidth=1.5, ax=ax, markers=markers, style="type", ) handles, labels = ax.get_legend_handles_labels() handles = [handles[0], handles[2], handles[6], handles[8], handles[1], handles[5], handles[10], handles[7], handles[9], handles[3], handles[4], handles[12], handles[14], handles[13], handles[11], handles[15], handles[16], handles[17]] labels = [labels[0], labels[2], labels[6], labels[8], labels[1], labels[5], labels[10], labels[7], labels[9], labels[3], labels[4], labels[12], labels[14], labels[13], labels[11], labels[15], labels[16], labels[17]] legend = ax.legend(handles, labels, loc="lower left", bbox_to_anchor=(0.00001, 0.00001), ncols=3, edgecolor="black") #fontsize=14, title_fontsize=16 legend.get_frame().set_linewidth(2); legend.get_frame().set_alpha(None); legend.get_frame().set_facecolor((1, 1, 1, .01)) ''' would print PCs explained variance but not running since no m3 is provided ''' #ax.set_xlabel(f"PC1 ({stds_pc1(m3):.2f}%)") #ax.set_ylabel(f"PC2 ({stds_pc2(m3):.2f}%)") plt.text(x=all_pca["PC1"].iloc[-1]-8.5, y=all_pca["PC2"].iloc[-1]+2, s="RPE1", fontsize=16, color="#5170d7", weight="bold", bbox=dict(facecolor="white", alpha=0)) plt.text(x=all_pca["PC1"].iloc[-2]-8.1, y=all_pca["PC2"].iloc[-2]+1.25, s="YAO", fontsize=16, color="#ff7855", weight="bold", bbox=dict(facecolor="white", alpha=0)) plt.text(x=all_pca["PC1"].iloc[-3]-1.75, y=all_pca["PC2"].iloc[-3]+2, s="HG002", fontsize=16, color="#5170d7", weight="bold", bbox=dict(facecolor="white", alpha=0)) plt.text(x=all_pca["PC1"].iloc[-4]-4.75, y=all_pca["PC2"].iloc[-4]+1.75, s="H9", fontsize=16, color="#000011", weight="bold", bbox=dict(facecolor="white", alpha=0))

and a MWE dataset to run on (first 44 lines of all_pca):

{'IID': ['HG02089', 'NA10842', 'HG01349', 'MU2_RP2E6', 'HG03310', 'HG03133', 'HG03642', 'HG01611', 'HG00185', 'RP2B4', 'NA20832', 'HGDP00330', 'HG03279', 'AP8_2_B6', 'HG01456', 'HG02140', 'HG03484', 'HGDP00106', 'NA12045', 'HG03725', 'HG03740', 'HG01169', 'NA19730', 'NA21086', 'HGDP00088', 'NA19727', 'AP9F3', 'HGDP00021', 'HGDP01267', 'NA21115', 'HG03558', 'HG03369', 'HG03926', 'NA12413', 'NA19258', 'HG00423', 'H9', 'HG002', 'YAO', 'RPE1', 'H9', 'HG002', 'YAO', 'RPE1'], 'PC1': [21.410344995829423, 74.88968958109967, 53.33456196998314, 76.65380204348175, -133.17527446955017, -137.15127831049395, 18.008226762219095, 71.29808021296589, 62.889620652931605, 74.832120953148, 73.01665522968568, 51.757913860616675, -132.55496450332427, 75.92584486294695, 35.34037714333217, -20.297391493216097, -135.82297830949702, 22.042065165574783, 73.60190954888027, 25.23831198996469, 22.786849677066304, 44.96402670984215, 3.036357579592402, 29.72163153170352, 52.324474598469465, 32.48837574241794, 72.03700365698732, 43.84897583612322, 28.64368551764103, 31.281576426914164, -134.27618687070859, -135.70465558182136, 19.529003968620565, 74.73455724920964, -139.73952992223238, -20.30698393322826, 67.1049113501295, 80.8309668584159, -31.60157314776359, 76.5075200579214, 67.1049113501295, 80.8309668584159, -31.60157314776359, 76.5075200579214], 'PC2': [-48.69118771436431, 43.054745388303594, 13.4236796715702, 51.43966661126227, 50.930980769395575, 52.01587959981691, -33.28652840490086, 50.5817328856708, 26.709482469420763, 55.53268867554156, 48.92685419925832, 2.62783887797197, 54.30416902258931, 49.88335394587888, -7.7214049665526305, -141.89080367222735, 50.58090164186229, -50.80372303788835, 46.886427878209, -26.418191463324415, -28.594987622204396, 38.01463722747548, -89.19610037366903, -19.09462805300637, 16.491057668961915, -22.57286359904835, 46.320300545995636, 19.464854712593773, 49.60884208546487, -18.829706819716435, 50.80992910611094, 52.04099747609546, -42.54192813188355, 46.614354911242415, 54.01237550341454, -149.86297749660147, 58.875630859406705, 63.36597313882753, -164.87754941041462, 60.58511663148425, 58.875630859406705, 63.36597313882753, -164.87754941041462, 60.58511663148425], 'label': ['AMR', 'EUR', 'AMR', 'AJ', 'AFR', 'AFR', 'SAS', 'EUR', 'FIN', 'AJ', 'EUR', 'CAS', 'AFR', 'AJ', 'AMR', 'EAS', 'AFR', 'CAS', 'EUR', 'SAS', 'SAS', 'AMR', 'AMR', 'SAS', 'CAS', 'AMR', 'AJ', 'CAS', 'MENA', 'SAS', 'AFR', 'AFR', 'SAS', 'EUR', 'AFR', 'EAS', 'H9', 'HG002', 'YAO', 'RPE1', 'H9', 'HG002', 'YAO', 'RPE1'], 'type': ['sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'sample', 'genome', 'genome', 'genome', 'genome']}

and what my full output looks like:

pca

Read Entire Article