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


Python Page – List of Posts

GUI Python Projects

This section is about using PyQT in order to design GUI application.

Encryption GUI App

PyQt Calculator Application Part 1

MatPlot Lib Tutorial

In this section we will be looking at how to use MatPlot to plot data and produce graphs for data analysis.

1: Plot series

2. Plot Vertical Bars

3. Plot Horizontal Bars

4. Plot Pie Charts