PyQt6基础_pyqtgraph_折线图with缩放调节

目录

字符型横坐标代码

折线图代码

运行


创建新类,继承pg.PlotWidget,在新类中实现业务内容,重写pg.PlotWidget中的wheelEvent方法并使用业务数据实现比较理想的缩放状态。

字符型横坐标代码

复制代码
class StrAxisItem(pg.AxisItem):
    def __init__(self,ticks,*args,**kwargs):
        pg.AxisItem.__init__(self,*args,**kwargs)
        self.x_values = [x[0] for x in ticks]
        self.x_strings = [x[1] for x in ticks]
        pass
    def tickStrings(self, values, scale, spacing):
        strings = []
        for v in values:
            vs = v*scale
            if vs in self.x_values:
                vstr = self.x_strings[self.x_values.index(vs)]
            else:
                vstr = ''
            strings.append(vstr)
        return strings

折线图代码

复制代码
class LineGraphWidget(pg.PlotWidget):
    def __init__(self):
        super().__init__()
        self.init_data()
        pass
    def init_data(self):
        self.whole_df:pd.DataFrame = pd.DataFrame()
        self.color_one = (30,144,255)
        self.color_two = (138,43,226)
        self.color_three = (220,20,60)
        pass
    def set_data(self,df:pd.DataFrame):
        self.clear()
        self.addLegend()

        self.whole_df = df

        x = df['x'].to_list()
        xTicks = df.loc[:, ['x', 'date']].values
        y_dict = {
            'one': df['one'].to_list(),
            'two': df['two'].to_list(),
            'three': df['three'].to_list()
        }

        one_curve = pg.PlotCurveItem(x=np.array(x),y=np.array(y_dict['one']),pen=pg.mkPen({'color':self.color_one}),connect='finite',name='one')
        two_curve = pg.PlotCurveItem(x=np.array(x),y=np.array(y_dict['two']),pen=pg.mkPen({'color':self.color_two}),connect='finite',name='two')
        three_curve = pg.PlotCurveItem(x=np.array(x),y=np.array(y_dict['three']),pen=pg.mkPen({'color':self.color_three}),connect='finite',name='three')

        horAxis = StrAxisItem(ticks=xTicks, orientation='bottom')
        self.setAxisItems({'bottom':horAxis})

        self.addItem(one_curve)
        self.addItem(two_curve)
        self.addItem(three_curve)

        self.vLine = pg.InfiniteLine(angle=90,movable=False)
        self.hLine = pg.InfiniteLine(angle=0,movable=False)
        self.label = pg.TextItem()

        self.addItem(self.vLine,ignoreBounds=True)
        self.addItem(self.hLine,ignoreBounds=True)
        self.addItem(self.label,ignoreBounds=True)

        self.vb = self.getViewBox()
        self.proxy = pg.SignalProxy(self.scene().sigMouseMoved, rateLimit=60, slot=self.pw_mouseMoved)
        self.enableAutoRange()
        pass
    def pw_mouseMoved(self, evt):
        pos = evt[0]
        if self.sceneBoundingRect().contains(pos):
            mousePoint = self.vb.mapSceneToView(pos)
            index = int(mousePoint.x())
            if index>=0 and index<len(self.whole_df):
                html_str = ''
                html_str += f"<br/>日期:{self.whole_df.loc[self.whole_df['x']==index].iloc[0]['date']}"
                html_str += f"<br/>one:{self.whole_df.loc[self.whole_df['x']==index].iloc[0]['one']}"
                html_str += f"<br/>two:{self.whole_df.loc[self.whole_df['x']==index].iloc[0]['two']}"
                html_str += f"<br/>three:{self.whole_df.loc[self.whole_df['x']==index].iloc[0]['three']}"

                self.label.setHtml(html_str)
                self.label.setPos(mousePoint.x(),mousePoint.y())
                pass
            self.vLine.setPos(mousePoint.x())
            self.hLine.setPos(mousePoint.y())
        pass
    def wheelEvent(self,ev):
        if len(self.whole_df) <= 0:
            super().wheelEvent(ev)
        else:
            delta = ev.angleDelta().x()
            if delta == 0:
                delta = ev.angleDelta().y()

            s = 1.001 ** delta

            before_xmin, before_xmax = self.viewRange()[0]
            val_x = self.getViewBox().mapSceneToView(ev.position()).x()

            after_xmin = int(val_x - (val_x - before_xmin) // s)
            after_xmax = int(val_x + (before_xmax - val_x) // s)

            if after_xmin < 1:
                after_xmin = 0
            if after_xmin >= len(self.whole_df):
                after_xmin = max(len(self.whole_df) - 3, len(self.whole_df) - 1)
            if after_xmax < 1:
                after_xmax = min(len(self.whole_df) - 1, 1)
            if after_xmax >= len(self.whole_df):
                after_xmax = len(self.whole_df) - 1

            # print(after_xmin, after_xmax)

            df00 = self.whole_df.loc[(self.whole_df['x'] >= after_xmin) & (self.whole_df['x'] <= after_xmax)].copy()
            after_ymin = min(df00['one'].min(), df00['two'].min(), df00['three'].min())
            after_ymax = max(df00['one'].max(), df00['two'].max(), df00['three'].max())
            self.setXRange(after_xmin, after_xmax)
            self.setYRange(after_ymin, after_ymax)
            pass
    pass

运行

复制代码
if __name__ == '__main__':
    df = pd.DataFrame(data={
        'date':['2020-01-01','2021-01-01','2022-01-01','2023-01-01','2024-01-01'],
        'one':[1,5,12,8,3],
        'two':[-9,2,4,8,-10],
        'three':[-1,5,8,10,-5]
    })
    df['x'] = range(len(df))

    app = QApplication([])
    pw = LineGraphWidget()
    pw.set_data(df.copy())
    pw.show()
    app.exec()
    pass
相关推荐
程序猿与金融与科技3 分钟前
PyQt6实例_消息工具_使用与完整代码分享
pyqt6·a股
朴拙数科10 分钟前
基于 RAG 的 Text2SQL 全过程的 Python 实现详解,结合 LangChain 框架实现自然语言到 SQL 的转换
python·sql·langchain
xiaohanbao0926 分钟前
day6 python数据可视化
python·学习·信息可视化·pandas
szial1 小时前
如何在 Conda 环境中降级 Python 版本:详细指南
python·conda
captain_keating1 小时前
使用matplotlib绘制Raincloud图/云雨图/柱状图/小提琴图
python·matplotlib
leolee181 小时前
PyInstaller 打包pc
python
站大爷IP1 小时前
Python frozenset 集合详解:不可变集合的终极指南
python
一点.点1 小时前
李沐动手深度学习(pycharm中运行笔记)——05.线性代数
pytorch·笔记·python·深度学习·pycharm·动手深度学习
Emma歌小白2 小时前
JavaScript (JS) 和 Python 语法对比
python
梓羽玩Python2 小时前
开源AI代理爆火!Suna:3天内新增5.5K+标星,自然对话驱动的自动化神器!
人工智能·python·github