Customizing line styles in Matplotlib can greatly enhance the clarity and aesthetics of your plots. This tutorial will guide you through various ways to customize line styles using the `matplotlib` library. We'll start with basic line style customizations and move on to more advanced patterns and examples. ### Basic Line Styles Matplotlib provides several predefined line styles that you can use to differentiate between different lines in your plots. **Line styles available in Matplotlib:** - `'-'` : Solid line (default) - `'--'` : Dashed line - `'-.'` : Dash-dot line - `':'` : Dotted line Here's an example showing how to use these predefined line styles:
import matplotlib.pyplot as plt # Sample data x = [0, 1, 2, 3, 4, 5] y1 = [0, 1, 4, 9, 16, 25] y2 = [0, 1, 8, 27, 64, 125] y3 = [0, 1, 2, 3, 4, 5] y4 = [0, 1, 0, 1, 0, 1] plt.plot(x, y1, linestyle='-', label='Solid') plt.plot(x, y2, linestyle='--', label='Dashed') plt.plot(x, y3, linestyle='-.', label='Dash-dot') plt.plot(x, y4, linestyle=':', label='Dotted') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Basic Line Styles') plt.legend() plt.show()
- **`linestyle='-'`** uses a solid line. - **`linestyle='--'`** uses a dashed line. - **`linestyle='-.'`** uses a dash-dot line. - **`linestyle=':'`** uses a dotted line. ### Customizing Line Colors and Widths You can further customize the appearance of lines by specifying colors and line widths.
import matplotlib.pyplot as plt # Sample data x = [0, 1, 2, 3, 4, 5] y1 = [0, 1, 4, 9, 16, 25] y2 = [0, 1, 8, 27, 64, 125] plt.plot(x, y1, linestyle='--', color='r', linewidth=1, label='Red Dashed') plt.plot(x, y2, linestyle=':', color='g', linewidth=4, label='Green Dotted') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Custom Line Colors and Widths') plt.legend() plt.show()
- **`color='r'`** sets the line color to red. - **`linewidth=2`** sets the line width to 2 pixels. - **`color='g'`** sets the line color to green. - **`linewidth=3`** sets the line width to 3 pixels. ### Custom Line Styles with Markers Combining line styles with markers can make your plots even more informative by highlighting data points.
import matplotlib.pyplot as plt # Sample data x = [0, 1, 2, 3, 4, 5] y1 = [0, 1, 4, 9, 16, 25] y2 = [0, 1, 8, 27, 64, 125] plt.plot(x, y1, linestyle='-', marker='o', color='b', label='Solid with Circles') plt.plot(x, y2, linestyle='--', marker='s', color='m', label='Dashed with Squares') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Custom Line Styles with Markers') plt.legend() plt.show()
- **`marker='o'`** adds circles at each data point. - **`marker='s'`** adds squares at each data point. ### Advanced Custom Line Styles You can create custom line styles using dash patterns defined as sequences of on/off ink lengths in points:
import matplotlib.pyplot as plt # Sample data x = [0, 1, 2, 3, 4, 5] y1 = [0, 1, 4, 9, 16, 25] y2 = [0, 1, 8, 27, 64, 125] line1, = plt.plot(x, y1, color='purple', label='Custom Dash Pattern 1') line2, = plt.plot(x, y2, color='brown', label='Custom Dash Pattern 2') # Custom dash patterns line1.set_dashes([5, 2, 10, 5]) # 5 points on, 2 off, 10 on, 5 off line2.set_dashes([2, 2, 2, 2]) # 2 points on, 2 off, example for fine dashed line plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Advanced Custom Line Styles') plt.legend() plt.show()
- **`line1.set_dashes([5, 2, 10, 5])`** defines a custom dash pattern with segments of 5 points on, 2 points off, 10 points on, and 5 points off. - **`line2.set_dashes([2, 2, 2, 2])`** defines a custom dash pattern with segments of 2 points on and 2 points off. ### Example with Combined Customizations Here’s an example combining various customizations to create a comprehensive and visually appealing plot:
import matplotlib.pyplot as plt # Sample data days = [0, 1, 2, 3, 4, 5] temps_A = [22, 21, 23, 24, 23, 22] temps_B = [18, 19, 20, 21, 20, 19] plt.plot(days, temps_A, linestyle='-', marker='o', color='blue', linewidth=2, label='City A') plt.plot(days, temps_B, linestyle='--', marker='s', color='red', linewidth=2, label='City B') plt.xlabel('Days', fontsize=12) plt.ylabel('Temperature (°C)', fontsize=12) plt.title('Temperature Variations Over Days', fontsize=14, fontweight='bold') plt.show()