【问题标题】:matplotlib.plot in Pyqt5 [duplicate]Pyqt5中的matplotlib.plot [重复]
【发布时间】:2021-04-30 02:36:20
【问题描述】:

我正在尝试在 Pyqt5 小部件中嵌入绘图,我找到了这个类并且它可以工作,但是绘图上的范围只能从 0 到 range()。我需要从负 x 范围内的情节。我尝试修改它但失败了。我怎么能这样做?

class PlotCanvas(FigureCanvas):

    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)

        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self,
                QSizePolicy.Expanding,
                QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)
        self.plot()


    def plot(self):
        data = [random.random() for i in range(25)]
        plot = self.figure.add_subplot(111)
        plot.plot(data, 'r-')
        plot.set_title('PyQt Matplotlib Example')
        self.draw()

【问题讨论】:

  • 通常 matplotlib 可以很好地处理绘图范围。如果不满意,我会尝试在plot.plot(data, 'r-') 之后添加plot.set_xlim( min_x, max_x ) 以手动分配绘图范围。

标签: python matplotlib pyqt


【解决方案1】:

你快到了,你只需要修改你的plot()函数并像这样使用plot.set_xlim()

...
...
    def plot(self):
        data = [random.random() for i in range(25)]
        plot = self.figure.add_subplot(111)
        plot.plot(data, 'r-')
        plot.set_xlim(left=-10) #<-- added here
        plot.set_title('PyQt Matplotlib Example')
        self.draw()

它会生成这个图:

如您所见,x 轴已扩展为包含从-10 开始的负数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    相关资源
    最近更新 更多