Python matplotlib pie chart

matplotlib.pyplot.pie#

matplotlib.pyplot. pie ( x , explode = None , labels = None , colors = None , autopct = None , pctdistance = 0.6 , shadow = False , labeldistance = 1.1 , startangle = 0 , radius = 1 , counterclock = True , wedgeprops = None , textprops = None , center = (0, 0) , frame = False , rotatelabels = False , * , normalize = True , hatch = None , data = None ) [source] #

Make a pie chart of array x. The fractional area of each wedge is given by x/sum(x) .

The wedges are plotted counterclockwise, by default starting from the x-axis.

Parameters : x 1D array-like

explode array-like, default: None

If not None, is a len(x) array which specifies the fraction of the radius with which to offset each wedge.

labels list, default: None

A sequence of strings providing the labels for each wedge

colors array-like, default: None

A sequence of colors through which the pie chart will cycle. If None, will use the colors in the currently active cycle.

hatch str or list, default: None

Hatching pattern applied to all pie wedges or sequence of patterns through which the chart will cycle. For a list of valid patterns, see Hatch style reference .

If not None, autopct is a string or function used to label the wedges with their numeric value. The label will be placed inside the wedge. If autopct is a format string, the label will be fmt % pct . If autopct is a function, then it will be called.

pctdistance float, default: 0.6

The relative distance along the radius at which the text generated by autopct is drawn. To draw the text outside the pie, set pctdistance > 1. This parameter is ignored if autopct is None .

labeldistance float or None, default: 1.1

The relative distance along the radius at which the labels are drawn. To draw the labels inside the pie, set labeldistance < 1. If set to None , labels are not drawn but are still stored for use in legend .

shadow bool, default: False

Draw a shadow beneath the pie.

startangle float, default: 0 degrees

The angle by which the start of the pie is rotated, counterclockwise from the x-axis.

radius float, default: 1

counterclock bool, default: True

Specify fractions direction, clockwise or counterclockwise.

wedgeprops dict, default: None

Dict of arguments passed to each patches.Wedge of the pie. For example, wedgeprops = sets the width of the wedge border lines equal to 3. By default, clip_on=False . When there is a conflict between these properties and other keywords, properties passed to wedgeprops take precedence.

Читайте также:  Рамка вокруг таблицы

textprops dict, default: None

Dict of arguments to pass to the text objects.

center (float, float), default: (0, 0)

The coordinates of the center of the chart.

frame bool, default: False

Plot Axes frame with the chart if true.

rotatelabels bool, default: False

Rotate each label to the angle of the corresponding slice if true.

normalize bool, default: True

When True, always make a full pie by normalizing x so that sum(x) == 1 . False makes a partial pie if sum(x) 1 .

data indexable object, optional

If given, the following parameters also accept a string s , which is interpreted as data[s] (unless this raises an exception):

Returns : patches list

texts list

A list of the label Text instances.

autotexts list

A list of Text instances for the numeric labels. This will only be returned if the parameter autopct is not None.

The pie chart will probably look best if the figure and Axes are square, or the Axes aspect is equal. This method sets the aspect ratio of the axis to «equal». The Axes aspect ratio can be controlled with Axes.set_aspect .

Источник

Pie Charts with Labels in Matplotlib

 Profile Image

Pie charts don’t have the best reputation in the visualization community, but there are times when you want one anyway.

Pie charts can be useful when utilized in the right context with the right data. So we’ll go over how to code them up in Matplotlib, which happens to be pretty straighforward.

The axes Matplotlib object has a baked in pie method, as does the higher level pyplot library. We’ll use the former in our examples but any of these could be coded up as the latter instead.

The pie method takes an x parameter as its first argument, which is the values that will make up the wedges of the pie. They can be given as decimals (percentages of total) or raw values, it doesn’t really matter; Matplotlib will convert them to percentages regardless. The one caveat here is that if the sum(x) < 1 , the wedges won't automatically fill in the full pie. We'll give an example of this below.

Читайте также:  Php последний день текущего месяца

The labels argument should be an iterable of the same length and order of x that gives labels for each pie wedge.

For our example, let’s say we want to show which sports are most popular at a given school by looking at the number of kids that play each.

import matplotlib.pyplot as plt x = [10, 50, 30, 20] labels = ['Surfing', 'Soccer', 'Baseball', 'Lacrosse'] fig, ax = plt.subplots() ax.pie(x, labels=labels) ax.set_title('Sport Popularity') plt.tight_layout() 

matplotlib basic pie chart

Matplotlib uses the default color cycler to color each wedge and automatically orders the wedges and plots them counter-clockwise.

Let’s make the pie a bit bigger just by increasing figsize and also use the autopct argument to show the percent value inside each piece of the pie. The autopct arg takes either a string format or a function that can transform each value.

fig, ax = plt.subplots(figsize=(6, 6)) ax.pie(x, labels=labels, autopct='%.1f%%') ax.set_title('Sport Popularity') plt.tight_layout() 

matplotlib pie chart with autopct

As explained above, if we switch the values to be decimals and their sum doesn’t equal one, the pie will have a gap or blank wedge.

fig, ax = plt.subplots(figsize=(6, 6)) x = [0.1, 0.25, 0.15, 0.2] ax.pie(x, labels=labels, autopct='%.1f%%') ax.set_title('Sport Popularity') plt.tight_layout() 

matplotlib pie chart with blank wedge

Styling the Pie Chart

You can use the wedgeprops and textprops arguments to style the wedges and texts, respectively.

Here, we add a wider border around each wedge and color it white; we also increase the text size to be more readable.

fig, ax = plt.subplots(figsize=(6, 6)) x = [10, 50, 30, 20] ax.pie(x, labels=labels, autopct='%.1f%%', wedgeprops='linewidth': 3.0, 'edgecolor': 'white'>, textprops='size': 'x-large'>) ax.set_title('Sport Popularity', fontsize=18) plt.tight_layout() 

matplotlib pie chart with wedge text styling

But what if you only want to style, say, the labels? If you only want to style some of the text, you’ll have to use the return values from pie . There are three return values:

fig, ax = plt.subplots(figsize=(6, 6)) # Capture each of the return elements. patches, texts, pcts = ax.pie( x, labels=labels, autopct='%.1f%%', wedgeprops='linewidth': 3.0, 'edgecolor': 'white'>, textprops='size': 'x-large'>) # Style just the percent values. plt.setp(pcts, color='white', fontweight='bold') ax.set_title('Sport Popularity', fontsize=18) plt.tight_layout() 

matplotlib pie chart with text label styling

Now that we know this, we can do cool things like have the text labels match the color of the wedge they correspond to.

fig, ax = plt.subplots(figsize=(6, 6)) patches, texts, pcts = ax.pie( x, labels=labels, autopct='%.1f%%', wedgeprops='linewidth': 3.0, 'edgecolor': 'white'>, textprops='size': 'x-large'>, startangle=90) # For each wedge, set the corresponding text label color to the wedge's # face color. for i, patch in enumerate(patches): texts[i].set_color(patch.get_facecolor()) plt.setp(pcts, color='white') plt.setp(texts, fontweight=600) ax.set_title('Sport Popularity', fontsize=18) plt.tight_layout() 

matplotlib pie chart with text color

Lastly, let’s change the colors of the pie wedges, highlighting one wedge in particular with a brighter color. Let’s also use the explode parameter to shift the Lacrosse wedge out a bit to highlight it from the rest.

import numpy as np fig, ax = plt.subplots(figsize=(6, 6)) # Get four different grey colors. cmap = plt.get_cmap('Greys') colors = list(cmap(np.linspace(0.45, 0.85, len(x)))) # Swap in a bright blue for the Lacrosse color. colors[3] = 'dodgerblue' # You could also just manually assign colors very easily. # colors = ['purple', 'black', 'pink', 'aqua'] patches, texts, pcts = ax.pie( x, labels=labels, autopct='%.1f%%', wedgeprops='linewidth': 3.0, 'edgecolor': 'white'>, textprops='size': 'x-large'>, startangle=90, colors=colors, # "Explode" the Lacrosse wedge to highlight it. explode=(0, 0, 0, 0.1)) for i, patch in enumerate(patches): # You could also do something like this if you want to assign colors # by some rule or by value. # if texts[i].get_text() == 'Lacrosse': # patch.set_facecolor('dodgerblue') texts[i].set_color(patch.get_facecolor()) plt.setp(pcts, color='white') plt.setp(texts, fontweight=600) ax.set_title('Sport Popularity', fontsize=18) plt.tight_layout() 

matplotlib pie chart with explode and custom colors

Hopefully that helps get you started creating pie charts in Matplotlib. There are many other cool things you can do with pie (and donut) charts in Matplotlib, but we’ll get to those in a follow-up post.

Источник

Оцените статью