contribute to gallery? Or, just advice on changing colors automagically....

Just curious if you're interested in folks contributing to the gallery. I was
playing around trying to come up with a routine to automatically choose
colors when plotting several datasets, not necessarily from a single array,
but rather say iterating through a list of datasets. I came up with the
following... maybe it's of interest? And certainly of interest to me... any
advice on what could be done better!

Thanks!

···

#!/usr/bin/env python

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.colors as colors

"""A script to demonstrate automatically assigning colors based
on the number of x,y pairs to be plotted. """

# First example
# set up some example data
x = np.random.random((430,23))

# This is the important part for 'autocoloring'
# get an array of 0-1 values, length of numint (#data sets
# that you will iterate through), these will define the colors
numint = x.shape[1]
Nc = np.array([float(i)/numint for i in range(numint)])
norm = colors.normalize(Nc.min(),Nc.max())

fig = plt.figure()
ax = fig.add_subplot(111)
interval = 0
for i in range(numint):
    #get a new color
    cmap = cm.jet(norm(Nc[i]))
    ax.scatter(x[:,0],x[:,i],color=cmap)

# Second example
# something a little more interesting
fig2 = plt.figure()
ax2 = fig2.add_subplot(111)
X = np.arange(400)
y = np.sin(X)
y2 = X*.2
x = np.column_stack((y,y2))

#define an interval, the dataset is divided by this value
intervalsize = 23
numint = int(np.round(x.shape[0]/intervalsize)) + 1

# This is the important part for 'autocoloring'
# get an array of 0-1 values, length of numint
# these will define the colors
Nc = np.array([float(i)/numint for i in range(numint)])
norm = colors.normalize(Nc.min(),Nc.max())

interval = 0
for i in range(0,len(x),intervalsize):
    # define the index array (easier than typing)
    indx = np.arange(i,i+intervalsize)
    #get a new color
    cmap = cm.jet(norm(Nc[interval]))
    # the indx as defined above may exceed
    # the data array
    try:
        ax2.scatter(x[indx,0],x[indx,1],color=cmap)
        #print indx
    # case to handle tail of data
    except:
        #plt.scatter(x[i:,0],x[i:,1],color=cmap)
        print 'OOPS, index exceeds dimensions:',indx
        pass
    # so that you don't miss the last interval
    if len(x)-i < intervalsize:
        ax2.scatter(x[i+1:,0],x[i+1:,1],color=cmap)
        print 'last bits...'
    interval+=1

plt.show()


View this message in context: http://www.nabble.com/contribute-to-gallery--Or%2C-just-advice-on-changing-colors-automagically…-tp24419101p24419101.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Hi, I may be wrong, but arent these already examples of what you trying to
show here?:

http://matplotlib.sourceforge.net/examples/pylab_examples/line_collection2.html
http://matplotlib.sourceforge.net/examples/pylab_examples/ellipse_collection.html

if you use a collection you can quickly setup the colors of all your
elements..by just passing an array....

jimmy

John [H2O] wrote:

···

Just curious if you're interested in folks contributing to the gallery. I
was playing around trying to come up with a routine to automatically
choose colors when plotting several datasets, not necessarily from a
single array, but rather say iterating through a list of datasets. I came
up with the following... maybe it's of interest? And certainly of
interest to me... any advice on what could be done better!

Thanks!

#!/usr/bin/env python

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.colors as colors

"""A script to demonstrate automatically assigning colors based
on the number of x,y pairs to be plotted. """

# First example
# set up some example data
x = np.random.random((430,23))

# This is the important part for 'autocoloring'
# get an array of 0-1 values, length of numint (#data sets
# that you will iterate through), these will define the colors
numint = x.shape[1]
Nc = np.array([float(i)/numint for i in range(numint)])
norm = colors.normalize(Nc.min(),Nc.max())

fig = plt.figure()
ax = fig.add_subplot(111)
interval = 0
for i in range(numint):
    #get a new color
    cmap = cm.jet(norm(Nc[i]))
    ax.scatter(x[:,0],x[:,i],color=cmap)

# Second example
# something a little more interesting
fig2 = plt.figure()
ax2 = fig2.add_subplot(111)
X = np.arange(400)
y = np.sin(X)
y2 = X*.2
x = np.column_stack((y,y2))

#define an interval, the dataset is divided by this value
intervalsize = 23
numint = int(np.round(x.shape[0]/intervalsize)) + 1

# This is the important part for 'autocoloring'
# get an array of 0-1 values, length of numint
# these will define the colors
Nc = np.array([float(i)/numint for i in range(numint)])
norm = colors.normalize(Nc.min(),Nc.max())

interval = 0
for i in range(0,len(x),intervalsize):
    # define the index array (easier than typing)
    indx = np.arange(i,i+intervalsize)
    #get a new color
    cmap = cm.jet(norm(Nc[interval]))
    # the indx as defined above may exceed
    # the data array
    try:
        ax2.scatter(x[indx,0],x[indx,1],color=cmap)
        #print indx
    # case to handle tail of data
    except:
        #plt.scatter(x[i:,0],x[i:,1],color=cmap)
        print 'OOPS, index exceeds dimensions:',indx
        pass
    # so that you don't miss the last interval
    if len(x)-i < intervalsize:
        ax2.scatter(x[i+1:,0],x[i+1:,1],color=cmap)
        print 'last bits...'
    interval+=1

plt.show()


View this message in context: http://www.nabble.com/contribute-to-gallery--Or%2C-just-advice-on-changing-colors-automagically…-tp24419101p24427289.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

vehemental wrote:

Hi, I may be wrong, but arent these already examples of what you trying to
show here?:

http://matplotlib.sourceforge.net/examples/pylab_examples/line_collection2.html
http://matplotlib.sourceforge.net/examples/pylab_examples/ellipse_collection.html

if you use a collection you can quickly setup the colors of all your
elements..by just passing an array....

jimmy

"same same, but different"...

I guess here one would have to assume you're only plotting lines? Or only
plotting ellipses, but say for example you had different types of plots you
would make, and just need a set of collections.. say you wanted to mix
scatter plots with line plots.

The point is, yes, you're correct, I think this basically does do what I'm
talking about, but having a way to create a 'generic' set of colors that can
be indexed I think is nice. There may be a more 'matplotlibish' way to do
it, I just didn't find it.

Seem ultimately there should be something like:

from matplotlib.colors import colorset

where colorset could just be defined with an integer as the number of
colors, and perhaps a cmap as an option... Maybe it's there?!?! I just
haven't found it.

My sudo code would be:

plotcolors = colorset(10,cmap=cm.jet)

for i in range(len(dataset)):
    x,y = dataset[i]
    plot(x,y, plotcolors[i])

... or something. **Note: I realize this isn't the best example, as
plot(X,Y) would do it automagically...

···


View this message in context: http://www.nabble.com/contribute-to-gallery--Or%2C-just-advice-on-changing-colors-automagically…-tp24419101p24427781.html
Sent from the matplotlib - users mailing list archive at Nabble.com.