Change Color of Data Points Based on Variable in Matplotlib



To change the color of data points based on some variable in matplotlib, we can take the following steps −

  • Create x, y and c variables using numpy.

  • Plot the scatter points using x, y and for color, use c (Step 1).

  • To display the image, use the show() method.

Example

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(1, 20, 50)
y = np.log(x)
c = np.random.randint(x)
plt.scatter(x, y, c=c)
plt.show()

Output

Updated on: 2021-04-09T08:32:15+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements