ไม่ใช่แทนครึ่งพิกเซลด้วย Matplotlib
Oct 30 2020
มีใครรู้บ้างว่าเป็นไปไม่ได้ที่จะไม่แสดงครึ่งพิกเซลของเมทริกซ์แนวทแยงโดยใช้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')
ผมคิดว่านี้สามารถทำได้โดยครอบคลุมภาพที่มีหน้ากากแต่มันก็เป็นตัวเลือกที่ฉันค่อนข้างหลีกเลี่ยง
คำตอบ
2 DizietAsahi Oct 30 2020 at 22:06
คุณสามารถใช้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)