Matplotlib

Customizing plot appearance

In this lesson, you will learn how to modify different aspects of your plot, such as adding a grid or customizing the ticks along the axes.


You already know many ways to adjust the appearance of the line.

But the line is not the only thing you can customize.

Let's have a look at the axes of the following figure.

Python
Output
example plot

Notice that the ticks on the x-axis are somewhat sparse. Currently, there's a tick every 20 units.

Suppose we need a tick mark every 10 units. We can achieve this using the plt.xticks() function:

Python

You can pass any sequence of numbers like a list, a range, or a NumPy array.

Let's apply this to our example:

Python
Output
example plot

You can do the same for the Y-axis using plt.yticks().

Let's take another look at our plot. Even with additional ticks, it's still hard to pinpoint specific points on the plot.

Let's improve this by adding a grid to our plot using plt.grid()

Python
Output
example plot

You can customize the grid lines' appearance using the color, linestyle, and linewidth arguments that you already know from above.

Python

The argument axis is used to only display gridlines along a specific axis.

Python

Let's add some gray, dashed gridlines along the Y-axis of our plot:

Python
Output
example plot