How to get the color of the most recent plotted line in Python?

MatplotlibPythonServer Side ProgrammingProgramming

To get the color of the most recent plotted line, we can take the following steps −

  • Create x and y points using numpy.

  • Plot the line using x and y, with color red and linewidth 2.

  • To get the color of the line, use the get_color() method, and print it.

  • To display the figure, 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, 10, 1000)
y = np.linspace(10, 20, 1000)
line, = plt.plot(x, y, c="red", lw=2)
print("Color of the most recent plot line: ", line.get_color())
plt.show()

Output

Color of the most recent plot line: red
raja
Published on 09-Apr-2021 12:33:07
Advertisements