Scatter plots are a great way to understand the relationship between two variables. This tutorial will help you learn how to create scatter plots using Matplotlib and customize various aspects such as marker color, size, and style. ### Basic Scatter Plot Let's start by creating a basic scatter plot.
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [10, 14, 19, 25, 30] # Basic scatter plot plt.scatter(x, y) # Display the plot plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Basic Scatter Plot') plt.show()
### Customizing Marker Color You can customize the marker color using the `c` parameter. You can use any [named color](/tutorials/matplotlib-colors).
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [10, 14, 19, 25, 30] # Scatter plot with custom marker colors plt.scatter(x, y, c='red') # Display the plot plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Scatter Plot with Custom Marker Color') plt.show()
### Customizing Marker Size The `s` parameter allows you to set the size of the markers.
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [10, 14, 19, 25, 30] # Scatter plot with custom marker size plt.scatter(x, y, s=200) # Size of markers set to 200 # Display the plot plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Scatter Plot with Custom Marker Size') plt.show()
### Customizing Marker Style The `marker` parameter can be used to change the style of the markers.
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [10, 14, 19, 25, 30] # Scatter plot with custom marker style plt.scatter(x, y, marker='^') # Triangle-up markers # Display the plot plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Scatter Plot with Custom Marker Style') plt.show()
### Adjust transparency Adjusting the transparency (alpha) of markers can help in making plots more readable, especially when dealing with overlapping data points.
import matplotlib.pyplot as plt import numpy as np # Sample data - generating random data points using normal distribution np.random.seed(0) x = np.random.randn(1000) y = np.random.randn(1000) # Scatter plot with custom marker alpha plt.scatter(x, y, alpha=0.5) # Alpha set to 0.5 for partial transparency # Display the plot plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Scatter Plot with Custom Marker Alpha') plt.show()
### Combining Customizations You can combine multiple customizations to create a more informative scatter plot.
import matplotlib.pyplot as plt import numpy as np # Sample data - generating random data points using normal distribution np.random.seed(0) x = np.random.randn(1000) y = np.random.randn(1000) colors = np.random.randn(1000) sizes = np.random.randint(10, 101, size=1000) # Scatter plot with multiple customizations plt.scatter(x, y, c=colors, cmap="viridis", s=sizes, marker='o', alpha=0.5) # Display the plot plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Scatter Plot with Multiple Customizations') plt.show()
### Conclusion This tutorial demonstrated how to create and customize scatter plots using Matplotlib. We covered basic scatter plots and various customization options, including marker color, size, and style. By combining these customizations, you can create more informative and visually appealing plots for your data analysis reports.