How to Set Plot Background Color in Matplotlib?

11 Dec, 2020
Prerequisites: 
Matplotlib
Numpy
From the below figure one can infer that a plot consists of X-axis, Y-axis, plot title and the axes. By default, the color of the plot is white. If we have to set the background color of the plot so that our plot looks beautiful, we have to make the axes object, by using axes() attribute after plotting the graph.
Approach:
Import module
Load or create data
Plot a regular graph
Create axes object
Set attribute set_facecolor() to the required color. This attribute accepts both name or color code of the color
Follow the given examples to understand better.
Example: Default color plot
Python
# importing libraryimport matplotlib.pyplot as plt  # giving values for x and y to plotstudent_marks = [50, 60, 70, 80, 90]student_grade = ['B', 'B', 'B+', 'B+', 'A']  plt.plot(student_marks, student_grade)  # Giving x label using xlabel() method# with bold settingplt.xlabel("student_marks", fontweight='bold')  # Giving y label using xlabel() method# with bold settingplt.ylabel("student_grade", fontweight='bold')  # Giving title to the plotplt.title("Student Marks v/s Student Grade")  # Showing the plot using plt.show()plt.show()
Output:
Example 2 : Setting background color to yellow
Python
# importing libraryimport matplotlib.pyplot as plt  # giving values for x and y to plotstudent_marks = [50, 60, 70, 80, 90]student_grade = ['B', 'B', 'B+', 'B+', 'A']plt.plot(student_marks, student_grade)  # Giving x label using xlabel() method# with bold settingplt.xlabel("student_marks", fontweight='bold')ax = plt.axes()  # Setting the background color of the plot # using set_facecolor() methodax.set_facecolor("yellow")  # Giving y label using xlabel() method # with bold settingplt.ylabel("student_grade", fontweight='bold')  # Giving title to the plotplt.title("Student Marks v/s Student Grade")  # Showing the plot using plt.show()plt.show()
Output:
Example 3: Setting background color to violet
Python
# importing librariesimport matplotlib.pyplot as pltimport numpy as np  # giving values for x and y to plotx = np.arange(0, 10, .1)y = np.sin(x)plt.plot(x, y)  # Giving x label using xlabel() method # with bold settingplt.xlabel("X")ax = plt.axes()  # Setting the background color of the# plot using set_facecolor() methodax.set_facecolor("violet")  # Giving y label using xlabel() method # with bold settingplt.ylabel('sin(x)')  # Showing the plot using plt.show()plt.show()
Output:
We can also set the color of the outer portion of the plot. To set both the color for plot background and for outer portion of the plot the only change we have to do in our code is that we have to add plt.figure(faceccolor=’color’) before plotting the graph.
Example 1:
Python
# importing librariesimport matplotlib.pyplot as pltimport numpy as np  # giving values for x and y to plotx = np.arange(0, 10, .1)y = np.sin(x)  # Set background color of the outer # area of the pltplt.figure(facecolor='yellow')  # Plotting the graph between x and yplt.plot(x, y)  # Giving x label using xlabel() method# with bold settingplt.xlabel("X")ax = plt.axes()  # Setting the background color of the plot # using set_facecolor() methodax.set_facecolor("violet")  # Giving y label using xlabel() method with# bold settingplt.ylabel('sin(x)')  # Showing the plot using plt.show()plt.show()
Output:
Example 2: Setting plot color using html color codes
Python
# importing librariesimport matplotlib.pyplot as pltimport numpy as np  # giving values for x and y to plotx = np.arange(0, 10, .1)y = np.sin(x)  # Set background color of the outer# area of the pltplt.figure(facecolor='#94F008')  # Plotting the graph between x and yplt.plot(x, y)  # Giving x label using xlabel() method# with bold settingplt.xlabel("X")ax = plt.axes()  # Setting the background color of the plot # using set_facecolor() methodax.set_facecolor("#1CC4AF")  # Giving y label using xlabel() method with# bold settingplt.ylabel('sin(x)')  # Showing the plot using plt.show()plt.show()
Output:
Python-matplotlib
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.