用Matplotlib不代表半个像素

米格尔·冈萨雷斯(Miguel Gonzalez)

有人知道是否可能不使用对角矩阵表示一半像素plt.imshow()吗?

这是我想要的图形表示:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
import matplotlib

bins = 5
Z = np.random.rand(bins, bins)

# Select lower triangle values:
condition = np.tril(np.ones((Z.shape))).astype(np.bool)
Z = np.where(condition, Z, np.nan)

fig, ax = plt.subplots(figsize = (8,8))
ax.imshow(Z, cmap = 'Spectral')

我想这可以通过用蒙版覆盖图像来完成,但是我宁愿避免这种选择

朝日朝日

您可以Patch在matplotlib中将对象用作剪贴蒙版。参见https://matplotlib.org/3.1.0/gallery/images_contours_and_fields/image_clip_path.html

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
import matplotlib

bins = 5
Z = np.random.rand(bins, bins)

# Select lower triangle values:
condition = np.tril(np.ones((Z.shape))).astype(np.bool)
Z = np.where(condition, Z, np.nan)

fig, ax = plt.subplots()
im = ax.imshow(Z, cmap = 'Spectral')

tri = matplotlib.patches.Polygon([(0,0),(1,0),(0,1)], closed=True, transform=ax.transAxes)
im.set_clip_path(tri)

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章