8
Lesson 8
Scatter plots
Objective
By the end of this lesson, students will learn how to create scatter plots using Matplotlib, customize markers, sizes, and colors, and understand the use cases for scatter plots in data visualization.
1. Using scatter() to create scatter plots:
Scatter plots are used to display values for typically two variables for a set of data. Each point represents an observation in the dataset.
Syntax:
By the end of this lesson, students will learn how to create scatter plots using Matplotlib, customize markers, sizes, and colors, and understand the use cases for scatter plots in data visualization.
1. Using scatter() to create scatter plots:
Scatter plots are used to display values for typically two variables for a set of data. Each point represents an observation in the dataset.
Syntax:
plt.scatter(x, y, s, c, alpha, marker)
- x : The data for the x-axis.
- y : The data for the y-axis.
- s : The size of the markers (optional).
- c : The color of the markers (optional).
- alpha : The transparency of the markers (optional).
- marker : The style of the marker (optional).
Example:
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] # x-axis data y = [2, 3, 5, 7, 11] # y-axis data # Create a scatter plot plt.scatter(x, y) # Add title and labels plt.title('Scatter Plot Example') plt.xlabel('X-axis') plt.ylabel('Y-axis') # Show the plot plt.show()
In this example:
- The scatter() function creates a scatter plot with points defined by the x and y data arrays.
- Titles and labels are added for clarity.
2. Customizing scatter plot markers, sizes, and colors:
Scatter plots can be customized in various ways to enhance visual representation and convey more information.
a. Customizing marker sizes:
You can specify different sizes for the markers using the s parameter.
Example:
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] sizes = [20, 50, 80, 200, 100] # Sizes of the markers # Create a scatter plot with customized marker sizes plt.scatter(x, y, s=sizes) plt.title('Scatter Plot with Custom Marker Sizes') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.show()
In this example, different sizes are assigned to each marker, allowing for visual differentiation based on size.
b. Customizing marker colors:
You can also customize the color of the markers using the c parameter. This can be a single color or an array of colors.
Example:
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] colors = ['red', 'green', 'blue', 'orange', 'purple'] # Color for each point # Create a scatter plot with customized colors plt.scatter(x, y, c=colors) plt.title('Scatter Plot with Custom Marker Colors') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.show()
In this example, each marker is colored differently, providing a distinct visual for each point.
c. Customizing marker styles:
You can customize the style of the markers using the marker parameter. Common marker styles include 'o' (circle), 's' (square), '^' (triangle), and more.
Example:
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] # Create a scatter plot with triangle markers plt.scatter(x, y, marker='^', c='blue') plt.title('Scatter Plot with Triangle Markers') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.show()
In this example, the markers are changed to triangles, showcasing how marker style can affect the appearance of the scatter plot.
3. Exercises:
Exercice: 1
1. Use the following data to create a scatter plot:
X-axis data: [1, 3, 4, 5, 6]
Y-axis data: [3, 2, 5, 6, 8]
2. Add titles and labels to the plot.
Exercice: 2
1. Create a scatter plot with the following data:
X-axis data: [1, 2, 3, 4, 5]
Y-axis data: [5, 7, 9, 11, 13]
Sizes: [50, 100, 150, 200, 250]
Colors: ['cyan', 'magenta', 'yellow', 'black', 'gray']
2. Customize the marker style to circles ('o').
Exercice: 3
1. Create a scatter plot using:
X-axis data: [10, 20, 30, 40, 50]
Y-axis data: [15, 25, 35, 45, 55]
2. Use different marker styles for each point (you can alternate between circles and squares).
3. Add titles and labels.
Conclusion
In this lesson, we explored how to create scatter plots using Matplotlib. We learned how to customize the markers, sizes, and colors for better visual representation. In the next lesson, we will delve into additional techniques for visualizing data in different formats using Matplotlib.