如何从散点图的图例中删除属性

How Do I Remove an Attribute from the Legend of a Scatter plot

我使用来自三列 seaborn 的散点图 plot ['Category','Installs' and 'Gross Income'] 和使用我数据集中类别列的色相图。然而在图例中,除了我想要出现的类别列之外,末尾有一个很大的自鸣得意的显示散点图中使用的列之一,Installs。我想删除这个元素,但是通过搜索其他问题和 seabornmatplotlib 的文档,我不知道如何继续。

这是我正在使用的代码片段:

fig, ax = pyplot.subplots(figsize=(12,6))

ax=sns.scatterplot( x="Installs", y="Gross Income", data=comp_income_inst, hue='Category', 
                   palette=sns.color_palette("cubehelix",len(comp_income_inst)), 
                   size='Installs', sizes=(100,5000), legend='brief', ax=ax) 

ax.set(xscale="log", yscale="log")
ax.set(ylabel="Average Income") 
ax.set_title("Distribution showing the Earnings of Apps in Various Categories\n", fontsize=18)
plt.rcParams["axes.labelsize"] = 15



# Move the legend to an empty part of the plot
plt.legend(loc='upper left', bbox_to_anchor=(-0.2, -0.06),fancybox=True, shadow=True, ncol=5)
#plt.legend(loc='upper left')

plt.show()

实际上,这不是污点,而是色调图的尺寸图例。因为气泡大小 (100, 5000) 相对于数据来说太大了,所以它们在图例中 space 重叠,产生了 "smudge" 效果。默认图例将颜色和尺寸图例组合在一起。

但读者可能需要知道 Installs 气泡大小的范围,而不是按照您的意图删除大小标记。因此,考虑将一个图例分成两个图例,并使用 borderpadprop size 来适应气泡和标签。

数据 (种子,随机数据)

categs = ['GAME', 'EDUCATION', 'FAMILY', 'WEATHER', 'ENTERTAINMENT', 'PHOTOGRAPHY', 'LIFESTYLE',
          'SPORTS', 'PRODUCTIVITY', 'COMMUNICATION', 'PERSONALIZATION', 'HEALTH_AND_FITNESS', 'FOOD_AND_DRINK', 'PARENTING',
          'MAPS_AND_NAVIGATION', 'TOOLS', 'VIDEO_PLAYERS', 'BUSINESS', 'AUTO_AND_VEHICLES', 'TRAVEL_AND_LOCAL',
          'FINANCE', 'MEDICAL', 'ART_AND_DESIGN', 'SHOPPING', 'NEWS_AND_MAGAZINES', 'SOCIAL', 'DATING', 'BOOKS_AND REFERENCES',
          'LIBRARIES_AND_DEMO', 'EVENTS']

np.random.seed(11222018)
comp_income_inst = pd.DataFrame({'Category': categs,
                                 'Installs': np.random.randint(100, 5000, 30),
                                 'Gross Income': np.random.uniform(0, 30, 30) * 100000
                                }, columns=['Category', 'Installs', 'Gross Income'])

图表

fig, ax = plt.subplots(figsize=(13,6))

ax = sns.scatterplot(x="Installs", y="Gross Income", data=comp_income_inst, hue='Category', 
                    palette=sns.color_palette("cubehelix",len(comp_income_inst)), 
                    size='Installs', sizes=(100, 5000), legend='brief', ax=ax) 

ax.set(xscale="log", yscale="log")
ax.set(ylabel="Average Income") 
ax.set_title("Distribution showing the Earnings of Apps in Various Categories\n", fontsize=20)
plt.rcParams["axes.labelsize"] = 15

# EXTRACT CURRENT HANDLES AND LABELS
h,l = ax.get_legend_handles_labels()

# COLOR LEGEND (FIRST 30 ITEMS)
col_lgd = plt.legend(h[:30], l[:30], loc='upper left', 
                     bbox_to_anchor=(-0.05, -0.50), fancybox=True, shadow=True, ncol=5)

# SIZE LEGEND (LAST 5 ITEMS)
size_lgd = plt.legend(h[-5:], l[-5:], loc='lower center', borderpad=1.6, prop={'size': 20},
                      bbox_to_anchor=(0.5,-0.45), fancybox=True, shadow=True, ncol=5)

# ADD FORMER (OVERWRITTEN BY LATTER)
plt.gca().add_artist(col_lgd)

plt.show()

输出

甚至在绘图前 sns.set() 考虑 seaborn 的主题: