简体   繁体   English

在PyQt中禁用matplotlib小部件

[英]Disable matplotlib widget in PyQt

I have a pyQt application with several embedded matplotlib widgets ( https://github.com/chipmuenk/pyFDA ). 我有一个带有几个嵌入式matplotlib小部件( https://github.com/chipmuenk/pyFDA )的pyQt应用程序。

The automatic updating of plots can be turned off for each plotting widget to speed up the application (especially 3D plots can take quite long). 可以为每个绘图小部件关闭绘图的自动更新,以加快应用程序的速度(特别是3D绘图可能需要很长时间)。

Unfortunately, I haven't managed to disable (grey out) the canvas completely yet. 不幸的是,我还没有完全禁用(灰色)画布。 What I'd like is to do something like 我想要做的是

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

to make it clear that there is nothing to interact with in this widget. 明确说明此小部件中没有与之交互的内容。 Is there an easy workaround? 有一个简单的解决方法吗?

Grey out figure. 灰色显示数字。

You may grey out the figure by placing a grey, semitransparent patch on top of it. 您可以通过在其顶部放置一个灰色的半透明色块来使图形变灰。 To this end, you may create a Rectangle, set its zorder very high and give it the figure transform. 为此,您可以创建一个Rectangle,将其zorder设置得很高,然后进行图形变换。 To add it to a an axes, you may use ax.add_patch ; 要将其添加到轴,可以使用ax.add_patch ; however in order to add it to a figure with a 3D axes, this will not work and you would need to add it via fig.patches.extend . 但是,要将其添加到带有3D轴的图形中,将无法正常工作,您需要通过fig.patches.extend添加它。 (See 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()

在此处输入图片说明

Disconnecting all events 断开所有事件

You may disconnect all events from the canvas. 您可以断开所有事件与画布的连接。 This will prevent any user interaction, but is also not reversible; 这将防止任何用户交互,但也是不可逆的。 so if you need those events back at a later stage the solution would be more complicated. 因此,如果您稍后需要这些事件,则解决方案将更加复杂。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM