Python、Seaborn:对数 Swarmplot 在 Swarm 中有意想不到的缺口 [英] Python, Seaborn: Logarithmic Swarmplot has unexpected gaps in the swarm

查看:76
本文介绍了Python、Seaborn:对数 Swarmplot 在 Swarm 中有意想不到的缺口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们看一个用 Python 3.5 和 Seaborn 制作的 swarmplot 对一些数据(它存储在 Pandas 数据帧 df 中,列标签存储在另一个类中.现在这无关紧要,只需看图):

ax = sns.swarmplot(x=self.dte.label_temperature, y=self.dte.label_current,hue=self.dte.label_voltage, data = df)

现在,如果在 y 轴上以对数刻度绘制,则数据更具可读性,因为它已经过去了几十年.因此,让我们将缩放比例更改为对数:

ax.set_yscale("log")ax.set_ylim(底部 = 5*10**-10)

好吧,我对群中的间隙有疑问.我猜他们在那里是因为他们在创建绘图时考虑了线性轴并且点不应该在那里重叠.但是现在它们看起来有点奇怪,并且有足够的空间来组成 4 个相同的群体.我的问题是:如何强制 seaborn 重新计算点的位置以创建更好看的群?

解决方案

Let's look at a swarmplot, made with Python 3.5 and Seaborn on some data (which is stored in a pandas dataframe df with column lables stored in another class. This does not matter for now, just look at the plot):

ax = sns.swarmplot(x=self.dte.label_temperature, y=self.dte.label_current, hue=self.dte.label_voltage, data = df)

Now the data is more readable if plotted in log scale on the y-axis because it goes over some decades. So let's change the scaling to logarithmic:

ax.set_yscale("log")
ax.set_ylim(bottom = 5*10**-10)

Well I have a problem with the gaps in the swarms. I guess they are there because they have been there when the plot is created with a linear axis in mind and the dots should not overlap there. But now they look kind of strange and there is enough space to from 4 equal looking swarms. My question is: How can I force seaborn to recalculate the position of the dots to create better looking swarms?

解决方案

mwaskom hinted to me in the comments how to solve this. It is even stated in the swamplot doku:

Note that arranging the points properly requires an accurate transformation between data and point coordinates. This means that non-default axis limits should be set before drawing the swarm plot.

Setting an existing axis to log-scale and use this for the plot:

    fig = plt.figure() # create figure
    rect = 0,0,1,1 # create an rectangle for the new axis
    log_ax = fig.add_axes(rect) # create a new axis (or use an existing one)
    log_ax.set_yscale("log") # log first
    sns.swarmplot(x=self.dte.label_temperature, y=self.dte.label_current, hue=self.dte.label_voltage, data = df, ax = log_ax)

This yields in the correct and desired plotting behaviour:

这篇关于Python、Seaborn:对数 Swarmplot 在 Swarm 中有意想不到的缺口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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