我正在用 matplotlib 和 seaborn 繪制散點圖。這是我的輸出。

正如您在圖例右側看到的那樣,有許多具有相同顏色的標簽,我試圖擦除除一個之外的所有灰色。這是我的代碼:
background = '#eeeeee'
high_c = '#5a83aa'
low_c = '#e09a67'
neutral_c = '#fff8c2'
palette = {'Western Europe':high_c,
'North America and ANZ':'#cccccc',
'Middle East and North Africa':'#cccccc',
'Latin America and Caribbean':'#cccccc',
'Central and Eastern Europe':'#cccccc',
'East Asia':'#cccccc',
'Southeast Asia':'#cccccc',
'Commonwealth of Independent States':'#cccccc',
'Sub-Saharan Africa':low_c,
'South Asia':'#cccccc'
}
fig, ax = plt.subplots(1,1, figsize=(10, 5),dpi=120)
fig.patch.set_facecolor(background) # figure background color
ax.set_facecolor(background)
# plots
sns.scatterplot(data=df21, x='Healthy life expectancy', y='Ladder score',
hue=df21['Regional indicator'],
palette=palette, alpha=0.95,ec='black',
size=df21["Logged GDP per capita"]*1000, legend=True, sizes=(5, 500))
# Axes
ax.set_xlabel("Healthy Life Expectancy",fontfamily='sans serif',loc='left',color='gray')
ax.set_ylabel("Happiness Index Score",fontfamily='sans serif',loc='top',color='gray')
ax.tick_params(axis = 'both', which = 'major', labelsize = 10)
for s in ["top","right","left"]:
ax.spines[s].set_visible(False)
# Annotations
ax.text(45,9.2,'Happiness Score, Life Expectancy, and GDP per Capita',
fontfamily='sans serif',fontweight='normal',fontsize=17,weight='bold',color='#323232')
# Legend
L = ax.legend(frameon=False,loc="upper center", bbox_to_anchor=(1.25, 0.8), ncol= 1)
plt.setp(L.texts, family='sans serif')
L.get_frame().set_facecolor('none')
L.get_texts()[2].set_text('Others')
L.get_texts()[3].set_text('')
L.get_texts()[4].set_text('')
L.get_texts()[5].set_text('')
ax.tick_params(axis='both', which='both',left=False, bottom=False,labelbottom=True)
plt.show()
我想如何更改標簽上的文本:L.get_texts()[2].set_text('Others'),但找不到如何洗掉它,我想要一個只有“西歐”的圖例, “撒哈拉以南非洲”和“其他”。
我認為這可能與“handler_map”有關,但我不知道它是如何作業的。
uj5u.com熱心網友回復:
圖例會自動忽略所有以 開頭的標簽'_'。一個想法是將所有灰色區域重命名為以'_'.
您的代碼缺少可重現的資料,但更改可能如下所示:
df21['Regional indicator']
new_name = {month: '_' month if color == '#cccccc' else month for month, color in palette.items()}
new_name['North America and ANZ'] = 'other'
df21['Regional indicator'] = df21['Regional indicator'].map(new_names)
new_palette = {new_name[month]: color for month, color in palette.items()}
sns.scatterplot(data=df21, x='Healthy life expectancy', y='Ladder score',
hue='Regional indicator',
palette=new_palette, alpha=0.95, ec='black',
size=df21["Logged GDP per capita"] * 1000, legend=True, sizes=(5, 500))
這是一個使用 seaborn 的航班資料集的獨立示例。
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
flights = sns.load_dataset('flights')
palette = {'Jan': 'crimson', 'Feb': 'limegreen', 'Mar': 'blue', 'Apr': 'gold', 'May': 'purple', 'Jun': '#cccccc',
'Jul': '#cccccc', 'Aug': '#cccccc', 'Sep': '#cccccc', 'Oct': '#cccccc', 'Nov': '#cccccc', 'Dec': '#cccccc'}
new_name = {month: '_' month if color == '#cccccc' else month for month, color in palette.items()}
new_name['Jun'] = 'other'
flights['month'] = flights['month'].map(new_names)
new_palette = {new_name[month]: color for month, color in palette.items()}
sns.scatterplot(data=flights, x='year', y='passengers', hue='month', palette=new_palette)
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/412606.html
標籤:
