对于这个问题我指的是:
链接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/