Pandas colormap with groupby

pandas, python

Solution

It sort of does work, but because you're plotting from a `GroupBy`, each group (containing 1 column) gets plotted after each other, but on the same axes. This single column gets the first color from the selected `colormap`.

To get the colormap to work, you need multiple columns, then each column gets a different color from the colormap.

You could move 'col1' and 'col2' to the index and then unstack them. This assumes you only have one (col1, col2) combination per timestamp. For your original `df`:

df.set_index(['col1', 'col2'], append=True, inplace=True)
df.unstack(['col1', 'col2']).xs('value', axis=1).plot(colormap='jet')

Alternatively you could modify Matplotlibs `color cycle` with your selected colormap. Then your first plot would get the colors from the colormap. See: http://matplotlib.org/1.2.1/examples/api/color_cycle.html

edit:

Using `pivot` is probably more appropriate then the unstacking as shown above:

df = pd.pivot_table(df.reset_index(),values='value', 
                    rows=['time'],cols=['col1', 'col2'])

df.plot(colormap='jet')

Problem

Pretty new to pandas and matplotlib and having trouble getting colormaps to work when using groupby. Here is my test; ``` x=[] for i in range(5): for j in range(9): x.append({'time':datetime(2013,1,1+i), 'col1':chr(ord('A')+j), 'col2':chr(ord('Z')-j), 'value':100+i*j}) df=pd.DataFrame(x) df=df.set_index('time') df ``` Builds this dataset; ``` col1 col2 value time 2013-01-01 A Z 100 2013-01-01 B Y 100 2013-01-01 C X 100 2013-01-01 D W 100 2013-01-01 E V 100 2013-01-01 F U 100 2013-01-01 G T 100 2013-01-01 H S 100 2013-01-01 I R 100 2013-01-02 A Z 100 2013-01-02 B Y 101 2013-01-02 C X 102 2013-01-02 D W 103 2013-01-02 E V 104 2013-01-02 F U 105 2013-01-02 G T 106 2013-01-02 H S 107 2013-01-02 I R 108 2013-01-03 A Z 100 2013-01-03 B Y 102 2013-01-03 C X 104 2013-01-03 D W 106 2013-01-03 E V 108 2013-01-03 F U 110 2013-01-03 G T 112 2013-01-03 H S 114 2013-01-03 I R 116 2013-01-04 A Z 100 2013-01-04 B Y 103 2013-01-04 C X 106 2013-01-04 D W 109 2013-01-04 E V 112 2013-01-04 F U 115 2013-01-04 G T 118 2013-01-04 H S 121 2013-01-04 I R 124 2013-01-05 A Z 100 2013-01-05 B Y 104 2013-01-05 C X 108 2013-01-05 D W 112 2013-01-05 E V 116 2013-01-05 F U 120 2013-01-05 G T 124 2013-01-05 H S 128 2013-01-05 I R 132 ``` If I plot it as normal the last few items are the same colour; ``` df.groupby(['col1','col2'])['value'].plot() plt.legend() ``` http://postimg.org/image/cta2pa76f/ If I try a colourmap it doesn't seem to work; ``` df.groupby(['col1','col2'])['value'].plot(colormap='jet') plt.legend() ``` http://postimg.org/image/8y6ompo0n/ If I try `'Blues'` it's even worse with all white lines on a white background. Any help appreciated!

Original source