font-family: "Helvetica Neue",Helvetica,Arial,sans-serif; font-size: 14px;

Aj St3ph@n web Page : introduction to matplotlib



First step : installation of the Matplotlib library

Here i will assume that you have isntalled Pycharm editor. it can be downloaded on the jetbrain webstite :

pycharm select downlaod and then pick the community version which is free



-1- Before starting you need to have the matplotlib library installed on your python editor. this can be done from the pycharm editor by clicking the folowing menus: pycharm select preference (mac) or file and Setup (windows)

Girl in a jacket


    

-2- then you need to navigate to the python interpreter and click on the "+" sign at the bottom of the page

Girl in a jacket


        

-3- This will open a new page in which you can type the name of the librairy you are trying to install. here we want to install the "matplotlib".

Girl in a jacket

later you can repeat the same steps to isntall numpy, scikit-image, scikit-lear and panda


basic structure of a figure

Girl in a jacket




First plot

The basic following code can be used to plot a basic graph

 

    

case 1 - X axis from 0-50 points

import matplotlib.pyplot as plt import numpy as np t = np.linspace(0,2*np.pi,50) x = np.sin(t) plt.figure() plt.plot(x) plt.show()
Girl in a jacket

    

case 2 - X axis from 0 to 2*PI

import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,2*np.pi,50) y = np.sin(t) plt.figure() plt.plot(x,y) plt.show()
Girl in a jacket
    
    
    
    
    

2 plots on the same figure

here are some basics details about how to create simple plots using the library matplotlib. Axis Vs Axes:
The Figure

this code would generate the following figure:

import numpy as np                             # this is for math calculation
import matplotlib.pyplot as plt                # This is for ploting        

x1 = np.linspace(0.0, 6.0)                     # linespace function will create the X axis
x2 = np.linspace(0.0, 3)

y1 = np.cos(3 * np.pi * x1) * np.exp(-x1)      # define equation of each plot
y2 = np.cos(3 * np.pi * x2)

fig, (ax1, ax2) = plt.subplots(2, 1)           # create the plot with 2 parts.
fig.suptitle('a Figure with 2 subplot')

ax1.plot(x1, y1, 'o-')                         # plot the first upper part
ax1.set_ylabel('Viscous polymer')              # set the y axis

ax2.plot(x2, y2, '.-')                         # plot the second lower part
ax2.set_xlabel('time (s)')                     # set both x axis
ax2.set_ylabel('Rigid polymer')                # set the y axis

plt.show()                                     # display the plot



Girl in a jacket