Pyplot - 移动图上x轴上的无用间隙

时间:2015-10-16 15:59:17

标签: python matplotlib graph pyqt

我在FigureCanvas上有一个PyPlot图,其中包含多个图,这些图通常会在计时器上清除并重新创建。

我希望x轴与绘图的长度相同(参见下面的可视示例),因为绘图遍历为数据的子集,但是发生了这种情况:

enter image description here

enter image description here

enter image description here

enter image description here

期望效果:

enter image description here

import matplotlib.pyplot as plt

class WidgetResults(QtGui.QWidget):

    def __init__(self, parent=None):

        super(WidgetResults, self).__init__(parent)

        #-- Plotting variables
        self.data               # A collection of PlotPair objects (Defined below)
                                # x-axis: datetime, y-axis: percentage
        self.curScreen = 0      # Number of data points that have been traversed
        self.screenCount = 100  # Number of data points to be plotted at a given time

        #-- Canvas
        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure) 
        self.canvas.setMinimumSize(350, 350) 
        self.canvas.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        self.ax = self.figure.add_subplot(111)
        self.ax.grid()
        self.ax.set_yticks([0,25,50,75,100]) # y-axis will always be a percentage
        self.ax.set_xticks([])
        self.ax.set_ylabel("Percent")

        #-- Layout
        layoutMain = QtGui.QHBoxLayout()
        layoutMain.addWidget(self.canvas)    
        self.setLayout(layoutMain)

        #-- Begin
        timer = QtCore.QTimer()
        timer.timeout.connect(self.updatePlot)
        timer.start(10)
        self.timer = timer
        self.plot()

    #-- Plots the initial graph
    def initialPlot(self):
        plots = self.data
        for i in range(len(plots)):
            plt = plots[i]
            dates = matplotlib.dates.date2num(plt.getX())[0:int(self.screenCount)]
            vals = plt.getY()[0:int(self.screenCount)]
            self.ax.plot(dates, vals, colours[i])
        self.ax.set_yticks([0,25,50,75,100])
        self.ax.set_xticks([])
        self.canvas.draw()

    #-- Called on timer ticks  
    def updatePlot(self):
        self.curScreen+=1
        plots = self.data
        self.ax.clear() 
        for i in range(len(plots)):
            plt = plots[i]
            dates = matplotlib.dates.date2num(plt.getX())[self.curScreen:int(self.screenCount)+self.curScreen]          
            vals = plt.getY()[self.curScreen:int(self.screenCount)+self.curScreen]
            self.ax.plot(dates, vals, colours[i])
        self.ax.set_yticks([0,25,50,75,100])
        self.ax.set_xticks([])        
        self.ax.set_autoscale_on(False)
        self.canvas.draw()

#-- This class holds a representation of a graph plot.
#-- Contains two lists of points, x-axis and y-axis.
class PlotPair(object):

    def __init__(self, x, y):

        self.xPlots = x
        self.yPlots = y

    def getPlots(self):

        plots = [self.xPlots, self.yPlots]
        return plots

    def getX(self):
        return self.xPlots

    def getY(self):
        return self.yPlots

1 个答案:

答案 0 :(得分:1)

通过调整updatePlot函数中的最后一行,我能够达到预期的效果。

在重绘之前重新定义yticks很重要,因为这些是在自动缩放中重置的。

    self.ax.relim()
    self.ax.autoscale()
    self.ax.set_yticks([0,25,50,75,100])
    self.canvas.draw()