Sometimes, we want to set color as a function of a third variable in a scatterplot with Python Matplotlib.
In this article, we’ll look at how to set color as a function of a third variable in a scatterplot with Python Matplotlib.
How to set color as a function of a third variable in a scatterplot with Python Matplotlib?
To set color as a function of a third variable in a scatterplot with Python Matplotlib, we call scatter with the c argument.
For instance, we write
import numpy as np
import matplotlib.pyplot as plt
x = np.random.random(10)
y = np.random.random(10)
plt.scatter(x, y, c=y, s=500)
plt.gray()
plt.show()
We generate data for the x and y axes with
x = np.random.random(10)
y = np.random.random(10)
Then we call scatter with x, y and c set to y to set the color to the y value.
Then we call gray to make the plot grayscale.
Finally, we call show to show the plot.
Conclusion
To set color as a function of a third variable in a scatterplot with Python Matplotlib, we call scatter with the c argument.