不使用 Matplotlib 表示半个像素
Not to represent half a pixel with 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')
我想这可以通过用 mask 覆盖图像来完成,但我宁愿避免。
您可以在 matplotlib 中使用 Patch
对象作为剪贴蒙版。参见 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)
有谁知道是否可以不使用 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')
我想这可以通过用 mask 覆盖图像来完成,但我宁愿避免。
您可以在 matplotlib 中使用 Patch
对象作为剪贴蒙版。参见 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)