更改Matplotlib小提琴图的颜色 [英] Changing the color of matplotlib's violin plots

查看:631
本文介绍了更改Matplotlib小提琴图的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法改变matplotlib中violin图的颜色?

Is there a way to change the color of the violin plots in matplotlib?

默认颜色是这种棕色"颜色,虽然还不错,但是我想为前3把小提琴上色不同.我在文档中找不到任何参数.有什么想法或技巧可以使小提琴的颜色不同吗?

The default color is this "brownish" color, which is not too bad, but I'd like to color e.g., the first 3 violins differently to highlight them. I don't find any parameter in the documentation. Any ideas or hacks to color the violins differently?

推荐答案

表示返回:

将小提琴图的每个组件映射到所创建的相应集合实例列表的字典.字典具有以下键:

A dictionary mapping each component of the violinplot to a list of the corresponding collection instances created. The dictionary has the following keys:

  • bodies:matplotlib.collections.PolyCollection实例的列表,其中包含每个小提琴的填充区域.
  • [...以及其他...]
  • bodies: A list of the matplotlib.collections.PolyCollection instances containing the filled area of each violin.
  • [...among others...]

PolyCollection s 的方法包括:

Methods of PolyCollections include:

因此,您似乎可以遍历结果的正文列表并修改每个的脸色:

So, it looks like you could just loop through the result's body list and modify the facecolor of each:

violin_parts = plt.violinplot(...)

for pc in violin_parts['bodies']:
    pc.set_facecolor('red')
    pc.set_edgecolor('black')

有点奇怪,尽管您不能像普通的绘图类型一样在创建它时进行设置.我猜这可能是因为该操作创建了这么多位(前面提到的PolyCollection以及其他5个LineCollections),所以其他参数将是模棱两可的.

It is a bit strange though that you can't set this when creating it like the common plot types. I'd guess it's probably because the operation creates so many bits (the aforementioned PolyCollection along with 5 other LineCollections), that additional arguments would be ambiguous.

这篇关于更改Matplotlib小提琴图的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆