微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用Matplotlib不代表半个像素

如何解决用Matplotlib不代表半个像素

有人知道是否可能无法使用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,1)],closed=True,transform=ax.transAxes)
im.set_clip_path(tri)

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。