
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Line Styles in Matplotlib Python
A line's aesthetics in any line plot are influenced by the line style, also known as linestyle. The Matplotlib module for Python provides a wide range of line styles to improve the aesthetic appeal of plots. The use of line styles in Matplotlib Python is thoroughly covered in this article along with lucid and succinct examples.
Understanding Line Styles in Matplotlib
The pattern of the line that can be plotted is defined by line styles in Matplotlib. Solid, dashed, dash-dot, and dotted lines are some of the most popular line types. You may make your plots clearer and more informative by utilising various line styles.
Getting Started with Matplotlib Line Styles
First, check to see if your Python environment has the Matplotlib module loaded. If not, pip can be used to install it ?
pip install matplotlib
Bring in the required library ?
import matplotlib.pyplot as plt
Example 1: Basic Line Styles
Let's start with some simple line style examples:
import numpy as np # Define a simple range of values for x and reshape so it can be used in multiple plots x = np.linspace(0, 10, 1000).reshape(-1,1) # Set up the figure and axis fig, ax = plt.subplots(1,1, figsize=(10,6)) # solid line style ax.plot(x, np.sin(x), linestyle='-', label='solid') # dashed line style ax.plot(x, np.cos(x), linestyle='--', label='dashed') # dashdot line style ax.plot(x, -np.sin(x), linestyle='-.', label='dashdot') # dotted line style ax.plot(x, -np.cos(x), linestyle=':', label='dotted') ax.legend() plt.show()
The plot() method is used in this example to plot four alternative line styles ('-', '--', '-.', and ':').
Example 2: Line Style Shortcodes
Additionally, Matplotlib offers one-letter shortcodes for the majority of common line styles ?
Additionally, Matplotlib offers one-letter shortcodes for the majority of common line styles: fig, ax = plt.subplots(1,1, figsize=(10,6)) # solid line style ax.plot(x, np.sin(x), linestyle='-', label='solid') # dashed line style ax.plot(x, np.cos(x), linestyle='d', label='dashed') # dashdot line style ax.plot(x, -np.sin(x), linestyle='-.', label='dashdot') # dotted line style ax.plot(x, -np.cos(x), linestyle=':', label='dotted') ax.legend() plt.show()
This example uses the single-letter shortcodes but still uses the same line styles as before.
Example 3: Line Styles with Custom Spacing
To specify dash patterns, you can define your own line styles using a tuple:
fig, ax = plt.subplots(1,1, figsize=(10,6)) # Define custom line styles line_styles = [(0, (1, 10)), (0, (1, 1)), (0, (5, 10)), (0, (5, 5))] for i, style in enumerate(line_styles, start=1): ax.plot(x, i * np.sin(x), linestyle=style, label=f'line style {i}') ax.legend() plt.show()
A line style is defined by each tuple. The offset is the first value in the tuple. The second value is a tuple that specifies the dash and spacing lengths.
Example 4: Combining Line Styles with Colors
To make your plot more interesting and instructive, you can also combine different colours and line styles.
fig, ax = plt.subplots(1,1, figsize=(10,6)) ax.plot(x, np.sin(x), linestyle='-', color='blue', label='solid blue') ax.plot(x, np.cos(x), linestyle='--', color='red', label='dashed red') ax.plot(x, -np.sin(x), linestyle='-.', color='green', label='dashdot green') ax.plot(x, -np.cos(x), linestyle=':', color='purple', label='dotted purple') ax.legend() plt.show()
In this illustration, a different colour is blended with each line type.
Example 5: Line Style and Marker Combination
Markers and line types can be combined to create more intricate visualisations:
fig, ax = plt.subplots(1,1, figsize=(10,6)) # Define a smaller range for x x = np.linspace(0, 10, 10) ax.plot(x, np.sin(x), linestyle='-', marker='o', label='solid with marker') ax.plot(x, np.cos(x), linestyle='--', marker='x', label='dashed with marker') ax.legend() plt.show()
There are markers at each data point in this plot along with various line types.
Conclusion
A key feature of Matplotlib is its line styles, which are essential for separating different datasets and improving the readability of the plot as a whole. You have a wide range of options, and you can utilise straightforward line styles, combine them with colours and markers, or make your own bespoke designs.
Always keep in mind that accurate data visualisation involves more than just plotting data on a graph. It involves telling an understandable story. You'll be one step closer to producing interesting and meaningful data stories by becoming an expert in Matplotlib's line styles.