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:

import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plt.plot(x, y)
plt.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:

plt.plot(x, y, color='red', linestyle='dashed')
plt.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:

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

Result:

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:

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

Now, our plot carries much more information!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal