SwadhyAI Notes

All Python notes

Matplotlib Line 📉

Line Customization

You can change the style, color, and width of the plotted line using arguments like linestyle, color, and linewidth.

✨ Line Style

Use linestyle or ls to change the style.

'solid' or '-' (Default)
'dotted' or ':'
'dashed' or '--'
'dashdot' or '-.'
python
import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, ls = 'dotted')
plt.show()

🎨 Line Color

Use color or c to set the line color. You can use names or hex codes.

python
plt.plot(ypoints, color = 'r') # Red
plt.plot(ypoints, c = '#4CAF50') # Green

📏 Line Width

Use linewidth or lw to change the width (in points).

python
plt.plot(ypoints, linewidth = '20.5')

💡 Multiple Lines

You can plot multiple lines by calling plot() multiple times or by adding more arguments to a single call.

python
# Method 1
plt.plot(y1)
plt.plot(y2)

# Method 2
plt.plot(x1, y1, x2, y2)