更改Matplotlib小提琴图的颜色

编程入门 行业动态 更新时间:2024-10-26 01:25:27
本文介绍了更改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:

  • set_color(c) ,它同时设置了facecolor和edgecolor ,
  • set_facecolor(c) 和
  • set_edgecolor(c) 都带有一个"matplotlib颜色arg或rgba元组的序列"

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

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小提琴图的颜色

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

发布评论

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

>www.elefans.com

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