如何在跨度矩形顶部绘制小提琴图的主体?
How to plot the body of violin-plot on top of span rectangle?
我想在背景颜色之上绘制小提琴图。我使用 axvspan
创建背景。我的问题是:我找不到让小提琴情节的主体(小提琴形状)出现在背景颜色之上的方法。它只能通过背景的透明度可见。由于 violin-plot 无法识别 zorder
命令,有没有办法让 violin-plot 位于所有内容之上?
例如:
import numpy as np
import matplotlib.pyplot as plt
plt.figure()
plt.axvspan(-5,0,color='r' ,alpha =0.5)
plt.axvspan(0,5,color='g' ,alpha =0.5)
plt.violinplot(np.random.normal(0,1,100), vert = False)
可以看到小提琴形状的蓝色出现在背景颜色(红色和绿色)的透明度中。我希望密度形状的蓝色位于所有内容之上。
这个问题类似于Changing the color of matplotlib's violin plots though not exactly the same. I won't repeat what Nick T has written in his answer (make sure to thank him as well), but just add that PolyCollection
has also set_alpha
and set_zorder
方法,你可以这样使用:
plt.figure()
plt.axvspan(-5, 0, color='r', alpha=0.5, zorder=1)
plt.axvspan(0, 5, color='g', alpha=0.5, zorder=1)
parts = plt.violinplot(np.random.normal(0, 1, 100), vert=False)
parts['bodies'][0].set_alpha(0.8)
parts['bodies'][0].set_zorder(2)
给出:
我想在背景颜色之上绘制小提琴图。我使用 axvspan
创建背景。我的问题是:我找不到让小提琴情节的主体(小提琴形状)出现在背景颜色之上的方法。它只能通过背景的透明度可见。由于 violin-plot 无法识别 zorder
命令,有没有办法让 violin-plot 位于所有内容之上?
例如:
import numpy as np
import matplotlib.pyplot as plt
plt.figure()
plt.axvspan(-5,0,color='r' ,alpha =0.5)
plt.axvspan(0,5,color='g' ,alpha =0.5)
plt.violinplot(np.random.normal(0,1,100), vert = False)
可以看到小提琴形状的蓝色出现在背景颜色(红色和绿色)的透明度中。我希望密度形状的蓝色位于所有内容之上。
这个问题类似于Changing the color of matplotlib's violin plots though not exactly the same. I won't repeat what Nick T has written in his answer (make sure to thank him as well), but just add that PolyCollection
has also set_alpha
and set_zorder
方法,你可以这样使用:
plt.figure()
plt.axvspan(-5, 0, color='r', alpha=0.5, zorder=1)
plt.axvspan(0, 5, color='g', alpha=0.5, zorder=1)
parts = plt.violinplot(np.random.normal(0, 1, 100), vert=False)
parts['bodies'][0].set_alpha(0.8)
parts['bodies'][0].set_zorder(2)
给出: