banner



How To Set Plot Background Color

In this tutorial, nosotros'll look at how to change the background color of plots in matplotlib. Note that, the background color here means the groundwork colour of the region divisional by the axes. Also referred to as the axes face colour.

To change the background color of matplotlib plots, you can apply the set_facecolor() function of the axes object of the plot. Y'all can also gear up a global confront color for all plots using rcParams. (Come across the syntax and examples below). The following is the syntax:

# gear up the face color of an axes object ax.set_facecolor('orangish') # set the confront color globally for all axes objects plt.rcParams.update({'axes.facecolor':'orange'})

The in a higher place syntax assumes matplotlib.pyplot is imported as plt.
In the higher up syntax, nosotros first have the lawmaking to set the confront color of a specific axes object and then we have the code to fix the face up color globally for all plots.

Let's look at some examples of using the above syntax to change the background color in matplotlib plots. First, we'll create a simple matplotlib line plot and see the default face color of the plot.

import matplotlib.pyplot as plt # reset the plot configurations to default plt.rcdefaults()  # number of employees of A emp_count = [three, 20, 50, 200, 350, 400] year = [2014, 2015, 2016, 2017, 2018, 2019]  # plot a line nautical chart fig, ax = plt.subplots() ax.plot(year, emp_count) # set up axis titles ax.set_xlabel("Year") ax.set_ylabel("Employees") # set up chart championship ax.set_title("Employee Growth at A") plt.show()

Output:

Line chart in matplotlib with default face color

You tin run into that in the above plot the default face color is white. Let'due south go alee and add a custom face color to our plot. There are multiple ways to add custom axes face color to our plots – You lot can gear up a custom face up colour individually for your axes objects or you can add together a default confront color for all the plots using rcParams.

To set the face color of individual plots, utilise the axes object'southward set_facecolor() function.

# plot a line chart with custom axes face up colour fig, ax = plt.subplots() ax.plot(year, emp_count)  # ready axis titles ax.set_xlabel("Twelvemonth") ax.set_ylabel("Employees") # ready nautical chart title ax.set_title("Employee Growth at A") # set axes facecolor ax.set_facecolor("orange") plt.prove()

Output:

Line plot with orange face color

In the above example, nosotros set the face up color of the ax object to "orange" and you can encounter that the resulting plot has an orangish background in region bounded by the axes.

Y'all can too set the default axes face colour to exist used in all your plots by updating the rcParams configurations.

# reset the plot configurations to default plt.rcdefaults() # set the axes color glbally for all plots plt.rcParams.update({'axes.facecolor':'lightblue'})  # plot a line chart fig, ax = plt.subplots() ax.plot(twelvemonth, emp_count) # gear up centrality titles ax.set_xlabel("Year") ax.set_ylabel("Employees") # set up chart title ax.set_title("Employee Growth at A") plt.prove()

Output:

Line plot with light blue face color

Note that first, we used the rcdefaults() office to reset the matplotlib configurations to their defaults, then went ahead to update our default axes face up color to "lightblue". The resulting plot has a "lightblue" axes face color.

Since we have updated the default axes face color, all the following plots volition have the same axes face color ("lightblue" in our case). Allow'southward create a different plot to run into this in activeness.

# plot a line nautical chart fig, ax = plt.subplots() ax.scatter(year, emp_count) # set centrality titles ax.set_xlabel("Year") ax.set_ylabel("Employees") # set chart title ax.set_title("Employee Growth at A") plt.show()
Scatte plot with light blue face color

The above scatter plot has "lightblue" axes confront color.

With this, we come to the end of this tutorial. The lawmaking examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having matplotlib version iii.ii.2


Subscribe to our newsletter for more informative guides and tutorials.
Nosotros do not spam and you can opt out whatever fourth dimension.

  • Piyush is a information scientist passionate about using data to understand things better and make informed decisions. In the past, he'southward worked every bit a Data Scientist for ZS and holds an applied science degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

How To Set Plot Background Color,

Source: https://datascienceparichay.com/article/change-background-color-of-plot-matplotlib/

Posted by: cannonbenty1991.blogspot.com

0 Response to "How To Set Plot Background Color"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel