Book Image

Sage Beginner's Guide

By : Craig Finch
1 (1)
Book Image

Sage Beginner's Guide

1 (1)
By: Craig Finch

Overview of this book

Table of Contents (17 chapters)
Sage Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – making a pie chart


matplotlib can also make business graphics that are more commonly associated with spreadsheets. Let's try a pie chart:

import numpy
import matplotlib.pyplot as plt

data = [1.0, 10.0, 20.0, 30.0, 40.0]
explode = numpy.zeros(len(data))
explode[3] = 0.1

plt.figure(figsize=(4, 4))
plt.pie(data, explode=explode, labels=['a', 'b', 'c', 'd', 'e'])
plt.title('Revenue sources')

plt.savefig('Pie_chart.png')
plt.close()

The plot should look like this:

What just happened?

We made a pie chart. The pie function in matplotlib only requires one argument, which is a list of numbers that indicate the relative size of the slices. The explode option takes a list of numbers that show how far to offset each piece of the pie from the centre. In this case, we created an array of zeros and set the fourth element to 0.1, which offset the fourth slice of the pie. We used the keyword explode to pass this array to the pie function. We used the labels keyword to pass a list of...