PyQtGraph: configuración de la pluma de símbolo de línea en el gráfico de línea

En este artículo veremos cómo podemos configurar la pluma de símbolo de línea en el gráfico de línea del módulo PyQtGraph. PyQtGraph es una biblioteca de interfaz de usuario y gráficos para Python que proporciona la funcionalidad comúnmente requerida en el diseño y las aplicaciones científicas. Sus objetivos principales son proporcionar gráficos rápidos e interactivos para mostrar datos (gráficos, videos, etc.) ‘ conectados por segmentos de línea recta. Es un tipo básico de gráfico común en muchos campos. El gráfico de líneas se crea con la ayuda de la clase de trazado en PyQtGraph. Los símbolos son la figura del punto que conectan los datos de la línea. Es principalmente x, o. La pluma es el objeto de pintor que se utiliza para dibujar el gráfico de líneas.

Podemos crear una ventana de trazado y crear líneas en ella con la ayuda de los comandos que se indican a continuación. 

# 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')

Para hacer esto usamos el método setSymbolPen con el objeto de línea
Sintaxis: line.setSymbolPen(QColor(10, 130, 3))
Argumento: Toma el objeto QColor/QPen como argumento
Retorno: Devuelve Ninguno 

A continuación se muestra la implementación. 

Python3

# importing Qt widgets
from PyQt5.QtWidgets import * import sys
 
# importing pyqtgraph as pg
import pyqtgraph as pg
from PyQt5.QtGui import *
 
# 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 = [2, 8, 6, 8, 6, 11, 14, 13, 18, 19]
 
        # 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 Values', units ='s')
 
        # setting horizontal range
        plt.setXRange(0, 10)
 
        # 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 symbol pen of the line 1
        line1.setSymbolPen(QColor(220, 30, 3))
 
        # 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 window
        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())

Producción : 

Publicación traducida automáticamente

Artículo escrito por rakshitarora y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *