0

UPDATE: Although there is a similar discussion here I'm keeping my question given the confusing/non-intuitive attribute that needs to be used to change a subplot background color. Hopefully the language I use to describe the issue will further help some confused coders.

BACKGROUND: I need to plot a series of charts, with various formats and data. When using Matplotlib's standard style, here is an example of what I get. Note that both the FIGURE and SUBPLOTS backgrounds are all white.

All white background

With rcParams I can quickly change the set-up for all my charts, which avoids me having to change the format for each chart individually. It also helps me to keep a certain standard on my charts, making them look professional.

THE ISSUE: As all my spreadsheets (see here a code I wrote to convert Excel spreadsheets to dark-format), browsers, etc are all in dark-format, I wanted to have my chart in Python also in dark format. Using rcParams was the best way to do it, given how small the code is:

import matplotlib.pyplot as plt rgb_color = [51,51,51] #color in RGB format rgb_decimal_1 = [x / 255.0 for x in rgb_color] #RGB color in "decimals" #set colors for all charts parameters params = {'text.color':'white', 'xtick.color':'white', 'ytick.color':'white', 'figure.facecolor':rgb_decimal_1} plt.rcParams.update(params) 

With this simple code, I change almost all the colors I needed to make my charts dark. There is one part, however, I can't figure out a way to do: the SUBPLOTs background. See how the same charts look when ruing the code above. Observe that although the FIGURE background is now dark (and the letters are in white, etc), the SUBPLOT backgrounds are still white:

Figure dark background

Note that I know how to make the subplots dark. If I add the simple code below, when plotting each subplot, I can make the subplot background dark. See the output just after the code:

rgb_color = [41,41,41] #color in RGB format rgb_decimal = [x / 255.0 for x in rgb_color] #RGB color in decimal format chart_p.set_facecolor((rgb_decimal)) #set color background of each chart subplot 

All dark background

Having to include the code above on each subplot however is exactly what I want to avoid. The documentation on how to use rcParams is very good (see it here). But I can't find a setup for the SUBPLOTs backgrounds. I was expecting on the "figure subplot parameters" something like figure.subplot.facecolor but it doesn't exist.

Am I missing something obvious? I know that there is a way to set the SUBPLOTs background color since if I use the style "dark background" [plt.style.use('dark_background')] the charts SUBPLOT background goes to black. Close but not the color that I want.

3
  • The attribute is 'axes.facecolor'. Add 'axes.facecolor': 'black' (or whatever color) to your params. Commented Oct 27, 2019 at 14:47
  • Possible duplicate of Change default background color for matplotlib plots Commented Oct 27, 2019 at 14:49
  • I just included a quick update on the top of my question re: the duplicate you mention. Indeed they discuss similar items. However, given the non-intuitive attribute name, I decided to leave my question here in case it helps someone find the issue more easily. Commented Oct 31, 2019 at 16:55

1 Answer 1

0

It appears that I was missing something obvious! Thank you @busybear for answering my question in two minutes! The attribute I should use is 'axes.facecolor' (what is sad is that I looked at all attributes with "facecolor" in it and was certain that the 'axes.facecolor' was not it...). Below is the new code that now makes all my charts subplots with a dark background:

import matplotlib.pyplot as plt rgb_color = [51,51,51] #color in RGB format rgb_decimal_1 = [x / 255.0 for x in rgb_color] #RGB color in "decimals" rgb_color = [41,41,41] #color in RGB format rgb_decimal_2 = [x / 255.0 for x in rgb_color] #RGB color in decimal format #set colors for all charts parameters params = {'text.color':'white', 'xtick.color':'white', 'ytick.color':'white', 'figure.facecolor':rgb_decimal_1, 'axes.facecolor':rgb_decimal_2} plt.rcParams.update(params) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.