英文:
How to adjust the size of the dots in the legend of a Seaborn scatterplot?
问题
我知道seaborn中scatterplot
的s
参数允许控制散点的大小。例如:
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")
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)
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论