Dan*_*234 5 python matplotlib scatter-plot seaborn
plot我使用seaborn三列进行了分散['Category','Installs' and 'Gross Income'],并使用数据集中的类别列进行了色调图。然而,在图例中,除了我想要出现的类别列之外,最后还有一个大自以为是的内容,显示了散点图中使用的列之一,安装。我想删除这个元素,但是通过搜索其他问题和文档seaborn,matplotlib我不知道如何继续。
这是我正在使用的代码片段:
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()
Run Code Online (Sandbox Code Playgroud)
实际上,这不是污迹,而是色调图的大小图例。由于气泡尺寸(100, 5000)相对于数据而言太大,因此它们在图例中的该空间中重叠,从而产生“涂抹”效果。默认图例将颜色和尺寸图例组合在一起。
但读者可能需要了解气泡的安装尺寸范围,而不是按照您的意愿删除尺寸标记。因此,请考虑将一个图例分成两个图例,并使用边框垫和道具大小来适应气泡和标签。
数据 (种子、随机数据)
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'])
Run Code Online (Sandbox Code Playgroud)
图形
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()
Run Code Online (Sandbox Code Playgroud)
输出
甚至在绘图之前考虑一下seaborn的主题sns.set():