我正在尝试在seaborn中绘制箱图,箱图的宽度取决于x轴值的对数。我正在创建宽度列表,并将其传递给seaborn.boxplot的widths=widths参数。
但是,我已经明白了
raise ValueError(datashape_message.format("widths"))
ValueError: List of boxplot statistics and `widths` values must have same the length
当我调试和检查时,在箱线图统计中只有一个字典,而我有8个箱图。不能准确地找出问题所在。
我正在使用pandas数据框和seaborn进行绘图。
发布于 2020-09-09 01:17:05
Seaborn的boxplot似乎不理解widths=
参数。
下面是一种通过x
库的boxplot
为每个matplotlib值创建箱形图的方法,它接受width=
参数。下面的代码假设数据是在熊猫的数据帧中组织的。
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
df = pd.DataFrame({'x': np.random.choice([1, 3, 5, 8, 10, 30, 50, 100], 500),
'y': np.random.normal(750, 20, 500)})
xvals = np.unique(df.x)
positions = range(len(xvals))
plt.boxplot([df[df.x == xi].y for xi in xvals],
positions=positions, showfliers=False,
boxprops={'facecolor': 'none'}, medianprops={'color': 'black'}, patch_artist=True,
widths=[0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
means = [np.mean(df[df.x == xi].y) for xi in xvals]
plt.plot(positions, means, '--k*', lw=2)
# plt.xticks(positions, xvals) # not needed anymore, as the xticks are set by the swarmplot
sns.swarmplot('x', 'y', data=df)
plt.show()
https://stackoverflow.com/questions/63792528
复制相似问题