Python - Matplotlib Pyplot Part 2: Scatter Plots
Scatter plots are useful for visualizing relationships between two variables.
Examples and Explanation
Basic Scatter Plot
plt.scatter([1, 2, 3, 4], [10, 20, 25, 30])
plt.title("Basic Scatter Plot")
plt.show()
Explanation: The scatter() function creates scatter plots with individual data points.
Customizing Scatter Points
sizes = [50, 100, 150, 200]
colors = ['red', 'blue', 'green', 'orange']
plt.scatter([1, 2, 3, 4], [10, 20, 25, 30], s=sizes, c=colors, alpha=0.5)
plt.title("Customized Scatter Plot")
plt.show()
Explanation: Adjusting size, color, and transparency of scatter points makes it easier to represent additional dimensions.
Overlaying Scatter and Line Plots
plt.plot([1, 2, 3, 4], [10, 20, 30, 40], linestyle='--')
plt.scatter([1, 2, 3, 4], [10, 20, 30, 40], color='red')
plt.title("Scatter and Line Overlay")
plt.show()
Explanation: Combining scatter and line plots provides a clearer representation of trends.