<p>@David Zwicker,谢谢你给我指出了正确的方向。以下代码在交互模式下正常工作(即可以重新调整图形窗口的大小),可以独立执行,也可以在ipythonqtconsole环境中执行。参见下面的嵌入图。然而,它仍然不能在ipythonwebnotebook环境中工作!任何关于这方面的帮助/想法都会很好。谢谢您。在</p>
<pre><code>#Imports
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['figure.dpi'] = 80 # default = 80
mpl.rcParams['savefig.dpi'] = 80 # default = 100
import matplotlib.patches as patches
import numpy as np
#Need to ensure that the figure.dpi (for displaying figure window) and
#savefig.dpi are consistent.
def redraw(event):
"""Redraw the plot on a resize event"""
if np.size(plt.get_figlabels()):
#Need to check if figure is closed or not and only then do the following
#operations. Else, the following operations will create a new figure
ax.clear()
drawRectangles(ax)
fig.canvas.draw()
else:
pass
def drawRectangles(ax):
"""Function to draw the normal and rotated patch in the transformed domain"""
#Transform for data coordinates to display coordinates
td2dis = ax.transData
coords = td2dis.transform([0.2, 0.5])
#rotate transform
tr = mpl.transforms.Affine2D().rotate_deg_around(coords[0], coords[1], 10)
t = td2dis + tr
rec0 = patches.Rectangle((0.2,0.5),0.25,0.2,color='blue',alpha=0.5)
ax.add_patch(rec0)
#Rotated rectangle patch
rect1 = patches.Rectangle((0.2,0.5),0.25,0.2,color='blue',alpha=0.5,transform=t)
ax.add_patch(rect1);
plt.grid()
figSize = (8,6)
fig = plt.figure("Patch rotate",figsize=figSize)
ax = fig.add_subplot(111)
ax.set_xlim(0,1);ax.set_ylim(0,1);
fig.canvas.mpl_connect('resize_event', redraw)
drawRectangles(ax)
plt.savefig("myfigure.png")
plt.show()
</code></pre>
<p>以下是上述代码的一些示例:</p>
<p>在代码中使用savefig()函数保存图像:
<img src="https://i.stack.imgur.com/FZtkd.png" alt="enter image description here"/></p>
<p>使用导航面板中的“保存”按钮保存的图像:
<img src="https://i.stack.imgur.com/YNKO6.png" alt="enter image description here"/></p>
<p>重新调整大小后使用导航面板中的“保存”按钮保存的图像:
<img src="https://i.stack.imgur.com/EWScr.png" alt="enter image description here"/>
<img src="https://i.stack.imgur.com/kfvUS.png" alt="enter image description here"/></p>