Matplotlib provides a way to implement color coding using the c
parameter in its plotting functions. The c
parameter can take various forms, such as a list or array of values, or a string representing the name of a column in a pandas dataframe.
For example, if we have a scatter plot with three different groups of data, we can use the c
parameter to color code each group differently:
import matplotlib.pyplot as plt
import numpy as np
# Generating some data
x = np.random.rand(50)
y = np.random.rand(50)
groups = np.random.choice(['Group A', 'Group B', 'Group C'], size=50)
# Scatter plot with color coding
scatter = plt.scatter(x, y, c=groups)
# Add a legend
plt.legend(*scatter.legend_elements(), title="Groups")
This code generates a scatter plot with 50 points, where each point belongs to one of three groups (Group A, Group B, or Group C). The c
parameter is set to the groups
array, which assigns a different color to each group. The resulting plot shows each point colored according to its group membership, with a legend indicating the color coding.
Asked: 2022-04-16 11:00:00 +0000
Seen: 12 times
Last updated: Feb 20 '22