python - 将 yaxis 固定到位

标签 python user-interface matplotlib pyqt scrollbar

这是我编写/改编的一些代码,它在下面产生输出:

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

import sys

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure

import matplotlib.pyplot as plt
import numpy as np


class LineBuilder:
    def __init__(self, ax):
        self.ax = ax
        self.on = 1
        self.lastline, = self.ax.plot([0],[0])
        self.cid = ax.figure.canvas.mpl_connect('pick_event', self)

    def __call__(self, event):
        self.on *=-1
        thisline = event.artist
        xdata = thisline.get_xdata()
        ydata = thisline.get_ydata()
        ind = event.ind
        print(xdata[ind])
        print('modified',xdata[ind][0])
        self.lastline.remove()
        self.lastline=self.ax.axvline(x=xdata[ind][0])
        self.ax.figure.canvas.draw()

class View(QGraphicsView):

    def __init__(self):
        super(View, self).__init__()

        self.initScene(5)

    def initScene(self,h):     

        self.scene = QGraphicsScene()
        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)
        self.figure.subplots_adjust(left=0.03,right=1,bottom=.1,top=1,wspace=0, hspace=0)

        ax = self.figure.add_subplot(111)
        ax.set_xlim([0,1000])
        data = np.random.rand(1000)
        ax.plot(data, '-') 

        self.canvas.draw()
        self.setScene(self.scene)
        self.scene.addWidget(self.canvas)

class MainWindow(QMainWindow):

    def __init__(self):
        super(MainWindow,self).__init__()

        self.setGeometry(150, 150, 700, 550) 

        self.view = View()
        self.view.setGeometry(0,0,self.width()*2,500)
        self.view.canvas.setGeometry(0,0,self.width()*2,500)        

        self.setCentralWidget(self.view)

app = QApplication(sys.argv)

window = MainWindow()
window.show()

app.exec_()

enter image description here

我可以使用底部的滚动条来查看整个图像。但是,滚动时,我很快就看不到 y 轴标签。是否可以将它们固定到位,以便滚动条仅更改可见的 x 轴部分(而 yaxis 刻度和标签保持固定)?

最佳答案

与其在 QGraphicsView 中滚动完整的图形,不如将 QScrollBar 添加到窗口并将其连接到一个函数,该函数更新图形内轴的 xlim。

import numpy as np
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure


class Fig(FigureCanvas):
    def __init__(self, *args,**kwargs):
        self.factor = kwargs.pop("factor", 2)
        FigureCanvas.__init__(self, Figure(), *args,**kwargs)
        self.plot()
        self.scroll = QScrollBar(Qt.Horizontal)
        self.scroll.actionTriggered.connect(self.updateFromScroll)

    def plot(self):
        self.ax = self.figure.add_subplot(111)
        data = np.random.rand(1000)
        self.ax.plot(data, '-')
        self.xmin,self.xmax = self.ax.get_xlim()
        self.ax.set_xlim(self.xmin, self.xmax/float(self.factor))


    def updateFromScroll(self, evt):
        v = self.scroll.value()
        a = [self.xmin, self.xmax/float(self.factor)]
        lim = a+v/100.*np.diff(a)[0]
        self.ax.set_xlim(lim)
        self.draw_idle()


class Window(QMainWindow):
    def __init__(self):


        QMainWindow.__init__(self)
        self.widget = QWidget()
        self.setCentralWidget(self.widget)
        self.widget.setLayout(QVBoxLayout())
        self.widget.layout().setContentsMargins(0,0,0,0)
        self.widget.layout().setSpacing(0)

        self.canvas = Fig()
        self.canvas.draw()


        self.nav = NavigationToolbar(self.canvas, self.widget)
        self.widget.layout().addWidget(self.nav)
        self.widget.layout().addWidget(self.canvas)
        self.widget.layout().addWidget(self.canvas.scroll)

        self.show()


qapp = QApplication([])
# pass the figure to the custom window
a = Window()
exit(qapp.exec_()) 

关于python - 将 yaxis 固定到位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48460935/

相关文章:

python - 如何修复: AttributeError:Class object has no attribute

Java 将 Swing 表存储在 Arraylist 中

python - 当 DISPLAY 在 python 中未定义时,使用 matplotlib 生成带有 UTF-8 标签的 JPG

python - bar3d 图中的错误重叠

python - 您如何确定 matplotlib 正在使用哪个后端?

python - Scikit-learn SVC 总是在随机数据交叉验证上给出准确度 0

python - DRF 在覆盖 get_queryset 时抛出 django.core.exceptions.ImproperlyConfigured

python - 如何在整个 numpy 矩阵中获取最大(顶部)N 个值

jquery - 如何在 jquery ui selectmenu 中对齐字符串

android - 如何在微调器中显示与下拉列表中不同的内容?