Matplotlib Notes Part1 With Code
Matplotlib Notes Part1 With Code
Basic Plot
1.0
0.5
0.0
0.5
1.0
0 2 4 6 8 10
plt.plot(x, y)
plt.show()
2. Labels and Title
1.0
0.5
0.0
0.5
1.0
0 2 4 6 8 10
Time
plt.plot(x, y)
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.title('Sine Wave')
3. Styling the Plot
1.0
0.5
0.0
0.5
1.0
0 2 4 6 8 10
plt.plot(x, y, color='r', linestyle='--', marker='o')
4. Legends
1.0 sin(x)
cos(x)
0.5
0.0
0.5
1.0
0 2 4 6 8 10
plt.plot(x, y1, label='sin(x)')
plt.plot(x, y2, label='cos(x)')
plt.legend()
5. Grid
1.0
0.5
0.0
0.5
1.0
0 2 4 6 8 10
plt.plot(x, y)
plt.grid(True)
6. Subplots
1.0
0.5
0.0
0.5
1.0
0 1 2 3 4 5
fig, axs = plt.subplots(2, 1)
axs[0].plot(x, y1)
axs[1].plot(x, y2)
7. Figure Size
1.0
0.5
0.0
0.5
1.0
0 2 4 6 8 10
plt.figure(figsize=(8, 4))
plt.plot(x, y)
8. Bar Plot
20
15
10
0 A B C
plt.bar(categories, values)
9. Histogram
15
10
0 2 1 0 1 2
plt.hist(data, bins=10)
10. Scatter Plot
1.0
0.5
0.0
0.5
1.0
0 2 4 6 8 10
plt.scatter(x, y)
11. Pie Chart
A
B 22.2%
44.4%
33.3%
C
0.5
0.0
0.5
1.0
0 2 4 6 8 10
plt.savefig('filename.png')