'colors and title in Pie charts

I have such code now:
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10,6))
ax = fig.add_subplot()
a = dataset['PassengerId'][dataset['Survived'] == 0].count()
b = dataset['PassengerId'][dataset['Survived'] == 1].count()
labels = [0,1]
vals = [a,b]
exp = (0.1, 0.0)
ax.pie(vals, labels=labels, autopct='%.1f', explode=exp, shadow="True")
I need to set a title and change colors on both axes(as on example) and set percents for values on each wedge i dont know how to do this
I was trying to find videos which could help me
Solution 1:[1]
To set the title:
ax.set_title('Survived')
Add %% to your format string (resulting in %.1f%%) that you're using as the autopct argument to add percent signs to labels:
ax.pie(vals, labels=labels, autopct='%.1f%%', explode=exp, shadow="True")
(The double %% is an "escaped" %)
I'm not sure what you were looking for in terms of colors, as the standard color cycle seems close to what you're looking for, but you can use the colors keyword argument to specify colors.
For example, from the image you posted:
colors=['#4c72b0', '#dd8452']
Full code:
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10,6))
ax = fig.add_subplot()
a = 616
b = 384
labels = [0,1]
vals = [a,b]
exp = (0.1, 0.0)
ax.pie(
vals,
labels = labels,
autopct = '%.1f%%',
explode = exp,
shadow = "True",
colors = ['#4c72b0', '#dd8452'],
)
ax.set_title('Survived')
plt.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

