SwadhyAI Notes

All Python notes

Matplotlib Scatter 🌌

Scatter Plots

Scatter plots are used to observe relationships between variables. Use scatter() to draw one dot for each observation.

✨ Basic Scatter

python
import matplotlib.pyplot as plt
import numpy as np

x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])

plt.scatter(x, y)
plt.show()

🌈 Colormaps

You can map colors to values using a Colormap. Use the c argument for an array of values and cmap for the color scheme.

python
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])

plt.scatter(x, y, c=colors, cmap='viridis')
plt.colorbar() # Show the color scale
plt.show()

💡 Dot Sizes

You can change the size of each dot individually using the s argument.

python
sizes = np.array([20,50,100,200,500,1000,60,90,10,300,600,800,75])
plt.scatter(x, y, s=sizes)