【问题标题】:Use color intensities instead of bubble size for weighted scatter plot对加权散点图使用颜色强度而不是气泡大小
【发布时间】:2021-03-04 17:02:53
【问题描述】:

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

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中找到了类似one的东西,但是我的数据结构完全不同,所以它不起作用。

【问题讨论】:

  • “颜色较深表示出现频率较高,颜色较浅表示出现频率较低” 但是,您正在绘制测量变量,所以我不明白频率来自哪里。
  • @NickCox 也许我没有使用正确的数据来说明。我有 x 和 y 分类变量。在Stata中,通常我们可以使用“egen weight_xy = group(x y)”,然后“twoway scatter y x [weight = weight_xy]”来生成组标识符。但是,我想使用颜色强度而不是气泡大小来绘制加权散点图。既然您是活跃的 Stata 用户,您有什么建议吗?谢谢。
  • 要获得不同的颜色,您需要针对另一个 x 变量绘制多个 y 变量。您引用的线程及其链接中提到了使用separate 的方法。没有不同的方法。

标签: python r seaborn stata scatter-plot


【解决方案1】:

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

之后,您可能可以使用this question 将其用作颜色列作为标记的色调。

【讨论】:

  • 谢谢,但我认为色调仅用于将产生不同颜色点的变量分组。不幸的是,这与体重无关。
  • @Arthur,欢迎来到 StackOverflow!由于您的答案没有直接回答 OP 问题(您的答案确实是另一个问题以及建议的链接),因此更适合作为评论而不是答案发布。更多信息(可以在这里找到)[stackoverflow.com/help/how-to-answer] :)
猜你喜欢
  • 2018-10-24
  • 2019-10-21
  • 2018-06-10
  • 1970-01-01
  • 2016-12-07
  • 2015-08-23
  • 2020-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多