如何设置matplotlib.image对象的透明度?

2024-06-26 14:44:57 发布

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

我通常知道如何在matplotlib中更改绘图的透明度

但是这个阴谋我很难做到

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.image import NonUniformImage
Y = [2, 12, 52]
X = [10, 100]
ymin, ymax = 0, 60
xmin, xmax = 0, 150
Z = np.random.rand(len(Y), len(X))
extent = [xmin, xmax, ymin, ymax]
fig, ax = plt.subplots()
im = NonUniformImage(ax, interpolation='nearest', extent=extent, alpha=0.1)
im.set_data(X, Y, Z)
im.set_clip_path(ax.patch)
ax.images.append(im)
ax.set_xlim(extent[0], extent[1])
ax.set_ylim(extent[2], extent[3])
ax.set_xticks(X)
ax.set_yticks(Y)

我试着把alpha=0.1放在几个地方,例如

ax.images.append(im, alpha=0.1)

im.set_data(X, Y, Z.T, alpha=0.1)

im = NonUniformImage(ax, interpolation='nearest', extent=extent, alpha=0.1)

它们都不起作用。 我需要不均匀的图像,因为我有不均匀间隔的箱子

谢谢你的帮助


Tags: importalphamatplotlibasnppltaxextent