相对于图例,Seaborn重绘和散点图的标记大小不正确 [英] Incorrect marker sizes with Seaborn relplot and scatterplot relative to legend

查看:400
本文介绍了相对于图例,Seaborn重绘和散点图的标记大小不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解如何获得图例示例,以使其与在Jupyter笔记本中使用Seaborn的relplot绘制的点对齐.我的熊猫DataFrame df中有一个size(float64)列:

I'm trying to understand how to get the legend examples to align with the dots plotted using Seaborn's relplot in a Jupyter notebook. I have a size (float64) column in my pandas DataFrame df:

sns.relplot(x="A", y="B", size="size", data=df)

size列中的值为[0.0, -7.0, -14.0, -7.0, 0.0, 1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 8.0, 2.0, 0.0, -4.0, 7.0, -4.0, 0.0, 0.0, 4.0, 0.0, 0.0, -3.0, 0.0, 1.0, 7.0],如您所见,最小值为-14,最大值为8.传说与之相符.但是,看看绘制的实际点,有一个点大大小于图例中与-16对应的点.在图例中,也没有绘制像8一样大的点.

The values in the size column are [0.0, -7.0, -14.0, -7.0, 0.0, 1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 8.0, 2.0, 0.0, -4.0, 7.0, -4.0, 0.0, 0.0, 4.0, 0.0, 0.0, -3.0, 0.0, 1.0, 7.0] and as you can see, the minimum value is -14 and the maximum value is 8. It looks like the legend is aligned well with that. However, look at the actual dots plotted, there's a dot considerably smaller than the one corresponding to -16 in the legend. There's also no dot plotted as large as the 8 in the legend.

我在做什么错-还是一个错误?

What am I doing wrong -- or is this a bug?

我使用的是熊猫0.24.2和海洋生的0.9.0.

I'm using pandas 0.24.2 and seaborn 0.9.0.

修改: 仔细查看 Seaborn重复显示示例:

最小的权重是1613,但是情节的最左边有一个橙色的点,比图例中的1500小. 我认为这表明这是一个错误.

the smallest weight is 1613 but there's an orange dot to the far left in the plot that's smaller than the dot for 1500 in the legend. I think this points to this being a bug.

推荐答案

不知道seaborn在这里做了什么,但是如果您愿意单独使用matplotlib,它可能看起来像

Not sure what seaborn does here, but if you're willing to use matplotlib alone, it could look like

import numpy as np; np.random.rand
import matplotlib.pyplot as plt
import pandas as pd

s = [0.0, -7.0, -14.0, -7.0, 0.0, 1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 8.0, 2.0, 
     0.0, -4.0, 7.0, -4.0, 0.0, 0.0, 4.0, 0.0, 0.0, -3.0, 0.0, 1.0, 7.0]
x = np.linspace(0, 2*np.pi, len(s))
y = np.sin(x)
df = pd.DataFrame({"A" : x, "B" : y, "size" : s})

# calculate some sizes in points^2 from the initial values
smin = df["size"].min()
df["scatter_sizes"] = 0.25 * (df["size"] - smin + 3)**2
# state the inverse of the above transformation
finv = lambda y: 2*np.sqrt(y)+smin-3

sc = plt.scatter(x="A", y="B", s="scatter_sizes", data=df)
plt.legend(*sc.legend_elements("sizes", func=finv), title="Size")

plt.show()

更多详细信息,请参见带有散点图的散点图图例示例.

More details are in the Scatter plots with a legend example.

这篇关于相对于图例,Seaborn重绘和散点图的标记大小不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆