python小提琴图正轴
python violin plot regular axis
我想绘制分箱数据的小提琴图,但同时能够绘制模型预测并可视化模型描述单个数据分布的主要部分的程度。我想我的问题是,小提琴情节之后的 x 轴的行为不像带有数字的常规轴,而更像是偶然碰巧是数字的字符串值。也许不是一个很好的描述,但在这个例子中我想有一个“正常”的绘图函数,例如f(x) = 2*x**2
,在 x=1、x=5.2、x=18.3 和 x=27 时,我想在背景中播放小提琴。
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
np.random.seed(10)
collectn_1 = np.random.normal(1, 2, 200)
collectn_2 = np.random.normal(802, 30, 200)
collectn_3 = np.random.normal(90, 20, 200)
collectn_4 = np.random.normal(70, 25, 200)
ys = [collectn_1, collectn_2, collectn_3, collectn_4]
xs = [1, 5.2, 18.3, 27]
sns.violinplot(x=xs, y=ys)
xx = np.arange(0, 30, 10)
plt.plot(xx, 2*xx**2)
plt.show()
不知何故,这段代码实际上并没有绘制小提琴,而是绘制了小节,这只是这个例子中的一个问题,而不是原始代码中的问题。在我的真实代码中,我希望两边都有不同的“半小提琴”,因此我使用 sns.violinplot(x="..", y="..", hue="..", data=.., split=True)
.
我认为 seaborn
很难做到这一点,因为它没有提供一种简单的方法来操纵它创建的艺术家,特别是如果在同一个轴上绘制了其他东西。 Matplotlib 的 violinplot
允许设置小提琴的位置,但不提供仅绘制半个小提琴的选项。因此,我建议使用 statsmodels.graphics.boxplots.violinplot
,两者都可以。
from statsmodels.graphics.boxplots import violinplot
df = sns.load_dataset('tips')
x_col = 'day'
y_col = 'total_bill'
hue_col = 'smoker'
xs = [1, 5.2, 18.3, 27]
xx = np.arange(0, 30, 1)
yy = 0.1*xx**2
cs = ['C0','C1']
fig, ax = plt.subplots()
ax.plot(xx,yy)
for (_,gr0),side,c in zip(df.groupby(hue_col),['left','right'],cs):
print(side)
data = [gr1 for (_,gr1) in gr0.groupby(x_col)[y_col]]
violinplot(ax=ax, data=data, positions=xs, side=side, show_boxplot=False, plot_opts=dict(violin_fc=c))
# violinplot above messes up which ticks are shown, the line below restores a sensible tick locator
ax.xaxis.set_major_locator(matplotlib.ticker.MaxNLocator())
我想绘制分箱数据的小提琴图,但同时能够绘制模型预测并可视化模型描述单个数据分布的主要部分的程度。我想我的问题是,小提琴情节之后的 x 轴的行为不像带有数字的常规轴,而更像是偶然碰巧是数字的字符串值。也许不是一个很好的描述,但在这个例子中我想有一个“正常”的绘图函数,例如f(x) = 2*x**2
,在 x=1、x=5.2、x=18.3 和 x=27 时,我想在背景中播放小提琴。
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
np.random.seed(10)
collectn_1 = np.random.normal(1, 2, 200)
collectn_2 = np.random.normal(802, 30, 200)
collectn_3 = np.random.normal(90, 20, 200)
collectn_4 = np.random.normal(70, 25, 200)
ys = [collectn_1, collectn_2, collectn_3, collectn_4]
xs = [1, 5.2, 18.3, 27]
sns.violinplot(x=xs, y=ys)
xx = np.arange(0, 30, 10)
plt.plot(xx, 2*xx**2)
plt.show()
不知何故,这段代码实际上并没有绘制小提琴,而是绘制了小节,这只是这个例子中的一个问题,而不是原始代码中的问题。在我的真实代码中,我希望两边都有不同的“半小提琴”,因此我使用 sns.violinplot(x="..", y="..", hue="..", data=.., split=True)
.
我认为 seaborn
很难做到这一点,因为它没有提供一种简单的方法来操纵它创建的艺术家,特别是如果在同一个轴上绘制了其他东西。 Matplotlib 的 violinplot
允许设置小提琴的位置,但不提供仅绘制半个小提琴的选项。因此,我建议使用 statsmodels.graphics.boxplots.violinplot
,两者都可以。
from statsmodels.graphics.boxplots import violinplot
df = sns.load_dataset('tips')
x_col = 'day'
y_col = 'total_bill'
hue_col = 'smoker'
xs = [1, 5.2, 18.3, 27]
xx = np.arange(0, 30, 1)
yy = 0.1*xx**2
cs = ['C0','C1']
fig, ax = plt.subplots()
ax.plot(xx,yy)
for (_,gr0),side,c in zip(df.groupby(hue_col),['left','right'],cs):
print(side)
data = [gr1 for (_,gr1) in gr0.groupby(x_col)[y_col]]
violinplot(ax=ax, data=data, positions=xs, side=side, show_boxplot=False, plot_opts=dict(violin_fc=c))
# violinplot above messes up which ticks are shown, the line below restores a sensible tick locator
ax.xaxis.set_major_locator(matplotlib.ticker.MaxNLocator())