How to Set Plot Background Color in Matplotlib? (original) (raw)

Last Updated : 15 Jan, 2025

**Prerequisites:

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:

Follow the given examples to understand better.

**Example: Default color plot

Python `

import matplotlib.pyplot as plt

student_marks = [50, 60, 70, 80, 90] student_grade = ['B', 'B', 'B+', 'B+', 'A']

plt.plot(student_marks, student_grade) plt.xlabel("student_marks", fontweight='bold') plt.ylabel("student_grade", fontweight='bold') plt.title("Student Marks v/s Student Grade") plt.show()

`

**Output:

**Example 2 : Setting background color to yellow

Python `

import matplotlib.pyplot as plt

grade_mapping = {'B': 2, 'B+': 3, 'A': 4} student_marks = [50, 60, 70, 80, 90] student_grade = ['B', 'B', 'B+', 'B+', 'A'] numeric_grades = [grade_mapping[grade] for grade in student_grade]

Plotting the data

plt.plot(student_marks, numeric_grades, color='black') plt.xlabel("Student Marks", fontweight='bold') plt.ylabel("Student Grade", fontweight='bold')

ax = plt.gca()

Setting Background colour yellow

ax.set_facecolor("yellow") plt.title("Student Marks v/s Student Grade") plt.yticks(ticks=list(grade_mapping.values()), labels=list(grade_mapping.keys())) plt.show()

`

**Output:

**Example 3: Setting background color to violet

Python `

Importing libraries

import matplotlib.pyplot as plt import numpy as np

Giving values for x and y to plot

x = np.arange(0, 10, 0.1) y = np.sin(x)

Plotting the graph

plt.plot(x, y)

Setting x and y labels

plt.xlabel("X", fontweight='bold') plt.ylabel("sin(x)", fontweight='bold')

Setting the background color of the plot

ax = plt.gca() # Get the current axes ax.set_facecolor("violet")

Showing the plot

plt.show()

`

**Output:

**Setting Outer and Inner color of plot

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 `

import matplotlib.pyplot as plt import numpy as np

Giving values for x and y to plot

x = np.arange(0, 10, 0.1) y = np.sin(x)

Set background color of the outer area of the figure

plt.figure(facecolor='yellow') plt.plot(x, y) plt.xlabel("X", fontweight='bold')

Accessing the current axes and setting its background color

ax = plt.gca() # ax.set_facecolor("violet") plt.ylabel('sin(x)', fontweight='bold') plt.show()

`

**Output:

**Example 2: Setting plot color using html color codes

Python `

import matplotlib.pyplot as plt import numpy as np

x = np.arange(0, 10, 0.1) y = np.sin(x)

Set background color of the outer area of the figure

plt.figure(facecolor='#94F008') plt.plot(x, y) plt.xlabel("X", fontweight='bold')

Accessing the current axes and setting its background color

ax = plt.gca() ax.set_facecolor("#1CC4AF") plt.ylabel('sin(x)', fontweight='bold') plt.show()

`

**Output: