Issue
I would like to change the label colors for each part of the pie chart in accordance with the color of the section. I find questions doing it with matplotlib (see Different colors for each label in my pie chart). In my case I would like to do it using the creation of the plot with pandas. Is there a way to create the pie chart with pandas and then access the text of each label to change the color? Below is an example of my simplified code for which I would like to add a part modifying the colors as it is done in the link above.
index_data = ["AAA", "BBB", "CCC", "DDD"]
data = [10,20,20,40]
df = pd.DataFrame(data=data,index=index_data,columns=['col1'])
color_list = ['#FF7800','#73B22D','#5F5D60','#803C00']
fig, ax = plt.subplots(figsize=(9,9))
df.plot(kind='pie', ax=ax, y=df.columns[0], legend=False, label='', colors=color_list, autopct='%1.1f%%', textprops={'fontsize': 17})
# --- I want to change colors here --- #
plt.show()
Do you have any ideas ? Thanks
Solution
You can get axis by assigning df.plot()
to ax
and then get texts
attribute from ax
as below:
index_data = ["AAA", "BBB", "CCC", "DDD"]
data = [10,20,20,40]
df = pd.DataFrame(data=data,index=index_data,columns=['col1'])
color_list = ['#FF7800','#73B22D','#5F5D60','#803C00']
fig, ax = plt.subplots(figsize=(9,9))
ax = df.plot(kind='pie', ax=ax, y=df.columns[0], legend=False, label='', colors=color_list, autopct='%1.1f%%', textprops={'fontsize': 17})
for text, color in zip(ax.texts, color_list):
text.set_color(color)
plt.show()
Answered By - the_pr0blem Answer Checked By - Mary Flores (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.