半小提琴情节

Rug*_*rra 8 matplotlib

最近matplotlib为小提琴情节添加了原生支持.我想做的是这里的萝卜情节.我想可以改变函数返回.您是否知道如何在示例中绘​​制半小提琴图,但使用matplotlib中的新函数?body

Rug*_*rra 17

data1 = (np.random.normal(0, 1, size=10000), np.random.normal(0, 2, size=10000))
data2 = (np.random.normal(1, 1, size=10000), np.random.normal(1, 2, size=10000))

f, ax = plt.subplots(figsize=(18, 7))
v1 = ax.violinplot(data1, points=50, positions=np.arange(0, len(data1)), widths=0.85,
               showmeans=False, showextrema=False, showmedians=False)
for b in v1['bodies']:
    m = np.mean(b.get_paths()[0].vertices[:, 0])
    b.get_paths()[0].vertices[:, 0] = np.clip(b.get_paths()[0].vertices[:, 0], -np.inf, m)
    b.set_color('r')
v2 = ax.violinplot(data2, points=50, positions=np.arange(0, len(data2)), widths=0.85,
               showmeans=False, showextrema=False, showmedians=False)
for b in v2['bodies']:
    m = np.mean(b.get_paths()[0].vertices[:, 0])
    b.get_paths()[0].vertices[:, 0] = np.clip(b.get_paths()[0].vertices[:, 0], m, np.inf)
    b.set_color('b')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 有一些评论,它可能会有所帮助... TY (3认同)