Domanda

Attualmente, per disegnare un rettangolo sulla mia trama in matplotlib, io uso:

currentAxis = plt.gca()
rect = mpatch.Rectangle((0.2, 0.420), 5.65, 0.730, edgecolor = None, facecolor = "white", zorder = 3)
currentAxis.add_patch(rect)
.

Come aggiungere un'ombra a questo rettangolo (come per le leggende)?

È stato utile?

Soluzione

Una volta il modo di farlo è usare Trasformazioni .Ecco un esempio usando il traduzione in scala metodo nel tentativo di fornire(Credo!) Cosa hai chiesto:

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.transforms as transforms

# set up fig and axis
fig = plt.figure()
currentAxis = plt.gca()

# set translation 
dx, dy = 5/72., -5/72.
offset = transforms.ScaledTranslation(dx, dy, fig.dpi_scale_trans)
shadow_transform = currentAxis.transData + offset

# plot patch shadow
rect = patches.Rectangle((0.2, 0.420), 0.65, 0.730, transform=shadow_transform, 
                         edgecolor = None, facecolor = "black", zorder = 3)
currentAxis.add_patch(rect)

# plot patch
rect = patches.Rectangle((0.2, 0.420), 0.65, 0.730, edgecolor = None, 
                         facecolor = "white", zorder = 3)
currentAxis.add_patch(rect)
.

Nota L'argomento transform è passato al metodo Rectangle.

Uscita:

shadowing

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top