mercredi 29 juin 2016

Coloring by Group using Scatter plot and Pandas Groupby [duplicate]


This question already has an answer here:

I would like to use pandas to group by a category, then color by that category on a scatter plot. I'd like to use a pandas object for the plotting because I'm using pandas columns for marker size, shape etc.

When I plot this using the pandas.plot function, I'm struggling to see how to color by group (in this example, by "cat"). The method I'm using below does not work when there are three items in a category, as then it interprets the color as an array of black-white values. Therefore the below plot is not what I want, I want it to be one color for CatA, and one color for catB.

Thanks for any help!

import pandas as pd
import pylab as plt
import seaborn as sns
import numpy as np

d = {'cat' : pd.Series(['catA', 'catA', 'catA', 'catB']),
    'y' : pd.Series([100., 52., 33., 4.]),
    'x': pd.Series([1., 2., 3., 4.]),
    'markersize' : pd.Series([50., 400., 600., 1600.]) }
df = pd.DataFrame(d)


colorlist = sns.husl_palette(len(df.groupby(df.cat)),l=.3, s=.8)

fig=plt.figure()
ax=fig.gca()
for i, (group, dataframe) in enumerate(df.groupby('cat')):
    ax=dataframe.plot(kind='scatter', x='x', y='y', ax=ax, color=colorlist[i], label=group, s=dataframe.markersize)

scatter


Aucun commentaire:

Enregistrer un commentaire