This page was generated from source/Jupyter/tutorials/tutorial-1-basic-plotting.ipynb. Interactive online version: Binder badge Slideshow: Binder badge

13.3.1. Basic plotting in Python

Here you will learn the basics of plotting in python including:

  1. How to make a basic plot

  2. How to add labels, legends and title to plot

  3. How to change the color, fontsize, and other properties of the lines in the plot

  4. How to save plots to files with different formats

Load the necessary packages

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

Let’s create some sample data to plot

[2]:
x=np.arange(0,10,.1)
y=np.sin(x)

A first quick plot:

[3]:
plt.plot(x,y)
[3]:
[<matplotlib.lines.Line2D at 0x1edf2412b70>]
../../_images/Jupyter_tutorials_tutorial-1-basic-plotting_8_1.png

We want to add x and y labels and title to the plot:

[4]:
plt.plot(x,y)
plt.xlabel('This is x label')
plt.ylabel('This is y label')
plt.title('This is the title')
[4]:
Text(0.5, 1.0, 'This is the title')
../../_images/Jupyter_tutorials_tutorial-1-basic-plotting_10_1.png

Now add more data (lines) to the plot, and make a legend

[5]:
plt.plot(x,y,label='y=sin(x)')
plt.plot(x,2*y,label='x=2*sin(x)')
plt.xlabel('This is x label')
plt.ylabel('This is y label')
plt.title('This is the title')
plt.legend()
[5]:
<matplotlib.legend.Legend at 0x1edf252db70>
../../_images/Jupyter_tutorials_tutorial-1-basic-plotting_12_1.png

The mathematical formula can be improved in the legend

You need to use: $your formula here$

[6]:
plt.plot(x,x*x,label='$y=x^2$')
plt.plot(x,x*x*x,label='$y=x^3$')
plt.xlabel('This is x label')
plt.ylabel('This is y label')
plt.title('This is the title')
plt.legend()
[6]:
<matplotlib.legend.Legend at 0x1edf2597b70>
../../_images/Jupyter_tutorials_tutorial-1-basic-plotting_14_1.png

The superscript is made using ^{your superscript} and subscript is made using _{your subscript}

$x^{-3}$=\(x^{-3}\)

$x_{data}$=\(x_{data}\)

The line width, line style and line color can all be changed:

[7]:
plt.plot(x,x*x,label='$y=x^2$',linewidth=2,color='r',linestyle='--')
plt.plot(x,x*x*x,label='$y=x^3$',linewidth=4,color='b')
plt.xlabel('This is x label')
plt.ylabel('This is y label')
plt.title('This is the title')
plt.legend()
[7]:
<matplotlib.legend.Legend at 0x1edf262c048>
../../_images/Jupyter_tutorials_tutorial-1-basic-plotting_18_1.png

To change the text fontsize:

[8]:
plt.plot(x,x*x,label='$y=x^2$',linewidth=2,color='r',linestyle='--')
plt.plot(x,x*x*x,label='$y=x^3$',linewidth=4,color='b')
plt.xlabel('this is fontsize 14',fontsize=14)
plt.ylabel('this is fontsize 20',fontsize=20)
plt.title('This is the title')
plt.legend(fontsize=14)
[8]:
<matplotlib.legend.Legend at 0x1edf26c7128>
../../_images/Jupyter_tutorials_tutorial-1-basic-plotting_20_1.png

To change the figure size and shape:

[9]:
plt.figure(figsize=(20,5))
plt.plot(x,x*x,label='$y=x^2$',linewidth=2,color='r',linestyle='--')
plt.plot(x,x*x*x,label='$y=x^3$',linewidth=4,color='b')
plt.xlabel('this is fontsize 14',fontsize=14)
plt.ylabel('this is fontsize 20',fontsize=20)
plt.title('This is the title')
plt.legend(fontsize=14)
[9]:
<matplotlib.legend.Legend at 0x1edf276e160>
../../_images/Jupyter_tutorials_tutorial-1-basic-plotting_22_1.png

and finally, to save your figure to a file:

[10]:
plt.figure(figsize=(20,5))
plt.plot(x,x*x,label='$y=x^2$',linewidth=2,color='r',linestyle='--')
plt.plot(x,x*x*x,label='$y=x^3$',linewidth=4,color='b')
plt.xlabel('this is fontsize 14',fontsize=14)
plt.ylabel('this is fontsize 20',fontsize=20)
plt.title('This is the title')
plt.legend(fontsize=14)
plt.savefig('sample.png',dpi=100)
../../_images/Jupyter_tutorials_tutorial-1-basic-plotting_24_0.png

You can use various formats such as jpg, pdf, etc.

You can control the quality of the saved file using dpi keyword. Be careful that using large numbers for dpi would make the process of saving figures slow and the created file very large.