15 Nov 2012 18:49

## Re: discrete colors from colormap for plt.plot and plt.scatter

```Claus,

I think you are looking for something in
http://matplotlib.org/api/colors_api.html

-Sterling

On Nov 15, 2012, at 8:24AM, Claus wrote:

> Hi,
> I have this issue, schematically:
>
> import numpy as np
> import matplotlib.pyplot as plt
>
> x = np.linspace(0.0, a, b)
>
> for i in range(d):
> 	y1 = f1(x, p1_i, p2_i)
> 	y2 = f2(x, p1_i, p2_i)
> 	plt.scatter(x, y1, c=color[i])
> 	plt.plot(x, y2, '-', c=color[i]
>
>
> my question:
> how can I setup color to be d colors from some colormap (like cm.copper or cm.jet), they should be somewhat
"equally" spaced… maybe the loop is not ideal, but I don't know a better way (yet)…
>
> Cheers,
```

15 Nov 2012 23:41

### Re: discrete colors from colormap for plt.plot and plt.scatter

Claus,

I agree with Sterling that the colors api page has a great deal of useful info. However, as another solution to your problem, keep in mind that the predefined colormaps contained in matplotlib.pyplot.cm return color tuples when called with a float between 0 and 1. To illustrate with an extension of your example code, try the following:

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,25) # Your x values
# A list of parameters for generating the y values
p = np.linspace(1,10,5)
# An array of values between 0 and 1 with the same length as your parameter list.
d = np.linspace(0, 1, 5)
for i,j in zip(p,d):
y = np.sin(x*i)
plt.scatter(x, y, color=plt.cm.copper(j))
plt.plot(x, y, color=plt.cm.jet(j))
plt.show()

If you have a lot of parameters, hence a large number of plots, you might want to start reading up on collections:
http://matplotlib.org/api/collections_api.html
My understanding is that collections plot faster than many repeated calls to plt.plot or plt.scatter. I've used LineCollection to plot a large number of lines: I don't know which collection to use for repeated scatter plots, though.

Good luck

Ryan

On Thu, Nov 15, 2012 at 12:49 PM, Sterling Smith wrote:
Claus,

I think you are looking for something in
http://matplotlib.org/api/colors_api.html

-Sterling

On Nov 15, 2012, at 8:24AM, Claus wrote:

> Hi,
> I have this issue, schematically:
>
> import numpy as np
> import matplotlib.pyplot as plt
>
> x = np.linspace(0.0, a, b)
>
> for i in range(d):
>       y1 = f1(x, p1_i, p2_i)
>       y2 = f2(x, p1_i, p2_i)
>       plt.scatter(x, y1, c=color[i])
>       plt.plot(x, y2, '-', c=color[i]
>
>
> my question:
> how can I setup color to be d colors from some colormap (like cm.copper or cm.jet), they should be somewhat "equally" spaced… maybe the loop is not ideal, but I don't know a better way (yet)…
>
> Cheers,
> Claus
>
>
>
> ------------------------------------------------------------------------------
> Monitor your physical, virtual and cloud infrastructure from a single
> web console. Get in-depth insight into apps, servers, databases, vmware,
> Pricing starts from \$795 for 25 servers or applications!
> http://p.sf.net/sfu/zoho_dev2dev_nov
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users

------------------------------------------------------------------------------
Monitor your physical, virtual and cloud infrastructure from a single
web console. Get in-depth insight into apps, servers, databases, vmware,
Pricing starts from \$795 for 25 servers or applications!
http://p.sf.net/sfu/zoho_dev2dev_nov
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

```------------------------------------------------------------------------------
Monitor your physical, virtual and cloud infrastructure from a single
web console. Get in-depth insight into apps, servers, databases, vmware,
```_______________________________________________