目前,在matplotlib中,我在图上画了一个矩形。
currentAxis = plt.gca()
rect = mpatch.Rectangle((0.2, 0.420), 5.65, 0.730, edgecolor = None, facecolor = "white", zorder = 3)
currentAxis.add_patch(rect)
如何给这个矩形添加阴影(比如图例)?
有一种方法可以做到这一点,那就是使用 变革. 下面是一个使用 音阶翻译 方法,试图提供(我相信!)你所要求的东西。
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)
注意 transform
参数传递给 Rectangle
方法。
输出。
你现在可以使用内置的 影子 在matplotlib
rect = matplotlib.patches.Rectangle((0.4, 0.2), 0.4, 0.35,
facecolor='white', edgecolor='gray',
transform=ax.transAxes)
shadow = matplotlib.patches.Shadow(rect, 1/72., -1/72)
ax.add_patch(shadow)
ax.add_patch(rect)