Python Basic Plotting Tutorial # 1 : Simple Plotting

To start plotting in Python install the library Matplotlib. If you are unfamiliar with matplotlib, it is a very popular graphing and visualization library.

You can Install this library by using the command in the terminal:

pip install matplotlib

Or visit their website Matplotlib

I would highly recommend viewing the documentation they provide, its very helpful. This library has many interesting features.

Simple Plotting

This example will demonstrate how to plot a straight line.

Notice that at the top, the library matplotlib was imported using the key word import

import matplotlib.pyplot as plt

#Setting x axis values 
x = [5,10,15,20] 
# Setting y axis values 
y = [5,10,15,20] 
  
# plotting the points  
plt.plot(x, y) 

# Title graph 
plt.title('Graphs Title') 
# Lable x axis 
plt.xlabel('x - axis') 
# Lable y axis 
plt.ylabel('y - axis') 

# plotting points  
plt.plot(x, y, color='blue', linestyle='solid', linewidth = 4, 
         marker='o', markerfacecolor='red', markersize=8) 
plt.show()
Output

Matplotlib has a built in feature that allows users to easily change the linestyle , marker and marker symbol. Use the string “dotted”, “dashed”, “dashdot” to change the line styles.

Dotted Line Style

## Dashdot Example 
#Setting x axis values 
a = [5,10,15,20] 
# Setting y axis values 
b = [5,10,15,20]
plt.plot(a, b) 
plt.title('dotted example  ') 
# Lable x axis 
plt.xlabel('x - axis') 
# Lable y axis 
plt.ylabel('y - axis') 
# plotting the points  
plt.plot(a, b, color='blue', linestyle='dotted', linewidth = 4, 
         marker='D', markerfacecolor='yellow', markersize=8) 
plt.show()
Output

Dashdot Line Style

##Dashdot example
#Setting x axis values 
z = [3,6,9,12] 
# Setting y axis values 
w = [3,9,16,12]
plt.plot(z, w) 
plt.title('dashdot example') 
# Lable x axis 
plt.xlabel('x - axis') 
# Lable y axis 
plt.ylabel('y - axis') 
# plotting the points  
plt.plot(z, w, color='green', linestyle='dashdot', linewidth = 4, 
         marker='X', markerfacecolor='red', markersize=8) 
plt.show()
Output

Dashed Line Style

##Dashed Example
#Setting x axis values 
q = [3,6,9,12] 
# Setting y axis values 
r = [3,12,6,9]
plt.plot(q, r) 
plt.title('dashed example') 
# Lable x axis 
plt.xlabel('x - axis') 
# Lable y axis 
plt.ylabel('y - axis') 
# plotting the points  
plt.plot(q, r, color='yellow', linestyle='dashed', linewidth = 4, 
         marker='s', markerfacecolor='red', markersize=8) 
plt.show()
Output