Add colormap= argument to DataFrame plotting methods by qwhelan · Pull Request #3860 · pandas-dev/pandas (original) (raw)

I frequently plot DataFrames with a large number of columns and generally have difficulty distinguishing series due to the short cycle length of the default color scheme.

Especially in cases where the ordering of columns has significant information, the ideal way to color the series would be with a matplotlib colormap that uniformly spaces colors. This is pretty straightforward with pyplot, but pretty annoying to have to repeatedly do.

This patch modifies DataFrame plotting functions to take a colormap= argument consisting of either a str name of a matplotlib colormap or a colormap object itself.

df.cumsum().plot(colormap='jet', figsize=(10,5))

jet_10

KDE plot:

df.plot(kind='kde', colormap='jet', figsize=(10,5))

kde

Some colormaps don't work as well on a white background (the 0 column is white):
df.cumsum().plot(colormap=cm.Greens, figsize=(10,5))
greens_10

But work better for other graph types:
df.plot(kind='bar', colormap='jet', figsize=(10,5))
greens_bar

Parallel coordinates on the iris dataset:

parallel_coordinates(iris, 'Name', colormap='gist_rainbow')

iris_parallel

Andrews curves (I'd appreciate someone double checking this one; don't think I have it quite right):

andrews_curves(iris, 'Name', colormap='winter')

andrews_winter

I've included some test coverage and unified all the color creation code into one method _get_standard_colors(). I started adding to the documentation but ran into a weird issue with the sphinx plot output. When adding this to visualization.rst:

.. ipython:: python

   from matplotlib import cm

   df = DataFrame(randn(1000, 4), index=ts.index, columns=list('ABCD'))
   df = df.cumsum()

   plt.figure()

   @savefig greens.png width=6in
   df.plot(colormap=cm.Greens)

I get this output (the lines should be white->green):
greens

My first thought was that it was the options.display.mpl_style = 'default', but plots render fine in IPython with this setting. My guess is something in @savefig, but is anyone familiar with what might be happening here?