【问题标题】:Half violin plot in matplotlibmatplotlib 中的半小提琴图
【发布时间】:2015-06-28 20:16:29
【问题描述】:

最近 matplotlib 添加了对 violin plot 的原生支持。我想做的是half-小提琴情节为here。我想可以通过更改函数返回的body 来完成。你知道如何像示例中那样绘制半小提琴图,但使用 matplotlib 中的新函数吗?

【问题讨论】:

标签: matplotlib


【解决方案1】:
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))

fig, ax = plt.subplots(figsize=(18, 7))

v1 = ax.violinplot(data1, points=100, positions=np.arange(0, len(data1)),
               showmeans=False, showextrema=False, showmedians=False)
for b in v1['bodies']:
    # get the center
    m = np.mean(b.get_paths()[0].vertices[:, 0])
    # modify the paths to not go further right than the center
    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=100, positions=np.arange(0, len(data2)), 
               showmeans=False, showextrema=False, showmedians=False)

for b in v2['bodies']:
    # get the center
    m = np.mean(b.get_paths()[0].vertices[:, 0])
    # modify the paths to not go further left than the center
    b.get_paths()[0].vertices[:, 0] = np.clip(b.get_paths()[0].vertices[:, 0], m, np.inf)
    b.set_color('b')

ax.legend([v1['bodies'][0],v2['bodies'][0]],['data1', 'data2'])

【讨论】:

  • 与一些 cmets,它可能会有所帮助... TY
猜你喜欢
  • 2020-08-23
  • 1970-01-01
  • 1970-01-01
  • 2017-05-13
  • 2017-08-10
  • 2019-05-21
  • 2017-09-03
  • 2021-02-15
  • 1970-01-01
相关资源
最近更新 更多