PyPlot: Individually-colored points possible?

plotting
pyplot

#1

Using PyPlot, what is a good way of plotting points which have individual colors?

I don’t want to do a LineCollection, partly because I tried and it didn’t work. Basically every idea I have seems like a hack (scatter plot with really small circles, etc.).

Btw, can someone with the right permissions create a PyPlot tag?

Edit: Added pyplot tag


#2

If you’re dead-set on using PyPlot, then i think you can pass a list of (r,g,b,a) tuples for the colors to scatter. But really you should be using Plots :wink:


#3

I don’t think that a scatter plot is a hack. I think that this is exactly what scatter plots are for:

x = linspace(0,2pi,10)
y = sin(x)
scatter(x, y, c=y, edgecolors="none")

Does that not do basically what you want?

http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.scatter

Btw, don’t listen to @tbreloff, PyPlot is awesome. :slight_smile:


#4

I’ve been meaning to try Plots but haven’t taken the time to sit down and go through all of the documentation. You have a ton of work done in it but it looks like it’s still evolving quickly.

@Daniel_Carrera, that’s just about what I was looking for. For some reason I though scatter plots only took shapes. The only thing I would really like to know now is how to make my own custom color map.

The PyPlot documentation and a matplotlib example let me to create my own,

cm = PyPlot.LinearSegmentedColormap("MyColormap",Vector{Float64}[[1,0,0],[0,1,0],[0,0,1],[1,0,0]],50)

which returns ColorMap "MyColormap" but scatterplot still doesn’t like it.

julia> scatter(x,y,c=y,edgecolors="none",cmap=cm)
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1540, in __call__
    return self.func(*args)
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 590, in callit
    func(*args)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 373, in idle_draw
    self.draw()
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 354, in draw
    FigureCanvasAgg.draw(self)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py", line 474, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/python2.7/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/figure.py", line 1159, in draw
    func(*args)
  File "/usr/lib/python2.7/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 2324, in draw
    a.draw(renderer)
  File "/usr/lib/python2.7/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/collections.py", line 815, in draw
    Collection.draw(self, renderer)
  File "/usr/lib/python2.7/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/collections.py", line 263, in draw
    self.update_scalarmappable()
  File "/usr/lib/python2.7/dist-packages/matplotlib/collections.py", line 713, in update_scalarmappable
    self._facecolors = self.to_rgba(self._A, self._alpha)
  File "/usr/lib/python2.7/dist-packages/matplotlib/cm.py", line 262, in to_rgba
    x = self.cmap(x, alpha=alpha, bytes=bytes)
  File "/usr/lib/python2.7/dist-packages/matplotlib/colors.py", line 550, in __call__
    self._init()
  File "/usr/lib/python2.7/dist-packages/matplotlib/colors.py", line 732, in _init
    self.N, self._segmentdata['red'], self._gamma)
TypeError: list indices must be integers, not unicode
PyObject <matplotlib.collections.PathCollection object at 0x7fae238f4c90>

If I try a simple array it still creates the colormap but again doesn’t like it.

julia> cm = PyPlot.LinearSegmentedColormap("MyColormap",[1.0 0 0;0 1 0;0 0 1;1 0 0]
       ,50)
ColorMap "MyColormap"

julia> scatter(x,y,c=y,edgecolors="none",cmap=cm)
/usr/lib/python2.7/dist-packages/matplotlib/colors.py:732: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  self.N, self._segmentdata['red'], self._gamma)
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1540, in __call__
    return self.func(*args)
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 590, in callit
    func(*args)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 373, in idle_draw
    self.draw()
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 354, in draw
    FigureCanvasAgg.draw(self)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_agg.py", line 474, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/python2.7/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/figure.py", line 1159, in draw
    func(*args)
  File "/usr/lib/python2.7/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 2324, in draw
    a.draw(renderer)
  File "/usr/lib/python2.7/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/collections.py", line 815, in draw
    Collection.draw(self, renderer)
  File "/usr/lib/python2.7/dist-packages/matplotlib/artist.py", line 61, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/collections.py", line 263, in draw
    self.update_scalarmappable()
  File "/usr/lib/python2.7/dist-packages/matplotlib/collections.py", line 713, in update_scalarmappable
    self._facecolors = self.to_rgba(self._A, self._alpha)
  File "/usr/lib/python2.7/dist-packages/matplotlib/cm.py", line 262, in to_rgba
    x = self.cmap(x, alpha=alpha, bytes=bytes)
  File "/usr/lib/python2.7/dist-packages/matplotlib/colors.py", line 550, in __call__
    self._init()
  File "/usr/lib/python2.7/dist-packages/matplotlib/colors.py", line 732, in _init
    self.N, self._segmentdata['red'], self._gamma)
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
PyObject <matplotlib.collections.PathCollection object at 0x7fae23871990>

I even tried adding a fourth value to each color for the alpha but the results were the same.

Registering the colormap doesn’t help either. It just gives one of the errors above.