Pie Charts

In this section we will be looking at Pie charts. Let’s see a small example. Suppose that we want to chart the distribution of active hours for a standard day.

Let’s suppose that 70% of the active hours are taken by work and than the rest of the day is equally split between Programming, Reading and Sport (that is how much I can at the moment).

Let’s have a look at the code below:

import matplotlib.pyplot as plt
l=["Work","Sport","Programming","Reading"]
mytime =[70,10,10,10]
e=(0,0,0.2,0)
fig1,ax1=plt.subplots()
ax1.pie(mytime, explode=e,labels=l,autopct="%1.1f%%",shadow=True,startangle=90)
ax1.axis('equal')
plt.title("Day Distribution")
plt.show()

We have a variable for the Label (l) and a variable with the hours distribution.

The variable “e” is used to indicate MatPlot which slice of the Pie we want to detach from the Pie (in this case we are selecting the Programming element (the third in the list)

The other important part of this small program is the ax1.pie where we insert the :

  • values to plot (mytime variable)
  • we set the explode flag so that we can detach the 3rd element of our list
  • the label (variable l)
  • the format of the data (autopct)
  • shadow paremeter is set to True
  • startangle set to 90

Here is our Pie Chart

Plotting Horizontal Bars

In this post we will review how to plot horizontal bars using MatPlot

Let’s put the code first

import matplotlib.pyplot as plt

items=["Radio","Television","Phones","PlayStation"]
sale_units=[400,100,200,600]

plt.figure(figsize=(10,4))
plt.barh(items,sale_units,color="green",label="Sales by Cat")
plt.xlabel("Sales Units")
plt.ylabel("Sales Category")
plt.text(500,1,"Price Dropped",fontsize=14, color="blue")
plt.text(100,3,"Stock Issue",fontsize=14, color="red")
plt.legend(loc="best")
plt.title("Sales by Category")
plt.show()

As you can see we can establish the size of the figure and adjust the size of the figure we the command plt.figure(figsize=(X,Y))

To draw horizontal bars we use the command plt.barh()

One interesting thing is the possibility to add specific comment to the plot area to facilitate the understanding from the user.

We can achieve that using the command plt.text where we first establish the position of the command, then the text we want to show and then we can format the text by specifying font size and color.

Plotting Vertical Bars

Let’s have a look at how to Plot Vertical Bars using MatPlot Lib

In this example we imagine that we have to represent the sales of 4 categories of items: “Radio”,”Television”,”Phones”,”PlayStation”

We also know that each of this categories has sold respectively : 400,100,200,600 units.

Let’s have a look at the code:

import matplotlib.pyplot as plt
items=["Radio","Television","Phones","PlayStation"]
sale_units=[400,100,200,600]
plt.bar(items,sale_units,color="blue",label="Units by Cat")
plt.title("Sales by Category")
plt.xlabel("Category of Goods")
plt.ylabel("Unit Sales")
plt.legend(loc="best")
plt.show()

As you can see in line 4 we represent categories of item (Axis X) and sale units (Axis Y). We also add that the label that describes better the content of the graph.

As we have defined a label we can now implement a new function (plt.legend). This function needs as a parameter represented by the location. Right now in this example we will tell the program to put the legend into the best location.

Finally we show the graph calling the function plt.show()




MatPlot Library

In this section we will look at how to plot series using Matplot Library.

Let’s suppose that we have a series with the following value : 200,400,600,800,800.

Those values relates to the month 2 (Feb), 4 (Apr), 6 (June), 8 (Aug) and 10 (Oct)

We want to represent the series using MatPlot.

Below the code that we have to write:

import matplotlib.pyplot as plt
plt.plot([2,4,6,8,10],[200,400,600,800,800])
plt.xlabel("Months")
plt.ylabel("Spent USD")
plt.title("Representing a Series of Data")
plt.show()

As you can see we define a plot which will contain the X and Y respectively

We can set the label using the command plt.xlabel(“some text”) or pltylabel(“some text”).

If we want to add a tile the command to use is plt.title(“Add your title here”)

Finally we show the plot with the command plt.show()

Ok we have plotted a line but what happens if we need to plot more lines and we need to put a legend and we want to format the lines in a different manner?

Let’s have a look at a more complicated example

import matplotlib.pyplot as plt

plt.plot([2,4,6,8,10],[200,250,300,400,550],'b--',label="Revenue")
plt.xlabel("Months")
plt.ylabel("Units Sold")
plt.plot([2,4,6,8,10],[150,200,210,260,360],'r:',label="Cost")

plt.title("Representing a Series of Data")
plt.legend(loc="best")
plt.show()

As you can see now we have two lines and a legend providing more information.

In comparison with the previous graph we have

  1. Added a sencond line
  2. Format the data choosing a different color for each line
  3. Chose a different style for each line

To format the line we have used a format string. Here is the link to the MatPlot WebPage which contains very useful information

Link