问题描述
有没有办法改变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?
推荐答案
-
bodies
:matplotlib.collections.PolyCollection
实例的列表,其中包含每个小提琴的填充区域. - [...以及其他...]
bodies
: A list of thematplotlib.collections.PolyCollection
instances containing the filled area of each violin.- [...among others...]
PolyCollection
s 的方法包括:
Methods of PolyCollection
s 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小提琴图的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!