Intro to Matplotlib
Matplotlib is a scientific library that allows python programmers to easily produce plots for reporting on experimental data.
Usage
The main feature of matplotlib that we will use is a sub-module called pyplot
- You can import it in your script by using this line of code:
import matplotlib.pyplot as pltDraw a simple plot line using
plot.plot(x,y)xpoints = [1,2,3,4,5,6,7,8,9,10] ypoints = [2,4,6,8,10,12,14,16,18,20] plt.plot(xpoints, ypoints) plt.show() # This line is important to display your graphTo draw a simple scatter point, you can use
plt.scatter(x,y):plt.scatter(xpoints, ypoints) plt.show()If you wish to save your graphs, use
plt.savefig(). This will create a PNG image inside your project directory.plt.plot(xpoints, ypoints) plt.savefig("my_first_graph.png") plt.show() # This line is important to display your graphFollow this tutorial to learn the different ways of styling your graph (colours, markers, titles, scales, etc)