Sunday, August 28, 2022

How to create grid on Matplotlib plot?

To create a grid simply type the plt.grid() just before showing the graph using plt.show(). The default grid function can be written as:
matplotlib.pyplot.grid(visible=None, which='major', axis='both', **kwargs)
Visible argument is used to show the grid lines. Visible argument can be set to True, False, and None. The which argument can be set to major, minor, or both. The definition of argument "which" is the grid lines to apply the changes on. The "axis" to apply changes on can be set to "both", "x", and "y".
Example 1 - Show the function \(y(x) = \sin(x)\cdot \cos(x)\) using matplotlib in range 0 to 20 with figsize \(12\times 8[\mathrm{in}]\) and grid (only major ticks).
Solution: First step is to generate data i.e. the x and y using numpy library. After the numpy library is imported the x list will be created in the range from 0 to 20 with the step of 0.1 (step is arbitrary). Then the y list will be created as np.sin(x[i])*np.cos(x[i]) for i in range(len(x)).
import numpy as np
x = [i for i in range(0,20.1,0.1)]
y = [np.sin(x[i])*np.cos(x[i]) for i in range(len(x))]
Now that the data is generated we can plot the function using matplotlib.
import matplotlib.pyplot as plt
The next step is to set the figsize to \(12\times 8 [\mathrm{in}]\).
plt.figure(figsize=(12,8))
To plot the \(y(x)\) function we will use matplotlib.pyplot.plot function.
plt.plot(x,y)
Finally, we will set the matplotlib.pyplot.grid function to make the grid visible for major ticks on both axes.
plt.grid(visible=True, which="major", axis="both")
To show the plot we will use matplotlib.pyplot.show() function.
plt.show()
The entire code used in this example is shown below.
import numpy as np
import matplotlib.pyplot as plt
x = [i for i in range(0,20.1,0.1)]
y = [np.sin(x[i])*np.cos(x[i]) for i in range(len(x))]
plt.figure(figsize=(12,8))
plt.plot(x,y)
plt.grid(visible=True, which ="major", axis = "both")
plt.show()
The result of the previous code is shown in Figure 1.
Figure 1 - Plot of \(y(x) = \sin(x)\cdot \cos(x)\) with visible grid (major ticks only).

Example 2 - Plot the function with given coordinates \begin{eqnarray} x &=& [0,1,2,3,4,5,6,7,8,9,10]\\ y &=& [350, 530, 450, 2456, 665, 1354, 1423, 8564, 2957, 1025,2312] \end{eqnarray} Set the figsize to \(12\times 8 [\mathrm{in}]\), set the y-axis scale type to 'log', and show the grid with major (blue color) and minor ticks (red color).
Solution: In this example the data is already given so we don't need to apply the numpy library. Instead, we will start by importing the matplotlib library and copy the x and y coordinates.
import matplotlib.pyplot as plt
x = [0,1,2,3,4,5,6,7,8,9,10]
y = [350, 530, 450, 2456, 665, 1354, 1423, 8564, 2957, 1025,2312]
Next, we will set the figsizes using matplotlib.pyplot.figure function, plot the coordiantes using matplotlib.pyplot.plot function, and set y-axis scale to 'log' using matplotlib.pyplot.yscale function.
plt.figure(figsize=(12,8))
plt.plot(x,y)
plt.yscale('log')
Now we can set the grid for this plot using matplotlib.pyplot.grid function. The function will be used twice since we have to define the type of lines for major ticks and minor ticks. The major ticks will be lines in blue color while minor ticks will be dashed lines in red color.
plt.grid(visible = True, which = 'major', color='blue')
plt.grid(visible = True, which = 'minor', color='red', linestyle = '--')
It can be seen from previous code lines that major ticks we set in blue color by color = 'blue'. The tick lines are full by default so no additional linestyle definition is required. However, the minor tick lines have to be set to '--' so linestyle arguments must be defined i.e. linestlye = '--'. The color of minor tick lines is red color = 'red'. Finally, we will set the range of x axis between 0 and 20 and show the plot.
plt.xlim(0,20)
plot.show()
The entire code used in this example is given below.
import matplotlib.pyplot as plt
x = [0,1,2,3,4,5,6,7,8,9,10]
y = [350, 530, 450, 2456, 665, 1354, 1423, 8564, 2957, 1025,2312]
plt.figure(figsize=(12,8))
plt.plot(x,y)
plt.yscale('log')
plt.grid(visible = True, which = 'major', color='blue')
plt.grid(visible = True, which = 'minor', color='red', linestyle = '--')
plt.xlim(0,20)
plot.show()
The result is shown in Figure 2.
Figure 2 - The plot with defined grid at major and minor ticks

No comments:

Post a Comment