This lesson is being piloted (Beta version)

Basic Plotting system

Overview

Teaching: 5 min
Exercises: 0 min
Questions
  • How many plotting system in Python

  • How to use Matplotlib & Seaborn

Objectives
  • Basic Matplotlib

  • Basic Seaborn

Plotting system in Python

There are many different packages providing plotting system for Python.

How to display plot in Jupyter Notebook

You can visualize plot in JNotebook using %matplotlib notebook and %matplotlib inline magic commands.

%matplotlib notebook
(%matplotlib inline)

import matplotlib.pyplot as plt
plt.plot(1,2,'*')

image

More control over plot using Matplotlib

# Design the plot
plt.figure()

# Start plotting
plt.plot(1,2,'o')
plt.plot(2,3,'*')
plt.plot(3,4,"s")

# Label and Title:
plt.xlabel('X Label')
plt.ylabel('Y Label')
plt.title('Title')

# Get current axes
ax = plt.gca()
#Set axis property to range (0, 5)
ax.axis([0,5,0,5]))

image

Subplot in Matplotlib

Very similar to Matlab, one just need to insert the plot number using subplot command:

plt.subplot(2,2,1)
plt.plot(1,2,'o')
plt.plot(2,3,'*')
plt.plot(3,4,"s")

plt.subplot(2,2,2)
plt.plot(1,2,'o')
plt.plot(2,3,'*')
plt.plot(3,4,"s")

plt.subplot(2,2,3)
plt.plot(1,2,'o')
plt.plot(2,3,'*')
plt.plot(3,4,"s")

plt.subplot(2,2,4)
plt.plot(1,2,'o')
plt.plot(2,3,'*')
plt.plot(3,4,"s")

image

Install and import seaborn

!pip install seaborn
import seaborn as sns

Key Points

  • Use Jupyter Notebook as general platform to write and visualize Python code using Matplotlib and Seaborn