gradient color for plot

555 views
Skip to first unread message

Adolfo Núñez

unread,
Jun 16, 2015, 6:48:09 AM6/16/15
to pyqt...@googlegroups.com
Hi, I am quite new with pyqtgraph.
I would like to know if having a simple xy plot it is possible that the line color changes acording a gradient depending on y values. I mean, for example, if my y-values range is let's say between 0 and 100, when the plot gets closer to 0 it is turning blue and if it is closer to 100  it is turning red. And, if it is possible, how?
Thanks in advance. 

anonymou...@gmail.com

unread,
Jun 16, 2015, 8:47:36 AM6/16/15
to pyqt...@googlegroups.com
#pyqtgraph has superpowers from Qt

import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np


#using dimensions in pixel coordinates, i guess screen size wise
w
, h = 800, 600

lg0
= QtGui.QRadialGradient(w/2, h/2, h/2, w/2, h/2) #(cx, cy, radius, fx, fy)
lg0
.setColorAt(0, pg.mkColor('#ffffff'))
lg0
.setColorAt(1, pg.mkColor('#ee00ee'))
backgroundbrush
= QtGui.QBrush(lg0) #qBrush with the gradient

lg1
= QtGui.QLinearGradient(w/2, 0, w/2, h) #(xstart, ystart, xstop, ystop)
lg1
.setColorAt(0.2, pg.mkColor('#000000'))
lg1
.setColorAt(0.8, pg.mkColor('#ffffff'))
pen
= QtGui.QPen(lg1, 1) #qPen with this gradient


data
= np.random.random(100) *h #scale up height to gradient screen pixels
pg
.plot(data, pen=pen, background=backgroundbrush)


import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
   
QtGui.QApplication.instance().exec_()

'''
http://pyqt.sourceforge.net/Docs/PyQt4/qradialgradient.html
http://pyqt.sourceforge.net/Docs/PyQt4/qlineargradient.html
http://pyqt.sourceforge.net/Docs/PyQt4/qbrush.html
http://pyqt.sourceforge.net/Docs/PyQt4/qpen.html
'''

untitled.PNG

Adolfo Núñez

unread,
Jun 17, 2015, 2:12:18 AM6/17/15
to pyqt...@googlegroups.com
Thanks! That is what I was looking for.

Reply all
Reply to author
Forward
0 new messages