在 PyQt 中禁用 matplotlib 小部件
Disable matplotlib widget in PyQt
我有一个带有几个嵌入式 matplotlib 小部件的 pyQt 应用程序 (https://github.com/chipmuenk/pyFDA)。
可以为每个绘图小部件关闭绘图的自动更新以加快应用程序速度(尤其是 3D 绘图可能需要很长时间)。
不幸的是,我还没有完全禁用(变灰)canvas。我想做的是
class MplWidget(QWidget):
"""
Construct a subwidget with Matplotlib canvas and NavigationToolbar
"""
def __init__(self, parent):
super(MplWidget, self).__init__(parent)
# Create the mpl figure and construct the canvas with the figure
self.fig = Figure()
self.pltCanv = FigureCanvas(self.fig)
#-------------------------------------------------
self.mplwidget = MplWidget(self)
self.mplwidget.pltCanv.setEnabled(False) # <- this doesn't work
明确表示此小部件中没有可交互的内容。有简单的解决方法吗?
灰掉数字。
您可以通过在其顶部放置一个灰色、半透明的补丁来使图形变灰。为此,您可以创建一个矩形,将其 zorder 设置得非常高,并对其进行图形变换。要将其添加到轴,您可以使用 ax.add_patch
;然而,为了将它添加到具有 3D 轴的图形中,这将不起作用,您需要通过 fig.patches.extend
添加它。 (参见 this answer)
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot([1,3,2],[1,2,3],[2,3,2])
rect=plt.Rectangle((0,0),1,1, transform=fig.transFigure,
clip_on=False, zorder=100, alpha=0.5, color="grey")
fig.patches.extend([rect])
plt.show()
正在断开所有事件
您可以断开与 canvas 的所有活动。这将阻止任何用户交互,但也是不可逆的;因此,如果您需要在稍后阶段返回这些事件,则解决方案会更加复杂。
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot([1,3,2],[1,2,3],[2,3,2])
for evt, callback in fig.canvas.callbacks.callbacks.items():
for cid, _ in callback.items():
fig.canvas.mpl_disconnect(cid)
plt.show()
我有一个带有几个嵌入式 matplotlib 小部件的 pyQt 应用程序 (https://github.com/chipmuenk/pyFDA)。
可以为每个绘图小部件关闭绘图的自动更新以加快应用程序速度(尤其是 3D 绘图可能需要很长时间)。
不幸的是,我还没有完全禁用(变灰)canvas。我想做的是
class MplWidget(QWidget):
"""
Construct a subwidget with Matplotlib canvas and NavigationToolbar
"""
def __init__(self, parent):
super(MplWidget, self).__init__(parent)
# Create the mpl figure and construct the canvas with the figure
self.fig = Figure()
self.pltCanv = FigureCanvas(self.fig)
#-------------------------------------------------
self.mplwidget = MplWidget(self)
self.mplwidget.pltCanv.setEnabled(False) # <- this doesn't work
明确表示此小部件中没有可交互的内容。有简单的解决方法吗?
灰掉数字。
您可以通过在其顶部放置一个灰色、半透明的补丁来使图形变灰。为此,您可以创建一个矩形,将其 zorder 设置得非常高,并对其进行图形变换。要将其添加到轴,您可以使用 ax.add_patch
;然而,为了将它添加到具有 3D 轴的图形中,这将不起作用,您需要通过 fig.patches.extend
添加它。 (参见 this answer)
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot([1,3,2],[1,2,3],[2,3,2])
rect=plt.Rectangle((0,0),1,1, transform=fig.transFigure,
clip_on=False, zorder=100, alpha=0.5, color="grey")
fig.patches.extend([rect])
plt.show()
正在断开所有事件
您可以断开与 canvas 的所有活动。这将阻止任何用户交互,但也是不可逆的;因此,如果您需要在稍后阶段返回这些事件,则解决方案会更加复杂。
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot([1,3,2],[1,2,3],[2,3,2])
for evt, callback in fig.canvas.callbacks.callbacks.items():
for cid, _ in callback.items():
fig.canvas.mpl_disconnect(cid)
plt.show()