对数刻度中的Seaborn条形图

问题描述

我正在使用pyplot.Boxplots在logscale中绘制Boxplot,然后我想使用seaborn在其上进行swarmplot / strip绘图。 但是,水手们把我们整个范围弄得一团糟,并且阴谋诡计。知道如何在seaborn中自定义职位吗?

att1= np.sort(np.unique(df1.att1))

w = 0.085
width = lambda p,w: 10 ** (np.log10(p) + w / 2.) - 10 ** (np.log10(p) - w / 2.)
custom_widths = width(freqns,w)


ax.Boxplot([df1[df1.att1== xi].att2 for xi in att1],positions=att1,Boxprops={'facecolor': 'none'},medianprops={'color': 'black'},patch_artist=True,widths=custom_widths)
ax.set_xscale("log")
fig.set_size_inches(10.5,8)
means = [np.median(df1[df1.Frequency == xi].CapDensity) for xi in freqs]
plt.plot(freqns,means,'--k*',lw=1.2)

这是不含条形图的图像:

This is image w/o strip plot

sns.stripplot(x="Frequency",y="CapDensity",data=df1,edgecolor="black",linewidth=.3,jitter=0.1,zorder=0.5,ax=ax)

这是我在Boxplot顶部剥离图的时候。

This is when I do strip plot on top of boxplot

解决方法

问题是您将0,1,2,3,...用作箱线图的位置,而seaborn始终会在内部位置from matplotlib import pyplot as plt import numpy as np import pandas as pd df = pd.DataFrame({'x': np.random.choice([1,5,8,10,30,50,100],500),'y': np.random.normal(750,20,500)}) xvals = np.unique(df.x) w = 0.085 width = lambda p,w: 10 ** (np.log10(p) + w / 2.) - 10 ** (np.log10(p) - w / 2.) custom_widths = width(xvals,w) fig,ax = plt.subplots(figsize=(12,4)) ax.set_xscale('log') ax.boxplot([df[df.x == xi].y for xi in xvals],positions=xvals,showfliers=False,boxprops={'facecolor': 'none'},medianprops={'color': 'black'},patch_artist=True,widths=custom_widths) medians = [np.median(df[df.x == xi].y) for xi in xvals] ax.plot(xvals,medians,'--k*',lw=2) ax.set_xticks(xvals) for xi,wi in zip(xvals,custom_widths): yis = df[df.x == xi].y ax.scatter(xi + np.random.uniform(-wi / 2,wi / 2,yis.size),yis) plt.show() 上放置一个简易绘图。最简单的解决方案是通过matplotlib创建剥离图。 (一个小样创建起来要复杂得多。)

假设您的数据与上一个问题类似,则可以将这样的图创建为:

def rows_generator(df):
    i = 0
    while (i+3) <= df.shape[0]:
        yield df.iloc[i:(i+3):1,:]
        i += 1

i = 1
for df in rows_generator(df):
    print(f'Time #{i}')
    print(df)
    i += 1

example plot