首页 > 解决方案 > 对加权散点图使用颜色强度而不是气泡大小

问题描述

有什么方法可以使用颜色强度而不是气泡大小来绘制加权散点图?我已经在网上搜索了几个小时的解决方案,但仍然没有找到。我使用以下企鹅数据进行说明。

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

penguins_data="https://raw.githubusercontent.com/datavizpyr/data/master/palmer_penguin_species.tsv"
penguins_df = pd.read_csv(penguins_data, sep="\t")

sns.set_context("talk", font_scale=1.1)
plt.figure(figsize=(10,6))
sns.scatterplot(x="culmen_length_mm", 
                y="culmen_depth_mm",
                size="body_mass_g",
                sizes=(20,500),
                alpha=0.5,
                data=penguins_df)
# Put the legend out of the figure
plt.legend(bbox_to_anchor=(1.01, 1),borderaxespad=0)
# Put the legend out of the figure
#plt.legend(bbox_to_anchor=(1.01, 0.54),  borderaxespad=0.)
plt.xlabel("Culmen Length (mm)")
plt.ylabel("Culmen Depth (mm)")
plt.title("Bubble plot in Seaborn")
plt.tight_layout()
plt.savefig("Bubble_plot_size_range_Seaborn_scatterplot.png",
                    format='png',dpi=150)

气泡图最小的气泡对应最小的体重,最大的气泡对应最大的体重。但是,我需要加权散点图的颜色强度。例如,较深的颜色表示它出现的频率较高,而较浅的颜色表示它出现的频率较低。任何使用 Stata(首选)、Python 或 R 的建议都将受到高度赞赏。

我在 Stata 中找到了类似这样的东西但是我的数据结构完全不同,所以它不起作用。

标签: pythonrseabornstatascatter-plot

解决方案


您是否考虑过在数据框中为颜色创建一个新列,您可以在其中自行调整 Alpha 通道?

之后,您可能可以从这个问题中使用它作为颜色列作为标记的色调。


推荐阅读