matplotlib /带有颜色映射的季节性小提琴图

时间:2019-08-02 09:44:12

标签: python matplotlib seaborn colormap violin-plot

我想创建一个带有matplotlib或searborn的小提琴图,其中该图根据颜色图进行着色。

这就是我得到的:

enter image description here

这就是我想要得到的(我在这里使用了Photoshop): enter image description here

如何获得所需的地块?

1 个答案:

答案 0 :(得分:3)

我认为这样做会更好,但是基于@ImportanceOfBeingErnest的评论,我想这实际上是可行的方法:

from matplotlib.path import Path
from matplotlib.patches import PathPatch


x = [np.random.normal(loc=i, scale=1, size=(100,)) for i in range(5)]

fig, ax = plt.subplots()
violins = ax.violinplot(x)

ymin, ymax = ax.get_ylim()
xmin, xmax = ax.get_xlim()

# create a numpy image to use as a gradient
Nx,Ny=1,1000
imgArr = np.tile(np.linspace(0,1,Ny), (Nx,1)).T
cmap = 'hsv'

for violin in violins['bodies']:
    path = Path(violin.get_paths()[0].vertices)
    patch = PathPatch(path, facecolor='none', edgecolor='none')
    ax.add_patch(patch)
    img = ax.imshow(imgArr, origin="lower", extent=[xmin,xmax,ymin,ymax], aspect="auto",
                    cmap=cmap,
                    clip_path=patch)

enter image description here