python - matplotlib + PyQt5 : replace a canvas

标签 python matplotlib pyqt

对于这个问题我指的是:

链接1:https://matplotlib.org/1.5.3/examples/user_interfaces/embedding_in_qt5.html

准确地说,我指的是上面链接中的这一部分:

class MyStaticMplCanvas(MyMplCanvas):
    """Simple canvas with a sine plot."""

    def compute_initial_figure(self):
        t = arange(0.0, 3.0, 0.01)
        s = sin(2*pi*t)
        self.axes.plot(t, s)

我想将上面的 Canvas 更改为这个:

链接2:https://matplotlib.org/gallery/pyplots/fig_x.html#sphx-glr-gallery-pyplots-fig-x-py

我是 matplotlib 的新手。虽然我尝试了很多但仍然不起作用。因此我想问是否有人可以进行替换,我可以理解 matplotlib 的工作原理。

感谢您的帮助!

最佳答案

尚不完全清楚您想要实现的目标。也许发布一个Minimal, Complete, and Verifiable example会更好(此处关键字是“minimal”),而不是尝试修改示例中的代码,这可能过于复杂。

无论如何都要替换原来使用的正弦波matplotlib example带有十字 second example ,您需要在基础 Canvas __init__ 函数中存储对 Figure 对象的引用

import matplotlib.lines as lines

class MyMplCanvas(FigureCanvas):
    """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""

    def __init__(self, parent=None, width=5, height=4, dpi=100):
        self.fig = Figure(figsize=(width, height), dpi=dpi)  # <------
        # replace all references to fig by self.fig in the rest of the code

然后将 MyStaticMplCanvas 类的代码替换为示例中的代码:

class MyStaticMplCanvas(MyMplCanvas):
    """Simple canvas with a sine plot."""

    def compute_initial_figure(self):
        l1 = lines.Line2D([0, 1], [0, 1], transform=self.fig.transFigure, figure=self.fig)
        l2 = lines.Line2D([0, 1], [1, 0], transform=self.fig.transFigure, figure=self.fig)
        self.fig.lines.extend([l1, l2])

关于python - matplotlib + PyQt5 : replace a canvas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48901369/

相关文章:

python - PyQt:在系统托盘应用程序中显示菜单

python - 类型错误 : QApplication(List[str]): not enough arguments

python - “str”对象不支持项目分配

python - 如何安排热图 pcolormesh 的数据?

python - 模块未找到错误: No module named 'matplotlib._path'

python - 在 python 中创建动画的方法?

python - 使用 df.column.str.contains 并更新 pandas 数据框列

python - 在 Python 中将 long 从寄存器转换为 char 数组

python - 检查 subprocess.getoutput 命令是否执行成功

python - PyQt5 从另一种形式调用一种形式