Can I plot multiple channels with different colors in pyqtgraph with ArrayToQPath?
path = pg.arrayToQPath(xdata.flatten(), ydata.flatten(), conn.flatten())
item = QtGui.QGraphicsPathItem(path)
item.setPen(pg.mkPen('w'))
plt.addItem(item)
QGraphicsPathItem only supports drawing with a single color, so unfortunately it is necessary to create one item per color. For example, see examples/MultiPlotSpeedTest.py.
If this is not fast enough for you, consider using an OpenGL-based vis. library. VisPy has an example of this in examples/demo/gloo/realtime_signals.py.
Related
I have question to branca.colormap
Below code works:
from branca.colormap import linear
x=linear.YlOrRd_09.scale(1,10)
but I would like to use a different color palette for example gnuplot or gnuplot2
Below code doesn't work:
from branca.colormap import linear
x=linear.gnuplot.scale(1,10)
I have error '_LinearColormaps' object has not attribiute 'gnuplot'. Do You know how use other pallet with linearColormap or where can I find list of available colors names ?
I have one more question, below my code
import folium
import branca.colormap as cm
color_mapa=cm.linear.YlOrRd_09.scale(1,10)
color_mapa=color_mapa.to_step(index=[10,20,30,40,50,60,70,80,90,100])
color_mapa2=color_mapa.to_linear()
m=folium.Map(location=[52,20],zoom_start=7)
color_mapa.add_to(m)
color_mapa.caption='Colors'
color_mapa.add_to(m)
color_mapa2.add_to(m)
color_mapa2.caption='Colors2'
color_mapa2.add_to(m)
m.save('mapy_test.html')
The problem is when I want add lables. I do this by 'to_step()' and define index. But then colors don't change smoothly. So I add 'to_linear()' (color_mapa2), but this change labels (Colors2 on my peacture). Is the way to keep labels and have colors change smoothly ?
You can use the dir() function from the Python standard library to see all the attributes from the linear object :
>>> dir(linear)
['Accent_03',
'Accent_04',
'Accent_05',
'Accent_06',
'Accent_07',
'Accent_08',
'Blues_03',
...]
So I am trying to use Axes objects to control my matlibplot figure. I am not using plt (aka import matlibplot.pyplot as plt) because I am embedding the figure in my tkinter GUI per this.
However, I am also using subplots in the figure, so something like:
a = f.add_subplot(121)
a2 = f.add_subplot(122)
a.plot(fn2,mag)
a2.bar(range(0,10), magBin, width)
This is all well and good, I can use the axes properties to control things (i.e. a.axesMethod()), but I want string labels for my bar plots, per this, see code.
My dilemma is that I cannot use
plt.xticks(ind+width, ('G1', 'G2', 'G3', 'G4', 'G5') )
as in the example, because I cannot use plt if i want to embed it into my tkinter gui. I am limited to what I can do with Axes objects. I am trying to use
a2.set_xticks, but this does not allow for the string as ticks functionality I need for my bar chart.
Any help in this regard would be amazing!
Tyler
you can use instead:
axes.set_xticks(ticks, minor=False)
and
axes.set_xticklabels(labels, fontdict=None, minor=False)
I would like to use pyqtgraph to plot optical spectra of some signal vs wavelength in nm. The harder part is that it would be useful to plot the energy of the corresponding wavelength along the top of the graph. See the bottom figure for an example.
My question is how to accomplish this in pyqtgraph. I've thought about trying to modify the two y-axis solution (such as here), but I don't think it's really appropriate. The axis should be linked, not free to move independently, so adding a new viewbox doesn't seem like the right path, unless it's to link everything.
I think I could do something by adding a new axisitem and connecting the appropriate resizing signals to force the new axis coordinates to work, but that feels rather dirty.
http://www.nature.com/nnano/journal/v10/n10/images/nnano.2015.178-f1.jpg
I found a quick work around which somewhat works for my purposes. I figure I'll post it here in case others are curious and it may be helpful for them. It involves subclassing AxisItem and specifying tickStrings. It doesn't work ideally, as it maintains the same tick positions as the main bottom axis, but it should be at least give me an idea for what I'm looking at.
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
class CustomAxis(pg.AxisItem):
def tickStrings(self, values, scale, spacing):
return ['{:.4f}'.format(1./i) for i in values]
pg.mkQApp()
pw = pg.PlotWidget()
pw.show()
pw.setWindowTitle('pyqtgraph example: MultipleXAxes')
p1 = pw.plotItem
p1.setLabels(left='axis 1')
# Get rid of the item at the grid position where the top should be
p1.layout.removeItem(p1.getAxis('top'))
# make our own, setting the parent and orientation
caxis = CustomAxis(orientation='top', parent=p1)
caxis.setLabel('inverted')
caxis.linkToView(p1.vb)
# set the new one for internal plotitem
p1.axes['top']['item'] = caxis
# and add it to the layout
p1.layout.addItem(caxis, 1, 1)
p1.plot(np.arange(1, 7), [1,2,4,8,16,32])
#p2.addItem(pg.PlotCurveItem(1./np.arange(1, 7), [1,2,4,8,16,32], pen='b'))
## 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_()
Obviously, the return value should be replaced by whatever function relates the two axis.
I wonder how I might create a solid box of color for my python traits GUI which I can change the color of by, say, clicking on different buttons.
I've found the ColorEditor editor, so I can achieve solid boxes of color by defining a trait:
my_color_box = Color()
and then in my traits view definition:
Item('my_color_box', editor=ColorEditor(),style='readonly'),
However, the box also contains text with the color name, which isn't the look I want. I've tried the other styles of the ColorEditor() and none seem to give me a solid box of color.
Does anyone know how I could achieve this please?
Thanks,
This isn't natively dealt with in a traitsui editor as far as I can see. The easiest thing to do (depending on what you want to do) is to just use solid images of the desired color (with ImageEditor). Even if you have several different possibilities for colors that you'd like to flip between, an ImageEnumEditor (in readonly style) could capture them.
To capture the expressive power of traits being able to capture any arbitrary color (without enumerating the list of 256^3 possible colors which I don't recommend), you would need to a bit more work. Probably, you could define a custom editor that delves into the toolkit code to do this without too much effort. I was going to try to provide a minimal working example using wxpython, but I didn't find a super-obvious widget to do this with in wxpython and my wxpython skills are pretty marginal.
Edit:
I found a way to generate a table with colored boxes in it a year ago. Sorry I didn't think of it earlier, it's pretty hacky if you don't actually want a table mapping to colors (which is what I did want), so I bet you could construct something simpler using the traitsui and not the wx internals. But anywhere, here's something, in the spirit of trying to give you tools to help solve your own problem:
from traits.api import *
from traitsui.api import *
class ColorColumn(ObjectColumn):
def get_cell_color(self,object):
return object.color
class ColorContainer(HasTraits):
color=Color('red')
blank_text=Str('')
class SomeApplication(HasTraits):
dummy_table=List(ColorContainer)
def _dummy_table_default(self):
return [ColorContainer()]
traits_view=View(Item(name='dummy_table',
editor=TableEditor(columns=
[ColorColumn(label='',editor=TextEditor(),name='blank_text',editable=False)],
selection_bg_color=None,),show_label=False))
SomeApplication().configure_traits()
Edit2:
As you've asked for, here's a minimal working example using ImageEnumEditor or ImageEditor. In this example, the images are located at /path_to_the_python_file/images. Note that ImageEnumEditor only works with .gif files.
$ ls images
green.gif red.gif yellow.gif
from traits.api import *
from traitsui.api import *
from pyface.image_resource import ImageResource
class ImageEnumStyle(HasTraits):
ci=Enum('yellow','green','red','yellow')
traits_view=View(Item('ci',editor=ImageEnumEditor(path='images',),style='readonly'))
class ImageStyle(HasTraits):
ci=Instance(ImageResource)
#to modify the image, modify the ImageResource `name` attribute
def _ci_default(self):
return ImageResource('yellow.gif')
traits_view=View(Item('ci',editor=ImageEditor()))
ImageWhicheverStyleYouPrefer().configure_traits()
I would like to add a scale bar (showing how big a micron is for example) to a mayavi plot I create with mlab.
For example, referencing this question: How to display a volume with non-cubic voxels correctly in mayavi
I can set the voxel size of a plot by using
from enthought.mayavi import mlab
import numpy as np
s=64
x,y,z = np.ogrid[0:s,0:s,0:s/2]
volume = np.sqrt((x-s/2)**2 + (y-s/2)**2 + (2*z-s/2)**2)
grid = mlab.pipeline.scalar_field(data)
grid.spacing = [1.0, 1.0, 2.0]
contours = mlab.pipeline.contour_surface(grid,
contours=[5,15,25], transparent=True)
mlab.show()
I would like an automated way of adding a some indicator of what the scale of the object I am showing is. Right now I am adding scale bars by hand with inkscape to exported images, but there has to be a better way.
A straightforward mayavi way would be most helpful, but if there is anything in vtk that would do it, I can always use mayavi's wrapper.
Something like text3d will let me add text, and then I suppose I could figure out how to draw a line as well and compute the correct scaling by hand, but I am hoping there is an easier way.
Try the following:
mlab.axes()
mlab.outline()
mlab.colorbar()
This reference: http://github.enthought.com/mayavi/mayavi/auto/mlab_reference.html would help as would the several examples.