如何调整 Seaborn 散点图图例中点的大小?

huangapple go评论73阅读模式
英文:

How to adjust the size of the dots in the legend of a Seaborn scatterplot?

问题

我知道seaborn中scatterplots参数允许控制散点的大小。例如:

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame()
df['Y_LOCATION'] = [1, 1, 1, 2, 2, 4]
df['X_LOCATION'] = [1, 2, 3, 4, 3, 1]
df['VALUE'] = [-0.45, -0.14, -0.12, -0.36, -0.48, -0.20]
sns.set(rc={'figure.figsize':(40,20)})
sns.set(font_scale=2)
sns.scatterplot(df.Y_LOCATION, df.X_LOCATION, df.VALUE, s=800, palette="Greens_r")

然而,这个参数似乎对图例中显示的点的大小没有影响。如何调整图例中的点的大小?

英文:

I know that the s argument in searbons scatterplot allows to control the size of the dots. For instance:

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame()
df['Y_LOCATION'] = [1, 1, 1, 2, 2, 4]
df['X_LOCATION'] = [1, 2, 3, 4, 3, 1]
df['VALUE'] = [-0.45, -0.14, -0.12, -0.36, -0.48, -0.20]
sns.set(rc={'figure.figsize':(40,20)})
sns.set(font_scale=2)
sns.scatterplot(df.Y_LOCATION, df.X_LOCATION, df.VALUE, s=800, palette = "Greens_r")

如何调整 Seaborn 散点图图例中点的大小?

However, this parameter appears to have no impact on the size of the dots shown in the legend. How can these be adjusted?

答案1

得分: 2

你可以通过获取图例中点的句柄并调用set_sizes()来增加这些点的大小。它们默认为[36],将其乘以10,将面积(而非直径)增加了10倍。

示例:

ax = sns.scatterplot(x=df.Y_LOCATION, y=df.X_LOCATION, hue=df.VALUE, s=800, palette="Greens_r")
handles, labels = ax.get_legend_handles_labels()
for dot in handles:
    dot.set_sizes(dot.get_sizes() * 10)
plt.legend(handles, labels)

另请参考:https://stackoverflow.com/questions/60886965/how-to-adapt-too-large-dot-sizes-in-a-seaborn-scatterplot-legend

关于PathCollection.set_sizes()的文档:https://matplotlib.org/stable/api/collections_api.html#matplotlib.collections.PathCollection.set_sizes

英文:

You can increase the size of those dots by getting the handles of the dots in the legend, and calling set_sizes() on them. They are [36] by default, by multiplying that by 10, it increases the area (not diameter) by a factor of 10.

Example:

ax = sns.scatterplot(x = df.Y_LOCATION, y = df.X_LOCATION, hue = df.VALUE, s=800, palette = "Greens_r")
handles, labels = ax.get_legend_handles_labels()
for dot in handles:
    dot.set_sizes(dot.get_sizes() * 10)
plt.legend(handles, labels)

See also: https://stackoverflow.com/questions/60886965/how-to-adapt-too-large-dot-sizes-in-a-seaborn-scatterplot-legend

Documentation on PathCollection.set_sizes(): https://matplotlib.org/stable/api/collections_api.html#matplotlib.collections.PathCollection.set_sizes

答案2

得分: 1

你可以像这样做。。

    import numpy as np
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    plt.figure(figsize=(4, 2))
    sns.scatterplot(np.random.rand(1000, 2), s=1)
    plt.legend(fontsize=8, markerscale=1)
    plt.show()
英文:

You can do like this..

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

plt.figure(figsize=(4, 2))
sns.scatterplot(np.random.rand(1000, 2), s=1)
plt.legend(fontsize=8, markerscale=1)
plt.show()

如何调整 Seaborn 散点图图例中点的大小?

huangapple
  • 本文由 发表于 2023年6月26日 03:16:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定