Non rappresentare mezzo pixel con Matplotlib

Oct 30 2020

Qualcuno sa se è possibile non rappresentare la metà dei pixel della matrice diagonale utilizzando plt.imshow()?

Questo è espresso graficamente quello che sto cercando:

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

Immagino che questo possa essere fatto coprendo l'immagine con una maschera , ma è un'opzione che preferirei evitare .

Risposte

2 DizietAsahi Oct 30 2020 at 22:06

Puoi usare un Patchoggetto come maschera di ritaglio in matplotlib. Vederehttps://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)