15 Feb Change the background color of the Matplotlib legend
The set_facecolor() function in Matplotlib is used to set the background color of the legend. Let us see an example wherein we will set the legend color to red, which displays the Frequency and Periods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import numpy as np import matplotlib.pyplot as plt # Data a = np.arange(5) b = [2,4,6,8,10] c = [5, 6, 7, 8, 9] # Create plots fig = plt.figure() ax = plt.subplot() ax.plot(a, b, 'k--', label='Frequency') ax.plot(a, c, 'k:', label='Periods') # Create a legend using the Matplotlib Axes.legend() method in Python. # Set the position using the loc parameter of the legend() method legend = ax.legend(loc='upper center') # Set the background color of the legend using the set_facecolor() function legend.get_frame().set_facecolor('red') # Plot Title plt.title("Frequency of a Signal") # Display plt.show() |
Output
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
No Comments