Matplotlib
plot() Function
1. What is `[Link]()`?
The plot() function in Matplotlib is used to draw line plots — the most common type of data visualization. It can also
be used to plot multiple lines, style lines, add markers, customize colors, and plot mathematical functions.
2. Basic Syntax
[Link](x, y, format_string, **kwargs)
Parameters:
- x: X-axis data points (optional)
- y: Y-axis data points
- format_string: e.g. 'ro--' (color, marker, line style)
- kwargs: keyword arguments like linewidth, label, etc.
3. Basic Example
import [Link] as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
[Link](x, y)
[Link]('X-Axis')
[Link]('Y-Axis')
[Link]('Simple Line Plot')
[Link]()
4. Using Format Strings
Format strings like 'ro--' specify color, marker, and line style.
'r' = red, 'g' = green, 'b' = blue, '--' = dashed line, 'o' = circle marker.
5. Multiple Lines
x = [1,2,3,4,5]
y1 = [2,4,6,8,10]
y2 = [1,3,5,7,9]
[Link](x, y1, 'r--', label='y=2x')
[Link](x, y2, 'b:', label='y=2x-1')
[Link]()
[Link]()
6. Mathematical Functions
import numpy as np
x = [Link](0,10,100)
y = [Link](x)
[Link](x, y, 'm', linewidth=2)
[Link]()
7. Common Keyword Arguments
color, linestyle, linewidth, marker, markersize, label, markerfacecolor, etc.
Example:
[Link](x, y, color='green', linestyle='--', linewidth=2, marker='o')
8. Grid, Titles, Labels
[Link](x, y)
[Link]('Customized Plot')
[Link]('X-axis')
[Link]('Y-axis')
[Link](True)
[Link]()
9. Multiple Series with Loop
for a in [1, 2, 3]:
[Link](x, a*[Link](x), label=f'a={a}')
[Link]()
[Link]()
10. Subplots
[Link](2,1,1)
[Link](x, [Link](x), 'r')
[Link](2,1,2)
[Link](x, [Link](x), 'b')
plt.tight_layout()
[Link]()
11. Styling with Custom Colors
[Link](x, [Link](x), color='#008080', linewidth=3)
[Link]()
12. Different Markers
Markers: 'o' (circle), 's' (square), '^' (triangle), 'x' (cross)
Example:
[Link](x, [Link](x), marker='s', markersize=6, color='purple')
13. Log Scales
[Link]('log')
[Link]('log')
[Link]()
14. Saving the Plot
[Link]('plot_example.png', dpi=300, bbox_inches='tight')
15. Full Example
x = [Link](0, 10, 100)
y1 = [Link](x)
y2 = [Link](x)
[Link](x, y1, label='sin(x)')
[Link](x, y2, label='cos(x)')
[Link]()
[Link]()
16. Summary
Use [Link](x, y) for line plots. Customize using color, linestyle, markers, add grid, title, and save using [Link]().