gpt4 book ai didi

python - 将 yaxis 固定到位

转载 作者:太空宇宙 更新时间:2023-11-04 04:50:30 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com