Pyplot colormap line by line
Question: I'm beginning with plotting on python using the very nice pyplot. I aim at
showing the evolution of two series of data along time. Instead of doing a
casual plot of data function of time, I'd like to have a scatter plot
(data1,data2) where the time component is shown as a color gradient.

In my two column file, the time would be described by the line number. Either
written as a 3rd column in the file either using the intrinsic capability of
pyplot to get the line number on its own.

Can anyone help me in doing that ?

Thanks a lot.

Nicolas


Answer: When plotting using
[`matplotlib.pyplot.scatter`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.scatter)
you can pass a third array via the keyword argument `c`. This array can choose
the colors that you want your scatter points to be. You then also pick an
appropriate colormap from
[`matplotlib.cm`](http://matplotlib.org/api/cm_api.html) and assign that with
the `cmap` keyword argument.

This toy example creates two datasets `data1` and `data2`. It then also
creates an array `colors`, an array of continual values equally spaced between
0 and 1, and with the same length as `data1` and `data2`. It doesn't need to
know the "line number", it just needs to know the total number of data points,
and then equally spaces the colors.

I've also added a `colorbar`. You can remove this by removing the
`plt.colorbar()` line.

    
    
    import matplotlib.pyplot as plt
    from matplotlib import cm
    import numpy as np
    
    
    N = 500
    data1 = np.random.randn(N)
    data2 = np.random.randn(N)
    colors = np.linspace(0,1,N)
    
    plt.scatter(data1, data2, c=colors, cmap=cm.Blues)
    
    plt.colorbar()
    
    plt.show()
    

![Example plot](http://i.stack.imgur.com/UdAnc.png)

