4.1. Basic plotting in Python

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

  1. How to have 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?

Loading necessary packages

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

Let’s create some sample data for plots

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

Let’s have a quick plot:

[3]:
plt.plot(x,y)
[3]:
[<matplotlib.lines.Line2D at 0x112fd9748>]
../_images/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/tutorials_tutorial-1-basic-plotting_10_1.png

Now let’s add more lines to the plot, and try to make legends

[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 0x1131e93c8>
../_images/tutorials_tutorial-1-basic-plotting_12_1.png

how to have more complex labels such as mathematic formula:

For math formulas, you need to use: $your formula here$

let’s give it a try:

[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 0x11324d630>
../_images/tutorials_tutorial-1-basic-plotting_14_1.png

As you can see above, the superscript is made using ^{your superscript} and subscript is made using _{your subscript}

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

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

How to change the line width, line style and line color:

[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 0x1133139b0>
../_images/tutorials_tutorial-1-basic-plotting_18_1.png

How 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 0x1134b1eb8>
../_images/tutorials_tutorial-1-basic-plotting_20_1.png

how to change the figure size:

[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 0x11356fe10>
../_images/tutorials_tutorial-1-basic-plotting_22_1.png

and finally, how 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/tutorials_tutorial-1-basic-plotting_24_0.png

Note that you can use various formats such as jpg, pdf, etc.

Also 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.