using color as z-axis for plots

I've got many series of data that I want to plot, and each has an additional scalar that is valid for the whole series. What I want to do is plot all these series on top of each other (plot can do this just fine), but with the additional scalar changing the color, efectively using color as the z-axis. I'm not seeing how to do that. If there was a function where I could give a color map a value and it would spit out the color, that would work, but I haven't seen it. Thanks for your help.

--Chad Kidder

I've got many series of data that I want to plot, and each has an
additional scalar that is valid for the whole series. What I want to
do is plot all these series on top of each other (plot can do this
just fine), but with the additional scalar changing the color,
efectively using color as the z-axis. I'm not seeing how to do that.
If there was a function where I could give a color map a value and it
would spit out the color, that would work, but I haven't seen it.
Thanks for your help.

Check out the scatter_demo -- scatter takes an optional argument 'c'
for the color and an optional colormap

http://matplotlib.sourceforge.net/users/screenshots.html#scatter-demo

···

On Mon, Dec 8, 2008 at 12:45 PM, Chad Kidder <cckidder@...287...> wrote:

--Chad Kidder

------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you. Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Chad Kidder wrote:

I've got many series of data that I want to plot, and each has an additional scalar that is valid for the whole series. What I want to do is plot all these series on top of each other (plot can do this just fine), but with the additional scalar changing the color, efectively using color as the z-axis. I'm not seeing how to do that. If there was a function where I could give a color map a value and it would spit out the color, that would work, but I haven't seen it. Thanks for your help.

Try using scatter instead of plot. Specifically, the 'c' keyword argument:

       *c*:
         a color. *c* can be a single color format string, or a
         sequence of color specifications of length *N*, or a
         sequence of *N* numbers to be mapped to colors using the
         *cmap* and *norm* specified via kwargs (see below). Note
         that *c* should not be a single numeric RGB or RGBA
         sequence because that is indistinguishable from an array
         of values to be colormapped. *c* can be a 2-D array in
         which the rows are RGB or RGBA, however.

For example:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.randn(100)
y = np.random.randn(100)
data = x**2 + y**2
plt.scatter(x, y, c=data)
plt.show()

Ryan

···

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma

Close, but not quite what I want. Maybe this will tell what I want to do better:

···

On Dec 8, 2008, at 12:55 PM, Ryan May wrote:

Chad Kidder wrote:

I've got many series of data that I want to plot, and each has an additional scalar that is valid for the whole series. What I want to do is plot all these series on top of each other (plot can do this just fine), but with the additional scalar changing the color, efectively using color as the z-axis. I'm not seeing how to do that. If there was a function where I could give a color map a value and it would spit out the color, that would work, but I haven't seen it. Thanks for your help.

Try using scatter instead of plot. Specifically, the 'c' keyword argument:

     *c*:
       a color. *c* can be a single color format string, or a
       sequence of color specifications of length *N*, or a
       sequence of *N* numbers to be mapped to colors using the
       *cmap* and *norm* specified via kwargs (see below). Note
       that *c* should not be a single numeric RGB or RGBA
       sequence because that is indistinguishable from an array
       of values to be colormapped. *c* can be a 2-D array in
       which the rows are RGB or RGBA, however.

For example:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.randn(100)
y = np.random.randn(100)
data = x**2 + y**2
plt.scatter(x, y, c=data)
plt.show()

Ryan

----------------------
import numpy as n
import matplotlib.pyplot as p

nlines = 100
z = n.random.rand(nlines)
x = n.array(range(nlines))
t1, t2 = n.meshgrid(x,z)
y = t1+t2-0.5
for ii in range(nlines):
    p.plot(x,y[ii,:],color = str(z[ii]))
p.show()
-------------------
Instead of getting a grayscale plot out, I'd like to use a colormap like jet() or winter(). Any ideas there?

--Chad Kidder

Chad Kidder wrote:

Chad Kidder wrote:

I've got many series of data that I want to plot, and each has an additional scalar that is valid for the whole series. What I want to do is plot all these series on top of each other (plot can do this just fine), but with the additional scalar changing the color, efectively using color as the z-axis. I'm not seeing how to do that. If there was a function where I could give a color map a value and it would spit out the color, that would work, but I haven't seen it. Thanks for your help.

Try using scatter instead of plot. Specifically, the 'c' keyword argument:

     *c*:
       a color. *c* can be a single color format string, or a
       sequence of color specifications of length *N*, or a
       sequence of *N* numbers to be mapped to colors using the
       *cmap* and *norm* specified via kwargs (see below). Note
       that *c* should not be a single numeric RGB or RGBA
       sequence because that is indistinguishable from an array
       of values to be colormapped. *c* can be a 2-D array in
       which the rows are RGB or RGBA, however.

For example:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.randn(100)
y = np.random.randn(100)
data = x**2 + y**2
plt.scatter(x, y, c=data)
plt.show()

Ryan

Close, but not quite what I want. Maybe this will tell what I want to do better:

----------------------
import numpy as n
import matplotlib.pyplot as p

nlines = 100
z = n.random.rand(nlines)
x = n.array(range(nlines))
t1, t2 = n.meshgrid(x,z)
y = t1+t2-0.5
for ii in range(nlines):
    p.plot(x,y[ii,:],color = str(z[ii]))
p.show()
-------------------
Instead of getting a grayscale plot out, I'd like to use a colormap like jet() or winter(). Any ideas there?

In [6]:cmap = get_cmap('jet')

In [7]:cmap(0.2)
Out[7]:(0.0, 0.29999999999999999, 1.0, 1.0)

In [8]:cmap(0.8)
Out[8]:(1.0, 0.40740740740740755, 0.0, 1.0)

The pyplot.get_cmap() function gets a colormap by name. Calling that colormap with a floating-point argument in the 0-1 range returns the mapped color as an rgba tuple, which will be accepted by the color kwarg of plot. You can use pyplot.normalize to map your z range to the 0-1 range:

In [2]:norm = normalize(vmin=2, vmax=4)

In [3]:norm(3)
Out[3]:0.5

Alternatively, you can use a LineCollection. See the examples/pylab_examples/line_collection2.py script.

Eric

···

On Dec 8, 2008, at 12:55 PM, Ryan May wrote:

--Chad Kidder

------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you. Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Chad Kidder wrote:

Chad Kidder wrote:

I've got many series of data that I want to plot, and each has an additional scalar that is valid for the whole series. What I want to do is plot all these series on top of each other (plot can do this just fine), but with the additional scalar changing the color, efectively using color as the z-axis. I'm not seeing how to do that. If there was a function where I could give a color map a value and it would spit out the color, that would work, but I haven't seen it. Thanks for your help.

Try using scatter instead of plot. Specifically, the 'c' keyword argument:

     *c*:
       a color. *c* can be a single color format string, or a
       sequence of color specifications of length *N*, or a
       sequence of *N* numbers to be mapped to colors using the
       *cmap* and *norm* specified via kwargs (see below). Note
       that *c* should not be a single numeric RGB or RGBA
       sequence because that is indistinguishable from an array
       of values to be colormapped. *c* can be a 2-D array in
       which the rows are RGB or RGBA, however.

Here's what you're looking for:

import numpy as n
import matplotlib.pyplot as p
import matplotlib.colors as mcolors
import matplotlib.cm as cm

cmap = cm.get_cmap('winter')
norm = mcolors.Normalize(0, 1) #Range of z

nlines = 100
z = n.random.rand(nlines)
x = n.arange(nlines)
t1, t2 = n.meshgrid(x,z)
y = t1+t2-0.5

#Uses normalize to map z values to range of 0 to 1.
#Cmap maps these normalized values to colors
colors = cmap(norm(z))
for ii in range(nlines):
     p.plot(x, y[ii], color=colors[ii])
p.show()

Ryan

···

On Dec 8, 2008, at 12:55 PM, Ryan May wrote:

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma

Thanks all. I had tried something close, but it didn't work. This works great.

···

On Dec 8, 2008, at 2:00 PM, Ryan May wrote:

Chad Kidder wrote:

On Dec 8, 2008, at 12:55 PM, Ryan May wrote:

Chad Kidder wrote:

I've got many series of data that I want to plot, and each has an additional scalar that is valid for the whole series. What I want to do is plot all these series on top of each other (plot can do this just fine), but with the additional scalar changing the color, efectively using color as the z-axis. I'm not seeing how to do that. If there was a function where I could give a color map a value and it would spit out the color, that would work, but I haven't seen it. Thanks for your help.

Try using scatter instead of plot. Specifically, the 'c' keyword argument:

    *c*:
      a color. *c* can be a single color format string, or a
      sequence of color specifications of length *N*, or a
      sequence of *N* numbers to be mapped to colors using the
      *cmap* and *norm* specified via kwargs (see below). Note
      that *c* should not be a single numeric RGB or RGBA
      sequence because that is indistinguishable from an array
      of values to be colormapped. *c* can be a 2-D array in
      which the rows are RGB or RGBA, however.

Here's what you're looking for:

import numpy as n
import matplotlib.pyplot as p
import matplotlib.colors as mcolors
import matplotlib.cm as cm

cmap = cm.get_cmap('winter')
norm = mcolors.Normalize(0, 1) #Range of z

nlines = 100
z = n.random.rand(nlines)
x = n.arange(nlines)
t1, t2 = n.meshgrid(x,z)
y = t1+t2-0.5

#Uses normalize to map z values to range of 0 to 1.
#Cmap maps these normalized values to colors
colors = cmap(norm(z))
for ii in range(nlines):
   p.plot(x, y[ii], color=colors[ii])
p.show()

Ryan