Matplotlib – Change the line color

To change the line color of a line graph in Matplotlib, use the color parameter of the plot() function. Let us see an example:

# Change the line color

import matplotlib.pyplot as plt
import numpy as np

# Data
a = np.arange(5) # [0 1 2 3 4]
b = [2, 4, 6, 8, 10]
c = [5, 6, 7, 8, 9]

# Create a plot
ax = plt.subplot()

ax.plot(a, b, 'k--', label='Frequency', color='red')
ax.plot(a, c, 'k:', label='Periods',color='#4285F4')

# legend
legend = ax.legend(loc='upper center')
legend.get_frame().set_facecolor('orange')

plt.title("Frequency of a signal")

plt.show()

Output

Change the line color in Matplotlib


If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read more:

Top Matplotlib Interview Questions and Answers (MCQs)
Matplotlib - Change the line width
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment