Scatter
What You'll Learn: In this tutorial, you'll discover how to use Matplotlib's scatter() function to draw scatter plots. We'll cover creating scatter plots, comparing plots, customizing colors, coloring each dot individually, and adjusting transparency.
Creating Scatter Plots
With Pyplot, you can use the scatter() function to draw a scatter plot. This function plots one dot for each observation, using two arrays of the same length for the x and y values.
Example: A simple scatter plot
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()
Result:
This example plots the age of cars on the x-axis and their speed on the y-axis.
Compare Plots
You can compare multiple scatter plots on the same figure to see if there are any patterns or relationships.
Example: Draw two plots on the same figure
import matplotlib.pyplot as plt
import numpy as np
# Day one
x1 = np.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])
y1 = np.array([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])
plt.scatter(x1, y1)
# Day two
x2 = np.array([2, 2, 8, 1, 15, 8, 12, 9, 7, 3, 11, 4, 7, 14, 12])
y2 = np.array([100, 105, 84, 105, 90, 99, 90, 95, 94, 100, 79, 112, 91, 80, 85])
plt.scatter(x2, y2)
plt.show()
Result:
This example compares the age and speed of cars on two different days.
Customizing Colors
You can set your own color for each scatter plot using the color or c argument.
Example: Set your own color for the markers
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, color='hotpink')
x = np.array([2, 2, 8, 1, 15, 8, 12, 9, 7, 3, 11, 4, 7, 14, 12])
y = np.array([100, 105, 84, 105, 90, 99, 90, 95, 94, 100, 79, 112, 91, 80, 85])
plt.scatter(x, y, color='#88c999')
plt.show()
Result:
Coloring Each Dot
You can set a specific color for each dot using an array of colors as the value for the c argument.
Example: Set your own color for each marker
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])
colors = np.array(["red", "green", "blue", "yellow", "pink", "black", "orange", "purple", "beige", "brown", "gray", "cyan", "magenta"])
plt.scatter(x, y, c=colors)
plt.show()
Result:
Adjusting Transparency
You can adjust the transparency of the dots with the alpha argument.
Example: Set your own size for the markers and adjust transparency
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])
sizes = np.array([20, 50, 100, 200, 500, 1000, 60, 90, 10, 300, 600, 800, 75])
plt.scatter(x, y, s=sizes, alpha=0.5)
plt.show()
Result:
Try It Yourself: Fun Exercises
- Create a Scatter Plot:
- Create a scatter plot with your own data and customize the colors and sizes of the markers.
- Compare Multiple Plots:
- Compare multiple scatter plots on the same figure to see if there are any patterns or relationships.
- Adjust Transparency:
- Experiment with the
alphaargument to adjust the transparency of the dots.
- Experiment with the
Summary:
In this tutorial, we learned how to create and customize scatter plots using Matplotlib in Python. We explored creating scatter plots, comparing plots, customizing colors, coloring each dot individually, and adjusting transparency. Keep experimenting and have fun with Matplotlib in Python!