箱线图:seaborn 中的自定义宽度
Boxplot : custom width in seaborn
我正在尝试在 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 进行绘图。
Seaborn 的箱线图似乎不理解 widths=
参数。
这是一种通过接受 width=
参数的 matplotlib boxplot
为每个 x
值创建箱线图的方法。下面的代码假设数据是在熊猫的数据框中组织的。
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()
一个相关问题询问如何根据组大小设置框的宽度。宽度可以计算为某个最大宽度乘以每个组的大小与最大组的大小的比较。
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
y_true = np.random.normal(size=100)
y_pred = y_true + np.random.normal(size=100)
df = pd.DataFrame({'y_true': y_true, 'y_pred': y_pred})
df['y_true_bin'] = pd.cut(df['y_true'], range(-3, 4))
sns.set()
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(12, 5))
sns.boxplot(x='y_true_bin', y='y_pred', data=df, color='lightblue', ax=ax1)
bins, groups = zip(*df.groupby('y_true_bin')['y_pred'])
lengths = np.array([len(group) for group in groups])
max_width = 0.8
ax2.boxplot(groups, widths=max_width * lengths / lengths.max(),
patch_artist=True, boxprops={'facecolor': 'lightblue'})
ax2.set_xticklabels(bins)
ax2.set_xlabel('y_true_bin')
ax2.set_ylabel('y_pred')
plt.tight_layout()
plt.show()
我正在尝试在 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 进行绘图。
Seaborn 的箱线图似乎不理解 widths=
参数。
这是一种通过接受 width=
参数的 matplotlib boxplot
为每个 x
值创建箱线图的方法。下面的代码假设数据是在熊猫的数据框中组织的。
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()
一个相关问题询问如何根据组大小设置框的宽度。宽度可以计算为某个最大宽度乘以每个组的大小与最大组的大小的比较。
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
y_true = np.random.normal(size=100)
y_pred = y_true + np.random.normal(size=100)
df = pd.DataFrame({'y_true': y_true, 'y_pred': y_pred})
df['y_true_bin'] = pd.cut(df['y_true'], range(-3, 4))
sns.set()
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(12, 5))
sns.boxplot(x='y_true_bin', y='y_pred', data=df, color='lightblue', ax=ax1)
bins, groups = zip(*df.groupby('y_true_bin')['y_pred'])
lengths = np.array([len(group) for group in groups])
max_width = 0.8
ax2.boxplot(groups, widths=max_width * lengths / lengths.max(),
patch_artist=True, boxprops={'facecolor': 'lightblue'})
ax2.set_xticklabels(bins)
ax2.set_xlabel('y_true_bin')
ax2.set_ylabel('y_pred')
plt.tight_layout()
plt.show()