Pyplot移动绘图上X轴上不需要的间隙

2024-10-04 03:29:04 发布

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

我在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

Tags: ofselfdatadefpltaxcanvasdates
1条回答
网友
1楼 · 发布于 2024-10-04 03:29:04

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

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

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

相关问题 更多 >