Search code examples
pythonplotlyplotly-python

How can I change the labels in this pie chart [plotly]?


I want to change the labels [2,3,4,5] from my pie chart and instead have them say [Boomer, Gen X, Gen Y, Gen Z] respectively. I can't seem to find a direct way of doing this without changing the dataframe. Is there any way to do this by working through the code I have?

pie chart

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

data = df.groupby("Q10_Ans")["Q4_Agree"].count()

pie, ax = plt.subplots(figsize=[10,6])
labels = data.keys()
plt.pie(x=data, autopct="%.1f%%", explode=[0.05]*4, labels=labels, pctdistance=0.5)
plt.title("Generations that agree data visualization will help with job prospects", fontsize=14);
pie.savefig("DeliveryPieChart.png")

Solution

  • how about change the code

    labels = data.keys()

    to

    labels = ['Boomer','Gen X','Gen Y','Gen Z']