📌  相关文章
📜  PyQtGraph - 设置折线图中线的起始坐标

📅  最后修改于: 2022-05-13 01:55:35.854000             🧑  作者: Mango

PyQtGraph - 设置折线图中线的起始坐标

在本文中,我们将看到如何在 PyQtGraph 模块中设置线的起始坐标,即线图的线的原点。 PyQtGraph 是一个用于Python的图形和用户界面库,它提供了设计和科学应用程序中通常需要的功能。其主要目标是提供用于显示数据(绘图、视频等)的快速交互式图形。折线图或折线图或折线图或曲线图是一种将信息显示为一系列称为“标记”的数据点的图表' 由直线段连接。它是许多领域中常见的一种基本类型的图表。线图是在 PyQtGraph 中的绘图类的帮助下创建的。通过设置起始坐标,线倾向于从新坐标而不是原点开始。

我们可以在下面给出的命令的帮助下创建一个绘图窗口并在其上创建线条。

# creating a pyqtgraph plot window
plt = pg.plot()

# plotting line in green color
# with dot symbol as x, not a mandatory field
line = plt.plot(x, y, pen='g', symbol='x', symbolPen='g',
                symbolBrush=0.2, name='green')

下面是实现。

Python3
# importing Qt widgets
from PyQt5.QtWidgets import *
import sys
 
# importing pyqtgraph as pg
import pyqtgraph as pg
from PyQt5.QtGui import *
 
from PyQt5.QtCore import Qt
 
 
# Bar Graph class
class BarGraphItem(pg.BarGraphItem):
 
    # constructor which inherit original
    # BarGraphItem
    def __init__(self, *args, **kwargs):
        pg.BarGraphItem.__init__(self, *args, **kwargs)
 
    # creating a mouse double click event
    def mouseDoubleClickEvent(self, e):
 
        # setting scale
        self.setScale(0.2)
 
 
class Window(QMainWindow):
 
    def __init__(self):
        super().__init__()
 
        # setting title
        self.setWindowTitle("PyQtGraph")
 
        # setting geometry
        self.setGeometry(100, 100, 600, 500)
 
        # icon
        icon = QIcon("skin.png")
 
        # setting icon to the window
        self.setWindowIcon(icon)
 
        # calling method
        self.UiComponents()
 
        # showing all the widgets
        self.show()
 
    # method for components
    def UiComponents(self):
 
        # creating a widget object
        widget = QWidget()
 
        # creating a new label
        label = QLabel("GeeksforGeeks Line Plot")
 
        # making it multiline
        label.setWordWrap(True)
 
        # y values to plot by line 1
        y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 
        # y values to plot by line 2
        y2 = [3, 1, 5, 8, 9, 11, 16, 17, 14, 16]
        x = range(0, 10)
 
        # create plot window object
        plt = pg.plot()
 
        # showing x and y grids
        plt.showGrid(x=True, y=True)
 
        # adding legend
        plt.addLegend()
 
        # set properties of the label for y axis
        plt.setLabel('left', 'Vertical Values', units='y')
 
        # set properties of the label for x axis
        plt.setLabel('bottom', 'Horizontal Vlaues', units='s')
 
        # setting horizontal range
        plt.setXRange(0, 20)
 
        # setting vertical range
        plt.setYRange(0, 20)
 
        # plotting line in green color
        # with dot symbol as x, not a mandatory field
        line1 = plt.plot(x, y, pen='g', symbol='x',
                         symbolPen='g', symbolBrush=0.2, name='green')
 
        # plotting line2 with blue color
        # with dot symbol as o
        line2 = plt.plot(x, y2, pen='b', symbol='o',
                         symbolPen='b', symbolBrush=0.2, name='blue')
 
        # setting position to the line1
        line1.setPos(8, 1)
 
        # label minimum width
        label.setMinimumWidth(120)
 
        # Creating a grid layout
        layout = QGridLayout()
 
        # setting this layout to the widget
        widget.setLayout(layout)
 
        # adding label to the layout
        layout.addWidget(label, 1, 0)
 
        # plot window goes on right side, spanning 3 rows
        layout.addWidget(plt, 0, 1, 3, 1)
 
        # setting this widget as central widget of the main widow
        self.setCentralWidget(widget)
 
 
# create pyqt5 app
App = QApplication(sys.argv)
 
# create the instance of our Window
window = Window()
 
# start the app
sys.exit(App.exec())


输出: