Adding and tweaking a background grid

We can also apply a background grid. It will lay down gridlines, which will show the reader and the viewer that each of the ticks extends far away from the axis. This is often useful if you want the viewer to be able to draw direct numerical values by looking at pieces of the grid or of their plot. So, it's important for your viewer to be able to actually look at a point or a piece of a line and draw out the numerical value of the grid lines, as shown here:

# Gridlines
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.grid()

We will get the following output:

Like any other set of lines in Matplotlib, we have a number of different attributes that we can give to these grids.

We can color them, so you can change them to red, as shown here:

# Gridlines
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.grid(color='r')

We will get the following output:

We can also change the line style. By default, we can see it's solid in the preceding diagram. But this can be changed easily into dotted lines, as shown here:

# Gridlines
nums = np.arange(0,10,0.1)
plt.plot(nums, np.sin(nums))
plt.grid(color='r', linestyle='--')

We will get the following output:

Hence, if the dotted line is not the aesthetic appearance that the user wants, we can change that quite easily, and any of the other attributes that we have seen for tweaking the appearance of lines can be applied to this grid.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset