Matplotlib is a powerful data visualization library in Python that provides an extensive range of tools for creating high-quality 2D and 3D plots. One common requirement when creating scatter plots is to assign different colors to each series of points. This tutorial will guide you through the process of customizing scatter plot colors using Matplotlib.
Introduction to Scatter Plots
A scatter plot is a type of plot that displays the relationship between two variables as a collection of points on a grid. Each point represents a single observation, and the x and y coordinates of the point correspond to the values of the two variables being plotted.
Basic Scatter Plot Example
To create a basic scatter plot using Matplotlib, you can use the scatter
function:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [4, 8, 12, 16]
plt.scatter(x, y)
plt.show()
This code will generate a simple scatter plot with default settings.
Customizing Scatter Plot Colors
To assign different colors to each series of points in a scatter plot, you can use the color
parameter of the scatter
function. For example:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y1 = [4, 8, 12, 16]
y2 = [1, 4, 9, 16]
plt.scatter(x, y1, color='red')
plt.scatter(x, y2, color='blue')
plt.show()
This code will generate a scatter plot with two series of points, one in red and the other in blue.
Using Color Maps
When dealing with multiple series of points, it can be challenging to manually assign colors to each series. In this case, you can use a color map to automatically generate a range of colors for your scatter plot. Matplotlib provides several built-in color maps that you can use:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(10)
ys = [i + x + (i * x) ** 2 for i in range(10)]
colors = plt.cm.rainbow(np.linspace(0, 1, len(ys)))
for y, c in zip(ys, colors):
plt.scatter(x, y, color=c)
plt.show()
This code will generate a scatter plot with multiple series of points, each assigned a unique color from the rainbow
color map.
Using Itertools to Cycle Through Colors
Another approach to customizing scatter plot colors is to use the itertools
module to cycle through a list of colors. This can be useful when you want to reuse a set of colors for multiple series:
import matplotlib.pyplot as plt
import itertools
import numpy as np
x = np.arange(10)
ys = [i + x + (i * x) ** 2 for i in range(10)]
colors = itertools.cycle(['r', 'b', 'g'])
for y in ys:
plt.scatter(x, y, color=next(colors))
plt.show()
This code will generate a scatter plot with multiple series of points, each assigned a color from the list ['r', 'b', 'g']
in a cyclical manner.
Changing Colors After Plotting
If you have already plotted your data and want to change the colors, you can use the set_color
method to update the colors of the scatter plot:
import matplotlib.pyplot as plt
from random import randint
import numpy as np
x = [[randint(0, 50) for _ in range(5)] for _ in range(24)]
y = [[randint(0, 50) for _ in range(5)] for _ in range(24)]
fig = plt.figure()
ax = fig.add_subplot(111)
for x_val, y_val in zip(x, y):
ax.scatter(x_val, y_val)
colormap = plt.cm.gist_ncar
colorst = [colormap(i) for i in np.linspace(0, 0.9, len(ax.collections))]
for t, j1 in enumerate(ax.collections):
j1.set_color(colorst[t])
plt.show()
This code will generate a scatter plot with multiple series of points and then update the colors using a color map.
Conclusion
Customizing scatter plot colors is an essential aspect of data visualization. By using Matplotlib’s built-in features, such as color maps and iterators, you can create visually appealing and informative scatter plots that effectively communicate your data insights.