Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the colormap of a pie chart?

Tags:

python

pandas

Simple question here, I am creating a pie chart in pandas for the value counts of a dataset's regional spread:

df['region'].value_counts().plot.pie

I want to use the colormap "cool" to color this so it matches my other visualizations, is this possible or am I oversimplifying? Do I need to assign a color to each region or can I somehow just add a colormap='cool' somewhere in here? New to visualizations here...

like image 292
LA 212 Avatar asked Oct 26 '25 13:10

LA 212


2 Answers

When not plotting straight from pandas, pie uses the colors argument, which takes a list of color codes.

You can retrieve color codes from some matplotlib colormaps using plt.cm.desired_colormap_name.colors

For example:

import matplotlib.pyplot as plt
import pandas as pd

s = pd.Series([1,2,3,4,5])
plt.pie(s, colors=plt.cm.Pastel2.colors)
plt.show()

pie chart example

Colormap names here: https://matplotlib.org/stable/tutorials/colors/colormaps.html

like image 92
Danilo Avatar answered Oct 29 '25 07:10

Danilo


You can pass the argument cmap='cool' to the .pie method. Here's an example:

import matplotlib.pyplot as plt
import pandas as pd

s = pd.Series([1,2,3,4,5])
s.plot.pie(cmap='cool')
plt.show()

enter image description here

like image 20
Derek O Avatar answered Oct 29 '25 06:10

Derek O



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!