# PyPlot: Individually-colored points possible?

**URL:** <https://discourse.julialang.org/t/pyplot-individually-colored-points-possible/696>\
**Category:** Visualization\
**Tags:** plotting, pyplot\
**Created:** [December 2, 2016, 3:35pm UTC](https://discourse.julialang.org/t/pyplot-individually-colored-points-possible/696 "2016-12-02T15:35:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daneel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daneel/32/175_2.png) [@Daneel](https://discourse.julialang.org/u/Daneel)\
**Post date:** [December 2, 2016, 3:35pm UTC](https://discourse.julialang.org/t/pyplot-individually-colored-points-possible/696/1 "2016-12-02T15:35:58Z")

</div>

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

I don’t want to do a [LineCollection](https://gist.github.com/gizmaa/7214002#line-collection), 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

---

<div class="post-metadata">

**Author:** ![tbreloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbreloff/32/68_2.png) [@tbreloff](https://discourse.julialang.org/u/tbreloff)\
**Post date:** [December 2, 2016, 3:46pm UTC](https://discourse.julialang.org/t/pyplot-individually-colored-points-possible/696/2 "2016-12-02T15:46:50Z")

</div>

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 😉

---

<div class="post-metadata">

**Author:** ![danielc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielc/32/220_2.png) [@danielc](https://discourse.julialang.org/u/danielc)\
**Post date:** [December 3, 2016, 4:02pm UTC](https://discourse.julialang.org/t/pyplot-individually-colored-points-possible/696/3 "2016-12-03T16:02:53Z")

</div>

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

```julia
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](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.scatter)

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

---

<div class="post-metadata">

**Author:** ![Daneel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daneel/32/175_2.png) [@Daneel](https://discourse.julialang.org/u/Daneel)\
**Post date:** [December 5, 2016, 10:51am UTC](https://discourse.julialang.org/t/pyplot-individually-colored-points-possible/696/4 "2016-12-05T10:51:51Z")

</div>

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](https://github.com/JuliaPy/PyPlot.jl#color-maps) and a [matplotlib example](http://matplotlib.org/examples/pylab_examples/custom_cmap.html) let me to create my own,

```julia
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
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
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.
