Matplotlib set_y / xlabel doen' nt work

时间:2015-04-20 13:05:05

标签: python matplotlib

我想简单地设置我的子标题的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()
提前thx!

1 个答案:

答案 0 :(得分:1)

因为你有

self.axes.hold(False)

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

你需要为你正在进行的情节类型保留hold(False)

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