I have this data frame diamonds
which is composed of variables like (carat, price, color)
, and I want to draw a scatter plot of price
to carat
for each color
, which means different color
has different color in the plot.
This is easy in R
with ggplot
:
ggplot(aes(x=carat, y=price, color=color), #by setting color=color, ggplot automatically draw in different colors
data=diamonds) + geom_point(stat='summary', fun.y=median)
I wonder how could this be done in Python using matplotlib
?
PS:
I know about auxiliary plotting packages, such as seaborn
and ggplot for python
, and I donot prefer them, just want to find out if it is possible to do the job using matplotlib
alone, ;P
Here's a succinct and generic solution to use a seaborn color palette.
First find a color palette you like and optionally visualize it:
Then you can use it with
matplotlib
doing this:You can pass
plt.scatter
ac
argument which will allow you to select the colors. The code below defines acolors
dictionary to map your diamond colors to the plotting colors.df['color'].apply(lambda x: colors[x])
effectively maps the colours from "diamond" to "plotting".(Forgive me for not putting another example image up, I think 2 is enough :P)
With
seaborn
You can use
seaborn
which is a wrapper aroundmatplotlib
that makes it look prettier by default (rather opinion-based, I know :P) but also adds some plotting functions.For this you could use
seaborn.lmplot
withfit_reg=False
(which prevents it from automatically doing some regression).The code below uses an example dataset. By selecting
hue='color'
you tell seaborn to split your dataframe up based on your colours and then plot each one.Without
seaborn
usingpandas.groupby
If you don't want to use seaborn then you can use
pandas.groupby
to get the colors alone and then plot them using just matplotlib, but you'll have to manually assign colors as you go, I've added an example below:This code assumes the same DataFrame as above and then groups it based on
color
. It then iterates over these groups, plotting for each one. To select a color I've created acolors
dictionary which can map the diamond color (for instanceD
) to a real color (for instancered
).Here a combination of markers and colors from a qualitative colormap in
matplotlib
:Using Altair.