Matplotlib tutorial 01

In [1]:
# import
import matplotlib.pyplot as plt
import numpy as np

%matplotlib inline

Simple Plot
By default, matplotlib is plotting line which joins all the points

In [2]:
X = [1, 2.4, 5, 7, 3.2]

plt.plot(X)
plt.show()
We can add different parameters to change the look of charts such as linestyle, linewidth or marker.  
Let's check them one by one - 

Adding linestyle

In [3]:
X = [1, 2.4, 5, 7, 3.2]

plt.plot(X, linestyle='--') # dashed line
plt.show()
In [4]:
X = [1, 2.4, 5, 7, 3.2]

plt.plot(X, linestyle='-.') # dashed dot line
plt.show()

Adding linewidth

In [5]:
X = [1, 2.4, 5, 7, 3.2]

plt.plot(X, linestyle='--', linewidth=4) # dashed dot line
plt.show()

Adding marker

In [6]:
X = [1, 2.4, 5, 7, 3.2]

plt.plot(X, marker='p') # added mark point
plt.show()

line style or marker

character description
'-' solid line style
'--' dashed line style
'-.' dash-dot line style
':' dotted line style
'.' point marker
',' pixel marker
'o' circle marker
'v' triangle_down marker
'^' triangle_up marker
'<' triangle_left marker
'>' triangle_right marker
'1' tri_down marker
'2' tri_up marker
'3' tri_left marker
'4' tri_right marker
's' square marker
'p' pentagon marker
'*' star marker
'h' hexagon1 marker
'H' hexagon2 marker
'+' plus marker
'x' x marker
'D' diamond marker
'd' thin_diamond marker
'|' vline marker (pipe)
'_' hline marker
In [7]:
days = list(range(0, 22, 3))
celsius_values = [25.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1]
plt.plot(days, celsius_values)
plt.show()
In [8]:
days = list(range(0, 22, 3))
celsius_values = [25.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1]
plt.plot(days, celsius_values, linestyle='--', color='g' )
plt.show()
In [9]:
days = list(range(0, 22, 3))
celsius_values = [25.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1]
plt.plot(days, celsius_values, 'og' )  # first char o is repesting the circle, #g is color green
plt.show()
In [10]:
days = list(range(0, 22, 3))
celsius_values = [25.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1]
plt.plot(days, celsius_values, 'og', linestyle='-')  # first char o is repesting the circle, #g is color green
plt.show()

Labels on Axes and Title

In [11]:
days = list(range(0, 22, 3))
celsius_values = [25.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1]
plt.plot(days, celsius_values, 'og', linestyle='-')  

plt.xlabel("days")
plt.ylabel("celsius values")
plt.title("Temprature Stats")

plt.show()

We can specify an arbitrary number of x, y, fmt groups in a plot function. In the following example, we use two different lists of y values:

In [12]:
days = list(range(1,9))
celsius_min = [19.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1]
celsius_max = [24.8, 28.9, 31.3, 33.0, 34.9, 35.6, 38.4, 39.2]
plt.xlabel('Day')
plt.ylabel('Degrees Celsius')
plt.plot(days, celsius_min,
         days, celsius_min, "oy",
         days, celsius_max, 
         days, celsius_max, "or")
plt.show()