Draw 2D Graphs in Python with Matplotlib

One thing every Pygrammer must know is how to plot graphs with Python. It may seem very tough, but a module called matplotlib makes our job a lot simpler. 

Plotting Graphs With Matplotlib: Now, a basic graph has two axes. Points are in the form of (x-coordinate, y-coordinate), and you can plot the point on the graph. After plotting every point, a curve is drawn connecting every point. Matplotlib.pyplot has a function called plot(). plot() function takes two lists, one list of x-coordinates and one list of y-coordinates. Let me show you an example. 

from matplotlib import pyplot

x = [1,2,3,4,5,6,7,8]
y = [2,3,5,6,8,9,11,10]

pyplot.plot(x,y)

pyplot.show()


Here, the x-coordinates will be from x_points, and the y-coordinates will be the corresponding value in y_points. So, the points will be (1,2) (2,3) (3,5), and so on. plt.show() shows us the graph which looks like this. 

matplotlib graph python
















Neat and simple. Now, what if we are given an equation, say y = 4x + 5, which is the equation of a line with slope 4 and passes the y-axis at 5. This can be done by creating a function that takes the x-value and returns the y-value, and then we can take this y-value and generate the list of y-values. In code, it looks like this:

from matplotlib import pyplot

def line(x_coordinate):
    return (4*x_coordinate)+5

x = [-5,-4,-3,-2,-1,0,1,2,3,4,5,6]
y = [line(x_point) for x_point in x]

pyplot.plot(x,y)

pyplot.show()

Here, I have taken some random x-values in the list x_points, and the y-value of the list according to the function is returned by line(). These returned values are then used as the corresponding y-values to get the line in the graph, which looks like this:















But, the cool thing is that the equation need not always be a line's equation. We could use a parabola, a cubic equation, or even a sine graph. We could draw a graph for any function possible with this method. Let's try one more. What about the logarithm function.


We need to import the math module because Python doesn't have an inbuilt log function. Math is an inbuilt module, so we don't need to download and install it. If you want to know more about The Math Module, check out this post first: The Math Module

What you need to know about the math module is that negative numbers and zero are not valid values for the log function, so we need to start our x-values from any number above 0. If we take only positive integers, the curve looks like this:


















To get a cleaner curve, We can use numbers from 0.01 to 10, increasing by 0.01 every time. Typing this will take ages, so let's use a while loop to make things quick. The final code looks like this:

from matplotlib import pyplot

import math

def logarithm(x_point):
    return math.log(x_point)

n = 0.01

x = []

while n<10:

    x.append(n)

    n+=0.01

y = [logarithm(x_point) for x_point in x]

pyplot.plot(x,y)

pyplot.show()

Now, when we execute this, we get a clean curve that represents the graph of the logarithm function:



log graph















Much cleaner and closer to an actual logarithm graph. 

Now, sometimes you might want to compare two graphs in the same window. That is simple too. Write another plot function with the new values under the first one to get two graphs. For example, if we want to compare a log graph and a parabola, you must use another pyplot.plot() function under the first one with the required values. 

You may also need to highlight a specific portion of the graph in some cases. What if you have two lines and you need to highlight the area between the two lines. Matplotlib offers a function for that too. The fill_between function takes attributes for the two lines and a color name to give you a shaded area. Let's take two lines, y = 2x and y = 3x. Plotting these two graphs gives us: 




















Now, to mark the area between the two lines, we can use pyplot.fill_between(x,y2,y1, color = '<Whatever color you want>'), where y2 is the list of y values of the upper line and y1, the lower line. 

from matplotlib import pyplot

def line1(x):
    return 2*x

def line2(x):
    return 3*x

x = [1,2,3,4,5,6,7,8,9,10]

y1 = [line1(x_point) for x_point in x]
y2 = [line2(x_point) for x_point in x]

pyplot.plot(x,y1,x,y2)

pyplot.fill_between(x,y2,y1,color='#42f5b0')

pyplot.show()

This gives us an output like this: 


The area between the two plotted lines is clearly filled with the color we entered. Cool, right?

Here are some of the most essential functions for plotting graphs with Matplotlib. Do follow ThePygrammer if you find this interesting. Feel free to use the comment section for any queries, and I will get back to you with a solution as soon as possible.


If you want to know more about the Math Module: The Math Module.

Comments