代码之家  ›  专栏  ›  技术社区  ›  Surabhi Choudhary

pyqtgraph散点图项目设置

  •  2
  • Surabhi Choudhary  · 技术社区  · 8 年前

    from pyqtgraph.Qt import QtGui, QtCore
    import pyqtgraph as pg
    import numpy as np
    
    app = QtGui.QApplication([])
    mw = QtGui.QMainWindow()
    mw.resize(800,800)
    view = pg.GraphicsLayoutWidget()  
    mw.setCentralWidget(view)
    mw.setWindowTitle('pyqtgraph example: ScatterPlot')
    w1 = view.addPlot()
    x = [1,2,3,4,5,6,7,8,9,10]
    y = [10,8,6,4,2,20,18,16,14,12]
    s1 = pg.ScatterPlotItem(x,y,size=10, pen=pg.mkPen(None), brush=pg.mkBrush(255, 255, 255, 120))
    s1.setBrush(['r']*10, mask=None)
    w1.addItem(s1)
    mw.show()
    

    当我运行这段代码时,我得到如下错误:

    回溯(最近一次调用:文件“”,第1行,在

    文件 “C:\Python34\lib\site packages\pyqtgraph\graphicsItems\ScatterPlotItem.py”, 第467行,在挫折中 self.updatespot(数据集)

    文件 “C:\Python34\lib\site packages\pyqtgraph\graphicsItems\ScatterPlotItem.py”, self.fragmentAtlas。getAtlas()#生成atlas,以便源宽度可用。

    文件 “C:\Python34\lib\site packages\pyqtgraph\graphicsItems\ScatterPlotItem.py”, 第193行,在getAtlas中 self.buildAtlas()

    文件 “C:\Python34\lib\site packages\pyqtgraph\graphicsItems\ScatterPlotItem.py”, buildAtlas第143行 img=renderSymbol(键[0],键[1],sourceRect.pen,sourceRect.画笔)

    文件 “C:\Python34\lib\site packages\pyqtgraph\graphicsItems\ScatterPlotItem.py”, 第74行,在renderSymbol中 drawSymbol(p、符号、大小、笔、画笔)

    文件 “C:\Python34\lib\site packages\pyqtgraph\graphicsItems\ScatterPlotItem.py”, 第49行,图纸符号 油漆匠setBrush(画笔)类型错误:参数与任何重载调用不匹配:QPainter。挫折(QBrush):参数1有 意外类型“str”QPainter.setBrush(Qt.BrushStyle):参数1

    我怎样才能通过这个列表呢?我必须给不同的点分配不同的颜色。

    2 回复  |  直到 8 年前
        1
  •  2
  •   boudewijn21    6 年前

    symbolBrush 关键字。然后,您的代码变成:

    from pyqtgraph.Qt import QtGui, QtCore
    import pyqtgraph as pg
    import numpy as np
    import time
    import sys
    
    app = QtGui.QApplication([])
    mw = QtGui.QMainWindow()
    mw.resize(800,800)
    view = pg.GraphicsLayoutWidget()
    mw.setCentralWidget(view)
    mw.setWindowTitle('pyqtgraph example: ScatterPlot')
    w1 = view.addPlot()
    x = [1,2,3,4,5,6,7,8,9,10]
    y = [10,8,6,4,2,20,18,16,14,12]
    
    brush_list = [pg.mkColor(c) for c in "rgbcmykwrg"]
    
    p1 = w1.plot()
    s1 = p1.setData(x=x, y=y, size=10, pen=pg.mkPen(None), brush='g', symbol='o', symbolBrush=brush_list, symbolPen='r')
    mw.show()
    sys.exit(QtGui.QApplication.exec_())
    

    我需要这样做 p1 = w1.plot() 为了让它工作,但我对所有的pyqtgraph对象不够流利,无法告诉您它的作用。

    我必须将颜色字符串(或元组等)转换为 pg.mkColor() 对象以使其工作(尽管文档说不需要)

    请注意 brush 关键字填充图形,直到指定某个值(例如x轴),这对于线很有用,但对于点不显示任何内容,如您在图像中看到的。

    这个 symbolPen 关键字可用于点的线颜色。

    enter image description here

        2
  •  1
  •   eyllanesc RAHUL KUMAR    8 年前

    添加 s.setBrush(QtGui.QBrush({your parameters})) 举例来说:

    • QtGui.QBrush()
    • QtGui.QBrush(Qt.BrushStyle bs)
    • QtGui。QBrush(QColor,Qt.BrushStyle style=Qt.SolidPattern)
    • QtGui.QBrush(Qt.GlobalColor,Qt.BrushStyle style=Qt.SolidPattern)
    • QtGui。QBrush(QColor颜色,QPixmap像素映射)
    • QtGui.QBrush(Qt.GlobalColor、QPixmap和pixmap)
    • QtGui。QBrush(QPixmap pixmap)
    • QtGui。QBrush(QGradient渐变)
    • QtGui。QBrush(QBrushBrush)
    • QtGui。QBrush(QVariant变体)

    在您的代码中:

    import sys
    from pyqtgraph.Qt import QtGui, QtCore
    import pyqtgraph as pg
    import numpy as np
    
    app = QtGui.QApplication(sys.argv)
    mw = QtGui.QMainWindow()
    mw.resize(800, 800)
    view = pg.GraphicsLayoutWidget()
    mw.setCentralWidget(view)
    mw.setWindowTitle('pyqtgraph example: ScatterPlot')
    w1 = view.addPlot()
    x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    y = [10, 8, 6, 4, 2, 20, 18, 16, 14, 12]
    
    # Create seed for the random
    time = QtCore.QTime.currentTime()
    QtCore.qsrand(time.msec())
    
    for i in range(len(x)):
        s = pg.ScatterPlotItem([x[i]], [y[i]], size=10, pen=pg.mkPen(None))  # brush=pg.mkBrush(255, 255, 255, 120))
        s.setBrush(QtGui.QBrush(QtGui.QColor(QtCore.qrand() % 256, QtCore.qrand() % 256, QtCore.qrand() % 256)))
        w1.addItem(s)
    mw.show()
    sys.exit(QtGui.QApplication.exec_())
    

    输出:

    enter image description here

    如果要为前5个点(x:1-5)设置红色,为另5个点设置蓝色(x:6-10)

    import sys
    from pyqtgraph.Qt import QtGui
    import pyqtgraph as pg
    
    app = QtGui.QApplication(sys.argv)
    mw = QtGui.QMainWindow()
    mw.resize(800, 800)
    view = pg.GraphicsLayoutWidget()
    mw.setCentralWidget(view)
    mw.setWindowTitle('pyqtgraph example: ScatterPlot')
    w1 = view.addPlot()
    x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    y = [10, 8, 6, 4, 2, 20, 18, 16, 14, 12]
    
    color = QtGui.QColor("#0000FF")
    s = pg.ScatterPlotItem(x[:5], y[:5], size=10, pen=pg.mkPen(None), brush='r')
    w1.addItem(s)
    s = pg.ScatterPlotItem(x[5:], y[5:], size=10, pen=pg.mkPen(None), brush='b')
    w1.addItem(s)
    mw.show()
    sys.exit(QtGui.QApplication.exec_())
    

    enter image description here