I am playing a bit with Python and its matplotlib
library, how can I create the following chart so that the first slice starts from the top and goes to the right (clockwise) instead of going to the left (counter clockwise):
Code:
import matplotlib.pyplot as plt
import re
import math
# The slices will be ordered and plotted counter-clockwise if startangle=90.
sizes = [175, 50, 25, 50]
total = sum(sizes)
print('TOTAL:')
print(total)
print('')
percentages = list(map(lambda x: str((x/(total * 1.00)) * 100) + '%', sizes))
print('PERCENTAGES:')
print(percentages)
backToFloat = list(map(lambda x: float(re.sub("%$", "", x)), percentages))
print('')
print('PERCENTAGES BACK TO FLOAT:')
print(backToFloat)
print('')
print('SUM OF PERCENTAGES')
print(str(sum(backToFloat)))
print('')
labels = percentages
colors = ['blue', 'red', 'green', 'orange']
patches, texts = plt.pie(sizes, colors=colors, startangle=-270)
plt.legend(patches, labels, loc="best")
# Set aspect ratio to be equal so that pie is drawn as a circle.
plt.axis('equal')
plt.tight_layout()
plt.show()
5) How to rotate a pie chart? By default, the first slice of the pie starts on the positive X-axis. If you want your first slice (wedge) to start at a different place, specify it using the property “startangle”. This angle is used to rotate the pie chart counter clock wise from the default start position.
The Pie chart actually works anti clockwise.
Note about the custom start angle: The default startangle is 0, which would start the "Frogs" slice on the positive x-axis. This example sets startangle = 90 such that everything is rotated counter-clockwise by 90 degrees, and the frog slice starts on the positive y-axis.
autopct enables you to display the percent value using Python string formatting. For example, if autopct='%. 2f' , then for each pie wedge, the format string is '%. 2f' and the numerical percent value for that wedge is pct , so the wedge label is set to the string '%.
How to set the direction of pie chart in Python Matplotlib? To specify fractions direction of the pie chart, you must set the counterclock parameter to True or False (value is True by default). ? plt.pie (sizes, labels=labels, counterclock=False, startangle=90)
Nested pies are a form of the pie chart that is a module variation of our normal pie chart. First import matplotlib.pyplot library. Next define outer labels, inner labels, outer size, inner size, inner color, and outer color of the pie slices.
We’ll increase the size of the pie chart by increasing a size of a figure. Here x and y represent width and height respectively. To increase the size of the pie chart, we pass figsize parameter to the figure () method of pyplot. Here we’ll see an example of a pie chart with labels inside the slices.
By default the plotting of the first wedge starts from the x-axis and move counterclockwise: Note: The size of each wedge is determined by comparing the value with all the other values, by using this formula: Add labels to the pie chart with the label parameter.
To specify fractions direction of the pie chart, you must set the counterclock
parameter to True
or False
(value is True
by default). For your need, you must replace:
patches, texts = plt.pie(sizes, colors=colors, startangle=-270)
with:
patches, texts = plt.pie(sizes, counterclock=False, colors=colors, startangle=-270)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With