Ne pas représenter un demi-pixel avec Matplotlib
Oct 30 2020
Est-ce que quelqu'un sait s'il est possible de ne pas représenter la moitié des pixels de la matrice diagonale en utilisant plt.imshow()?
Ceci est exprimé graphiquement ce que je recherche:
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')
Je suppose que cela pourrait être fait en couvrant l'image avec un masque , mais c'est une option que je préfère éviter .
Réponses
2 DizietAsahi Oct 30 2020 at 22:06
Vous pouvez utiliser un Patchobjet comme masque d'écrêtage dans matplotlib. Voirhttps://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)