Matplotlib Scatter in Python
Matplotlib Scatter in Python
A scatter plot is used to visualize the relationship between two continuous variables. Each point in the plot represents a pair of values (x, y). Scatter plots are useful for observing the correlation or patterns in the data.
Basic Scatter Plot using matplotlib.pyplot.scatter()
To create a scatter plot in Python using Matplotlib, you can use the scatter() function from the pyplot module.
Example: Basic Scatter Plot
import matplotlib.pyplot as plt# Datax = [1, 2, 3, 4, 5]y = [1, 4, 9, 16, 25]# Create a scatter plotplt.scatter(x, y)# Add labels and titleplt.xlabel('X Axis')plt.ylabel('Y Axis')plt.title('Basic Scatter Plot')# Show the plotplt.show()Explanation:
plt.scatter(x, y): This function creates a scatter plot where thexlist contains the values for the x-axis, and theylist contains the corresponding values for the y-axis.plt.xlabel('X Axis')andplt.ylabel('Y Axis'): These functions add labels to the x-axis and y-axis.plt.title('Basic Scatter Plot'): Adds a title to the plot.plt.show(): Displays the plot.
Customizing a Scatter Plot
You can customize scatter plots in several ways, including changing the color, size, and style of the points.
Example: Customized Scatter Plot
import matplotlib.pyplot as plt# Datax = [1, 2, 3, 4, 5]y = [1, 4, 9, 16, 25]# Create a scatter plot with customized markersplt.scatter(x, y, color='red', marker='*', s=100) # 's' is for size# Add labels and titleplt.xlabel('X Axis')plt.ylabel('Y Axis')plt.title('Customized Scatter Plot')# Show the plotplt.show()Explanation:
color='red': Sets the color of the scatter points to red.marker='*': Changes the shape of the points to a star (*).s=100: Sets the size of the scatter points to 100.
Example: Scatter Plot with Different Colors and Sizes
import matplotlib.pyplot as plt# Datax = [1, 2, 3, 4, 5]y = [1, 4, 9, 16, 25]colors = [1, 2, 3, 4, 5] # Color by this listsizes = [20, 50, 80, 200, 500] # Size by this list# Create a scatter plot with different colors and sizesplt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='viridis')# Add labels and titleplt.xlabel('X Axis')plt.ylabel('Y Axis')plt.title('Scatter Plot with Different Colors and Sizes')# Show the plotplt.colorbar() # Show color barplt.show()Explanation:
c=colors: Colors each point according to the corresponding value in thecolorslist.s=sizes: Sizes each point according to the values in thesizeslist.alpha=0.5: Adds transparency to the points (0 is fully transparent, 1 is fully opaque).cmap='viridis': Sets the color map to'viridis'(you can change this to other color maps like'plasma','inferno', etc.).plt.colorbar(): Displays a color bar to show the color scale.
Scatter Plot with Labels
You can add annotations to your scatter plot to label individual points.
Example: Scatter Plot with Annotations
import matplotlib.pyplot as plt# Datax = [1, 2, 3, 4, 5]y = [1, 4, 9, 16, 25]# Create a scatter plotplt.scatter(x, y)# Add annotationsfor i in range(len(x)): plt.text(x[i], y[i], f'({x[i]},{y[i]})', fontsize=12, ha='right')# Add labels and titleplt.xlabel('X Axis')plt.ylabel('Y Axis')plt.title('Scatter Plot with Annotations')# Show the plotplt.show()Explanation:
plt.text(x[i], y[i], f'({x[i]},{y[i]})', fontsize=12, ha='right'): Adds a text label to each point on the scatter plot.ha='right'ensures the text is aligned to the right of the point.
Conclusion
scatter()is a versatile function in Matplotlib for creating scatter plots, useful for visualizing relationships between two variables.You can customize the plot using attributes like
color,size,alpha, andmarker.Adding labels and annotations helps to make the plot more informative.
Matplotlib’s scatter plots are a great way to explore data relationships visually and can be customized to fit your specific needs.