Matplotlib set_y/xlabel 不起作用

Matplotlib set_y/xlabel doen'nt work

我想简单地设置我的子图的 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()

提前谢谢!

因为你有

self.axes.hold(False)

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

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

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