1. MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 1
https://www.linkedin.com/in/mohammedismail1982/
MATPLOTLIB with EXAMPLES
PART 1
Plot – Subplot – Histograms
Matplotlib is one of the most amazing and effective Pyhton
packages for data visualization and graph representation. It’s
really a free and open-source competitive to Matlab; especially
if Matplotlib is combined with other Pyhton technical packages
like Numpy, Scipy, and Pandas.
Learning Matplotlib using simple examples is very efficient.
And I think that the lack of examples regarding Matplotlib is
the major drawback of the package documentation. This
doesn’t mean that we can get rid of referring to the official
documentation! Whatever any official reference is complicated
and needing more effort to understand; we should keep in
mind that the official reference would be our first priority and
our main source of knowledge.
Here I will start illustrating three plotting techniques with
examples using Matplotlib. These techniques are:
• Plot.
• Subplot.
• Histograms.
Of course; your comments and feedbacks are essential for best
tuning and understanding.
So; let’s go on …
2. MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 2
https://www.linkedin.com/in/mohammedismail1982/
1 – Plot
Example (1/4) - Simple plot.
From this example you will also know:
• How to name the plot.
• How to name both the horizontal and vertical axis.
• How to choose whether to display or not display grid.
import matplotlib.pyplot as plt
x = list(range(101))
y = list(map(lambda n: n**2, x))
plt.plot(x, y)
plt.title("DEMO GRAPH")
plt.xlabel("Horizontal axis")
plt.ylabel("Vertical axis")
plt.grid() #If you like to show grid.
plt.show()
3. MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 3
https://www.linkedin.com/in/mohammedismail1982/
Example (2/4) – Scattering plot.
From this example you will also know how to choose the color
and style of your data representation.
For more and full knowledge, kindly refer to the following link:
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.p
lot
import matplotlib.pyplot as plt
Weighs = [10, 17, 23, 27, 32]
Talls = [88, 110, 150, 213, 271]
plt.title("Weighs Vs Talls")
plt.xlabel("WEIGHS")
plt.ylabel("TALLS")
plt.plot(Weighs, Talls, 'ro')
plt.show()
4. MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 4
https://www.linkedin.com/in/mohammedismail1982/
Example (3/4) – Axes ranges.
In this example we need to represent the same data in Example
(2/5) with the same representation graph but we want to have
the option of setting the values of ranges for both the
horizontal and vertical axes.
Horizontal axis will be in range: 0 – 50.
Vertical axis will be in range: 100 – 300.
plt.xlabel("WEIGHS")
plt.ylabel("TALLS")
plt.plot(Weighs, Talls, 'bo')
plt.axis([0,50,100,300])
plt.show()
5. MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 5
https://www.linkedin.com/in/mohammedismail1982/
Example (4/4) – Multiple representations in one graph.
x = list(range(101))
xblue = list(map(lambda n: n*2, x))
xgreen = list(map(lambda n: n*3, x))
xred = list(map(lambda n: n*4, x))
plt.plot(x, xblue, 'b', x, xgreen, 'g', x, xred, 'r')
plt.show()
6. MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 6
https://www.linkedin.com/in/mohammedismail1982/
2 – Subplot
Sometimes we need to keep more than one data graph on eye.
Subplot is facilitating this option. You can view several graphs
simultaneously in horizontal view, vertical view, or matrix
view. You have only to determine the dimensions of your view
model and the location index you like of each graph.
Kindly read the comments in the code examples for boosting
and fixing the concepts in your mind!
Example (1/3) – Two plots in horizontal subplot.
x1 = list(range(101))
y1 = list(map(lambda k: k**2, x1))
x2 = list(range(51))
y2 = list(map(lambda k: k*5, x2))
plt.subplot(1,2,1)
7. MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 7
https://www.linkedin.com/in/mohammedismail1982/
plt.plot(x1,y1)
plt.title("Two plots in HORIZONTAL subplot")
plt.subplot(1,2,2)
plt.plot(x2,y2)
plt.show()
#(1,2,1)===> 1 row, 2 columns, position is in index 1
#(1,2,2)===> 1 row, 2 columns, position is in index 2
Example (2/3) – Two plots in vertical subplot.
We will use the same datasets used in Example (2/5).
plt.subplot(2,1,1)
plt.plot(x1,y1)
plt.title("Two plots in VERTICAL subplot")
plt.subplot(2,1,2)
plt.plot(x2,y2)
8. MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 8
https://www.linkedin.com/in/mohammedismail1982/
plt.show()
#(2,1,1)===> 2 row, 1 columns, position is in index 1
#(2,1,2)===> 2 row, 1 columns, position is in index 2
Example (3/3) – Multiple plots in matrix subplot.
x1 = list(range(101))
x = list(range(-100, 101))
yr = list(map(lambda k: k**2, x))
yg = list(map(lambda k: k**3, x))
yb = list(map(lambda k: k**4, x))
yy = list(map(lambda k: k**5, x))
plt.subplot(2,2,1)
plt.plot(x,yr,'r')
plt.subplot(2,2,2)
plt.plot(x,yg,'g')
plt.subplot(2,2,3)
9. MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 9
https://www.linkedin.com/in/mohammedismail1982/
plt.plot(x,yb,'b')
plt.subplot(2,2,4)
plt.plot(x,yy,'y')
plt.show()
#(2,2,1) ===> 2 rows, 2 columns, position is in index 1
#(2,2,2) ===> 2 rows, 2 columns, position is in index 2
#(2,2,3) ===> 2 rows, 2 columns, position is in index 3
#(2,2,4) ===> 2 rows, 2 columns, position is in index 4
3 – Histogram
Let’s have some fundamental talk about histogram.
Suppose that there is a 23 students in a class. The 23 students
passed a mathematics exam which its full score is 100.
Now we are having the scores the students achieved in the
exam as follow:
10. MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 10
https://www.linkedin.com/in/mohammedismail1982/
[99, 40, 82, 6, 38, 85, 87, 94, 0, 80, 18, 83, 26, 49, 81, 59, 68, 45,
9, 13, 17, 5, 57]
Good! Now let’s create the following table:
How many students achieved scores in range … Number of
students is:
From 0 to less than 10? 4
From 10 to less than 20? 3
From 20 to less than 30? 1
From 30 to less than 40? 1
From 40 to less than 50? 3
From 50 to less than 60? 2
From 60 to less than 70? 1
From 70 to less than 80? 0
From 80 to less than 90? 6
From 90 to 100? 2
Using histogram related terms; ranges in the way as mentioned
in the table above are called bins. The number of occurrences -
which in our example case expressing the number of students –
is called frequency.
A frequency vs bins graph is called histogram!
We configured our example to have 10 bins. No problem if you
choose to configure it to have 20, 15, 2, 5 or any number of bins
you want. Worthless to say that more bins you have, more data
resolution you obtain.
Let’s move to Matplotlib examples!
11. MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 11
https://www.linkedin.com/in/mohammedismail1982/
Example (1/2) – Basic histogram example.
If you don’t determine the number of bins, it will be set to be
10 by default. In this example we will not determine the
number of bins. But we will do in Example (2/2).
scores = [99, 40, 82, 6, 38, 85, 87, 94, 0, 80, 18, 83, 26, 49,
81, 59, 68, 45, 9, 13, 17, 5, 57]
plt.hist(scores)
plt.grid()
plt.show()
Example (2/2) – Specifying the number of bins.
Now it’s time to configure our desired number of bins which
will be 20 in this example. We will use the same dataset
mentioned in Example (1/2) above.
plt.hist(scores, 20)
plt.grid()
plt.show()
12. MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 12
https://www.linkedin.com/in/mohammedismail1982/