Take the 2-minute tour ×

One of my favorite aspects of using the ggplot2 library in R is the ability to easily specify aesthetics. I can quickly make a scatterplot and apply color associated with a specific column and I would love to be able to do this with python/pandas/matplotlib. I'm wondering if there are there any convenience functions that people use to map colors to values using pandas dataframes and Matplotlib?

##ggplot scatterplot example with R dataframe, `df`, colored by col3
ggplot(data = df, aes(x=col1, y=col2, color=col3)) + geom_point()

##ideal situation with pandas dataframe, 'df', where colors are chosen by col3

EDIT: Thank you for your responses but I want to include a sample dataframe to clarify what I am asking. Two columns contain numerical data and the third is a categorical variable. The script I am thinking of will assign colors based on this value.

import pandas as pd
df = pd.DataFrame({'Height':np.random.normal(10),
                   'Gender': ["Male","Male","Male","Male","Male",
share|improve this question

2 Answers 2

up vote 7 down vote accepted

In this case, I would use matplotlib directly.

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

def dfScatter(df, xcol='Height', ycol='Weight', catcol='Gender'):
    fig, ax = plt.subplots()
    categories = np.unique(df[catcol])
    colors = np.linspace(0, 1, len(categories))
    colordict = dict(zip(categories, colors))  

    df["Color"] = df[catcol].apply(lambda x: colordict[x])
    ax.scatter(df[xcol], df[ycol], c=df.Color)
    return fig

if 1:
    df = pd.DataFrame({'Height':np.random.normal(size=10),
                       'Gender': ["Male","Male","Unknown","Male","Male",
                                  "Female","Did not respond","Unknown","Female","Female"]})    
    fig = dfScatter(df)

And that gives me:

scalle plot with categorized colors As far as I know, that color column can be any matplotlib compatible color (RBGA tuples, HTML names, hex values, etc).

update: I'm having trouble getting anything but numerical values to work with the colors.

share|improve this answer
Thanks Paul. This is excellent but I wasn't clear enough in my question. The third column, 'C', is a categorical variable. I have updated the question to reflect that. –  zach Feb 15 '13 at 22:27
@zach understood. see my updated example. –  Paul H Feb 15 '13 at 22:57
Great update; I gave you the answer but you get extra points if you rephrase your answer in the form of a single function that takes a dataframe , the names of the X-Y columns, and the categorical column and plots them with out having to manually specify a dictionary as in your answer. –  zach Feb 17 '13 at 23:17
@zach does that suit your needs? does constructing the dictionary automatically count? –  Paul H Feb 18 '13 at 7:09
thats awesome. The automatic dictionary construction will definitely do. One extra goodie that would be particularly useful would be to choose a matplotlib color scheme. That would be cool but would be beyond the scope of the original question. Thanks for your time/answer. –  zach Feb 18 '13 at 16:02

You can use the color parameter to the plot method to define the colors you want for each column. For example:

from pandas import DataFrame
data = DataFrame({'a':range(5),'b':range(1,6),'c':range(2,7)})
colors = ['yellowgreen','cyan','magenta']

Three lines with custom colors

You can use color names or Color hex codes like '#000000' for black say. You can find all the defined color names in matplotlib's color.py file. Below is the link for the color.py file in matplotlib's github repo.


share|improve this answer
I've updated the question. I want the script to see a categorical variable and assign per-sample color based on the value of that variable. –  zach Feb 15 '13 at 22:28

Your Answer


