matplotlib中的半小提琴图

编程入门 行业动态 更新时间:2024-10-26 05:29:05
本文介绍了matplotlib中的半小提琴图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

最近 matplotlib 添加了对 小提琴图 的原生支持.我想要做的是 half-violin plot as 此处.我想可以通过更改函数返回的 body 来完成.您是否知道如何像示例中那样绘制半小提琴图,但是使用matplotlib中的新功能?

Recently matplotlib has added a native support for violin plot. What I want to do is half-violin plot as here. I guess that it can be done changing the body the function is returning. Do you know how to plot half violin plot as in the example but using the new function from matplotlib?

推荐答案

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'])

更多推荐

matplotlib中的半小提琴图

本文发布于:2023-07-18 08:19:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1142781.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小提琴   matplotlib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!