Color in Isocurve?

434 views
Skip to first unread message

Travis Autry

unread,
Dec 12, 2015, 8:23:09 PM12/12/15
to pyqtgraph
Hi,

I am new to pyqtgraph and have been modifying an example trying to write a useable data analysis program.  It seems fantastic but I cannot figure out how to make a color contour plot using isocurve and matplotlib colormaps.  My attempt at a solution is below


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

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import cm


cmap = plt.get_cmap("YlOrRd")

if (QtGui.QApplication.instance() == None):
    app = QtGui.QApplication([])

## make pretty looping data

x = np.linspace(0,10,100)
y = np.linspace(0,5,200)

a,b = np.meshgrid(x,y)

z = np.exp(-10*a)*np.exp(b)

data = z

data = np.fft.fftshift(np.fft.fft(data))
win = pg.GraphicsWindow()
win.setWindowTitle('pyqtgraph example: Isocurve')
vb = win.addViewBox()
img = pg.ImageItem(data)
vb.addItem(img)
vb.setAspectLocked()

## generate empty curves
curves = []
levels = np.linspace(data.min(), data.max(), 100)
for i in range(len(levels)):
    v = levels[i]
    ## generate isocurve with color selection
    f = pg.mkPen(cmap(i))
    c = pg.IsocurveItem(level=v, pen= f)
    c.setParentItem(img)  ## make sure isocurve is always correctly displayed over image
    c.setZValue(10)
    curves.append(c)

c.setParentItem(img)     
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

vas...@gmail.com

unread,
Dec 12, 2015, 11:14:37 PM12/12/15
to pyqt...@googlegroups.com
Hi.

I don't really know what are you trying cause still don't see any of contour plot in your app.
Generally, my way to convert matplotlib color to colormap that pyqtgraph can recognize is:

import matplotlib.pyplot as plt
import pyqtgraph as pg
cmap = plt.get_cmap("flag")
pg_cmap = tuple(int(255*x) for x in cmap(i))
pg_pen = pg.mkPen(pg_cmap)

So, there is some modified original script https://github.com/campagnola/test/blob/master/examples/isocurve.py with converted matplotlib colormap. I used "flag" colormap.



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

import matplotlib.pyplot as plt
cmap = plt.get_cmap("flag")
# Possible values are:
# Spectral, summer, coolwarm, Wistia_r, pink_r, Set1, Set2, Set3, brg_r, Dark2, prism, PuOr_r, afmhot_r, terrain_r, PuBuGn_r, RdPu,
# gist_ncar_r, gist_yarg_r, Dark2_r, YlGnBu, RdYlBu, hot_r, gist_rainbow_r, gist_stern, PuBu_r, cool_r, cool, gray, copper_r, Greens_r,
# GnBu, gist_ncar, spring_r, gist_rainbow, gist_heat_r, Wistia, OrRd_r, CMRmap, bone, gist_stern_r, RdYlGn, Pastel2_r, spring, terrain,
# YlOrRd_r, Set2_r, winter_r, PuBu, RdGy_r, spectral, rainbow, flag_r, jet_r, RdPu_r, gist_yarg, BuGn, Paired_r, hsv_r, bwr, cubehelix,
# Greens, PRGn, gist_heat, spectral_r, Paired, hsv, Oranges_r, prism_r, Pastel2, Pastel1_r, Pastel1, gray_r, jet, Spectral_r, gnuplot2_r,
# gist_earth, YlGnBu_r, copper, gist_earth_r, Set3_r, OrRd, gnuplot_r, ocean_r, brg, gnuplot2, PuRd_r, bone_r, BuPu, Oranges, RdYlGn_r,
# PiYG, CMRmap_r, YlGn, binary_r, gist_gray_r, Accent, BuPu_r, gist_gray, flag, bwr_r, RdBu_r, BrBG, Reds, Set1_r, summer_r, GnBu_r, BrBG_r,
# Reds_r, RdGy, PuRd, Accent_r, Blues, autumn_r, autumn, cubehelix_r, nipy_spectral_r, ocean, PRGn_r, Greys_r, pink, binary, winter, gnuplot,
# RdYlBu_r, hot, YlOrBr, coolwarm_r, rainbow_r, Purples_r, PiYG_r, YlGn_r, Blues_r, YlOrBr_r, seismic, Purples, seismic_r, RdBu, Greys, BuGn_r,
# YlOrRd, PuOr, PuBuGn, nipy_spectral, afmhot

 
app = QtGui.QApplication([])
 
## make pretty looping data
frames = 200
data = np.random.normal(size=(frames,30,30), loc=0, scale=100)
data = np.concatenate([data, data], axis=0)
data = ndi.gaussian_filter(data, (10, 10, 10))[frames/2:frames + frames/2]
data[:, 15:16, 15:17] += 1

 
win = pg.GraphicsWindow()
win.setWindowTitle('pyqtgraph example: Isocurve')
vb = win.addViewBox()
img = pg.ImageItem(data[0])

vb.addItem(img)
vb.setAspectLocked()
 
## generate empty curves
curves = []
levels = np.linspace(data.min(), data.max(), 100)
for i in range(len(levels)):
    v = levels[i]
    ## generate isocurve with cmap color selection
    pg_cmap = tuple(int(255*x) for x in cmap(i))
    pg_pen = pg.mkPen(pg_cmap)   
    #print i, pg_cmap
    c = pg.IsocurveItem(level=v, pen=pg_pen)

    c.setParentItem(img)  ## make sure isocurve is always correctly displayed over image
    c.setZValue(10)
    curves.append(c)
 
## animate!
ptr = 0
imgLevels = (data.min(), data.max() * 2)
def update():
    global data, curves, img, ptr, imgLevels
    ptr = (ptr + 1) % data.shape[0]
    data[ptr]
    img.setImage(data[ptr], levels=imgLevels)
    for c in curves:
        c.setData(data[ptr])
 
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(50)

 
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/e17a0183-2800-4ec6-af1a-f271dd344d62%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Travis Autry

unread,
Dec 13, 2015, 8:27:58 PM12/13/15
to pyqtgraph
Thank you for the reply.  I'm not sure your code works?  I just see the same greyscale/ black and white isocurve when executing.  I'm trying to create a contour or contour filled plot in a manner that is similiar to matplotlibs contour or contourf plot.

The post here https://groups.google.com/forum/#!topic/pyqtgraph/XGao_P9bSnQ led me to think that it had to be done with isocurve.  So I am trying to implement a relatively simple way of doing this.  If this is an incorrect approach I would love to know a better one.  Any simple working examples would be extremely helpful.  I'm not familiar with pyqt and I find the pyqtgraph to be a bit lacking in describing this type of plotting.

Thanks so much.

vas...@gmail.com

unread,
Dec 13, 2015, 8:47:44 PM12/13/15
to pyqt...@googlegroups.com
Try to uncomment line 44 (print i, pg_cmap) to see final values. It should be 4 0-255 integers.

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
Isocurve.png
isocurve_item_original.py

Travis Autry

unread,
Dec 13, 2015, 9:19:48 PM12/13/15
to pyqtgraph

So the conversion to a colormap clearly works.  I am unsure how you are getting a colored contour though.  When I run the code I just get








On Saturday, December 12, 2015 at 6:23:09 PM UTC-7, Travis Autry wrote:

vas...@gmail.com

unread,
Dec 13, 2015, 9:29:13 PM12/13/15
to pyqt...@googlegroups.com
Maybe we not using same version of python libraries. My PC (Windows 7-32, Python 2.7) responds:
0.9.10
1.9.1
0.16.0b2

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
pg_np_sc_version.py

vas...@gmail.com

unread,
Dec 14, 2015, 4:56:44 AM12/14/15
to pyqt...@googlegroups.com
This is just my way for making isocurve to look as conoturf in the example. Maybe this could be good for a start. First problem is that matplotlib's contourf do most of thing (levels, colors, dtihering) to you, but it is too slow, and second is to pyqtgraph isocurve's demo is for animation of 3d arrays, so in this case I used same 2D structure as in the matplotlib's example and just plot 255 levels of isocurves with 255 color levels from 'jet' colormap without any animation. My original intention is to result looks similar like in  the matplotlib's example, so I also calculating min and max values of an array and make some flipping of an array at the end. I am sure that someone could make this better.

Good luck,
Vasilije
 
IMPORTANT: As I can see - not working on some 15.04 64-bit Ubuntu with numpy 1.10.x, but works on Windows 7 32-bit with numpy 1.9.1  and 64-bit Zorin OS 9 with numpy 1.8.2.

isocurve_as_contourf.png
isocurve_as_contourf.py

vas...@gmail.com

unread,
Dec 14, 2015, 5:17:38 AM12/14/15
to pyqt...@googlegroups.com
Also, if you add QtGui.QApplication.processEvents() in your loop, you can see iscocurves as they occur.
isocurve_as_contourf_with_processEvents.py

Travis Autry

unread,
Dec 14, 2015, 10:58:59 PM12/14/15
to pyqtgraph
Hi Vasilije,

Thanks for the code to try.  I still seem to be having trouble with it.  My version of python is 2.7.10 and I am using Anaconda 2.2.0 (x86_64) on a Mac OSX El Capitan 10.11.  My PyQT version is '4.8.7'.

Any help would be appreciated.

vas...@gmail.com

unread,
Dec 15, 2015, 12:13:28 AM12/15/15
to pyqt...@googlegroups.com
I think that version of numpy makes problem.

What is response of your configuration for the next code:
import numpy
print numpy.__version__


vas...@gmail.com

unread,
Dec 15, 2015, 12:29:22 AM12/15/15
to pyqt...@googlegroups.com
You also can try to run the next script:
import pyqtgraph.examples
pyqtgraph.examples.run()

Then try to run some demo example scripts. It should run all (IsoCurveItem is under GraphicsItems), but try some "force" combinations, maybe that could give you the answer what is going wrong with your configuration.
Reply all
Reply to author
Forward
0 new messages