Matplotlib集合

2024-10-03 17:20:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我只想设置我的子批次的x和y标签,我不明白我做错了什么?代码没有给我错误,只是没有显示标签。 下面不显示调用update_figure函数的代码。每秒都会调用Update_figure。但我希望init函数中有set_xlabel函数。在

有人能帮我解决这个问题吗?在

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):
    fig = Figure(figsize=(width, height), dpi=dpi)
    self.axes = fig.add_subplot(111)
    self.axes.autoscale(False)
    #We want the axes cleared every time plot() is called
    self.axes.hold(False)

    self.axes.set_title('Sharing x per column, y per row')
    self.axes.set_ylabel('time(s)')
    self.axes.set_ylim(0, 100)

    self.compute_initial_figure()

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

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

def compute_initial_figure(self):
    self.axes.plot([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], scaley=False)

class MyDynamicMplCanvas(MyMplCanvas):
"""A canvas that updates itself every second with a new plot."""
yAxe = [0]
xAxe = [0]
i = 0
def __init__(self, *args, **kwargs):
    MyMplCanvas.__init__(self, *args, **kwargs)
    # self.a = np.array([0,0,0,0,0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    # timer = QtCore.QTimer(self)
    # timer.timeout.connect(self.update_figure)
    # timer.start(1000)

def update_figure(self):
    # Build a list of 4 random integers between 0 and 10 (both inclusive)
    self.yAxe = np.append(self.yAxe, (getCO22()))
    self.xAxe = np.append(self.xAxe, self.i)
    # print(self.xAxe)
    if len(self.yAxe) > 10:
        self.yAxe = np.delete(self.yAxe, 0)

    if len(self.xAxe) > 10:
        self.xAxe = np.delete(self.xAxe, 0)
    self.axes.set_ylabel('time(s)')
    self.axes.plot(self.xAxe, self.yAxe, scaley=False)
    self.axes.grid(True)
    self.i = self.i + 1

    self.draw()

提前通知!在


Tags: 函数selffalseplotinitdefnpupdate
1条回答
网友
1楼 · 发布于 2024-10-03 17:20:45

因为你有

self.axes.hold(False)

每次调用plot时,都会清除绘图和地物(包括标签、标题和轴限制)。在

您需要为您正在进行的绘图类型保留hold(False)。在

因此,您需要将self.axes.set_title('...')self.axes.set_ylabel(...)以及任何其他类似的命令移动到update_figure()函数中的self.axes.plot(..)之下。在

相关问题 更多 >