In this article, we will be exploring how to plot a very simple line graph using a Python library called matplotlib.
Matplotlib is a library for making 2D and 3D graphs in Python. It has been present since around 2008. Install it through pip. Go to the command line and run the following line:
pip install matplotlib
Let’s see the example.
# First import the pyplot class. It is pretty much the # only thing we need right now. import matplotlib.pyplot as plt # The x-ordinates and their respective y-coordinate x = [1,2,3] y = [1,8,4] # Plot them plt.plot(x,y) # Just naming the axes plt.xlabel('X-axis') plt.ylabel('Y-axis') # Giving a title to the graph plt.title("Example 1") # Now display it plt.show()
I hope it is now clear how to draw a single using 2 arrays. It is fairly intuitive.
- Use pyplot
- Specify x and y values in two arrays
- Set up your graph i.e its title and the axes name.
- Plot it and display it