Lesson 4

Mastering Plot Styling in Python Using Matplotlib

Topic Overview and Actualization

Welcome! Today's lesson is all about styling plots. Styling is essential for making plots visually attractive and informative. We'll walk through various styling plot aspects with Python's Matplotlib, enhancing a simple line plot as we progress. Let's get started!

Basic Plot

In Matplotlib, each plot line defaults to a specific color and line type. Here's an example with a basic line plot:

1import matplotlib.pyplot as plt 2x = [1, 2, 3, 4, 5] 3y = [1, 4, 9, 16, 25] 4plt.plot(x, y) 5plt.show()

Adjusting Colors and Line Types

Ever want to change these defaults? Fortunately, Matplotlib lets you do just that with the color and linestyle parameters:

1plt.plot(x, y, color='red', linestyle='dashed') 2plt.show()

Voila! Our line is now red and dashed!

Here's the fun part: Matplotlib offers many color options (like 'green', 'blue', 'cyan', etc.) and line styles (like 'solid', 'dotted', 'dashdot', etc.). This feature allows for more personalized and differentiated line plots.

Adding Markers

Markers can significantly enhance the aesthetics and readability of your plot by highlighting the data points. Matplotlib allows us to add markers using the marker parameter:

1plt.plot(x, y, color='red', linestyle='dashed', marker='o') 2plt.show()


Some commonly used markers include 'o' (circle), '.' (point), '*' (star), 's' (square), '+' (plus), 'x' (cross), etc.

Adding Titles, Labels, and Legends

Good labels make plots easy to understand. So, let's add a title, x-label, y-label, and a legend to make our plot more self-explanatory:

1plt.plot(x, y, color='red', linestyle='dashed') 2plt.title('Square Numbers') # Title of the plot 3plt.xlabel('Numbers') # x-axis label 4plt.ylabel('Squares') # y-axis label 5plt.legend(['Square Numbers']) # Legend 6plt.show()

Now, our plot carries much more information!

Setting Axes Limits and Ticks

You may want to focus on a particular region in your plot. Matplotlib allows us to limit the ranges shown on the x- and y-axes using xlim() and ylim(). Additionally, it allows us to set the tick marks using xticks() and yticks(). Let's try them out:

1plt.plot(x, y, color='red', linestyle='dashed') 2plt.title('Square Numbers') 3plt.xlabel('Numbers') 4plt.ylabel('Squares') 5plt.legend(['Square Numbers']) 6plt.xlim(2, 5) # Limit on x-axis 7plt.ylim(4, 25) # Limit on y-axis 8plt.xticks(range(2, 6)) # Ticks on x-axis 9plt.yticks(range(4, 26, 5)) # Ticks on y-axis 10plt.show()

We pass the lower and upper limits of the x and y axes in the xlim and ylim functions. xticks and yticks functions take exact ticks locations as an argument. Ticks are usually evenly spaced so we can set their locations with range.

Our plot now displays squares for numbers from 2 to 5.

Adding Gridlines

Gridlines provide an easier way to estimate values in plots. To add them, we use grid(True):

1plt.plot(x, y, color='red', linestyle='dashed') 2plt.title('Square Numbers') 3plt.xlabel('Numbers') 4plt.ylabel('Squares') 5plt.legend(['Square Numbers']) 6plt.xlim(1, 6) 7plt.ylim(1, 30) 8plt.xticks(range(1, 7)) 9plt.yticks(range(0, 31, 5)) 10plt.grid(True) # Adding gridlines 11plt.show()

With the gridlines turned on, our plot becomes more precise!

Lesson Summary and Practice

Congratulations! You've mastered plot styling and transformed a simple line plot into an attractive, informative visualization. Next, we'll put what you've learned to the test. These exercises will reinforce your understanding and expertise in Python data visualization. Ready to proceed? We sure are. Let's tackle the practice problems!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.