matplotlib/seaborn小提琴绘图,带彩色地图

2024-09-27 17:54:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用matplotlib或searborn创建一个小提琴图,其中的图是根据颜色图着色的。你知道吗

这就是我得到的:

enter image description here

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

我怎样才能得到想要的情节?你知道吗


Tags: matplotlib颜色着色情节photoshopsearborn
1条回答
网友
1楼 · 发布于 2024-09-27 17:54:06

我原以为这样做会更好,但是,根据@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

相关问题 更多 >

    热门问题