Mit Matplotlib kein halbes Pixel darstellen

Oct 30 2020

Weiß jemand, ob es möglich ist, nicht die Hälfte der Pixel der Diagonalmatrix mit darzustellen plt.imshow()?

Dies wird grafisch ausgedrückt, wonach ich suche:

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')

Ich denke, dies könnte erreicht werden, indem das Bild mit einer Maske abgedeckt wird , aber es ist eine Option, die ich lieber vermeiden möchte .

Antworten

2 DizietAsahi Oct 30 2020 at 22:06

Sie können ein PatchObjekt als Schnittmaske in matplotlib verwenden. Sehenhttps://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)