16

I'm successfully using Python and Matplotlib to create transparent PNG figures that look good when I add the plots to a Powerpoint slide that has a white background. Below is an example:

enter image description here

However, when I use a presentation slide deck that has a black background, the figure does not look as good. The text font and lines are all black, and they blend right into the background.

enter image description here

Is there a quick and easy way to generate these figures so that they will look good on a black slide? For example, I'd like to quickly make all the lines and text white. I know I can individually set the colors for the title, axis labels, axis values, etc., but is there a "master theme" for quickly making this kind of change?

Here is what my code generally looks like:

_ = plt.bar(hours, series_two, label='One')
_ = plt.bar(hours, series_one, label='Two')
_ = plt.grid()
_ = plt.xticks(range(0,24))
_ = plt.yticks(np.arange(0, 1.01, 0.05))
_ = plt.ylim(0, 1.0)
_ = plt.xlabel('Hour of day')
_ = plt.ylabel('Value')
_ = plt.tight_layout()
_ = plt.title('Experimental results')
_ = plt.legend()
_ = plt.show()

EDIT: I used plt.style.use("dark_background") from the related questions, but the results look terrible. I just want to change the lines and text, not the colors of the bars.

enter image description here

stackoverflowuser2010
  • 38,621
  • 48
  • 169
  • 217
  • Those are not exact duplicates, but the solution is clearly to change the style to `dark_background`, as shown in both of them. – ImportanceOfBeingErnest Jan 22 '18 at 22:59
  • @ImportanceOfBeingErnest: Thanks. Those other two questions have relevant answers, but the titles of the questions are unrelated to "dark background". That is why I could not find them. – stackoverflowuser2010 Jan 22 '18 at 23:02
  • @ImportanceOfBeingErnest: I updated the question with the "dark backgorund" style, but the results look terrible. So the answers to the other questions did not answer mine. – stackoverflowuser2010 Jan 22 '18 at 23:06
  • 1
    Fine, in that case please clearly state how the different parts of the plots should look like and in how far setting the respective rcParams do not help. – ImportanceOfBeingErnest Jan 22 '18 at 23:09
  • How about this one? https://stackoverflow.com/questions/43000500/change-pyplot-axes-text-color-to-white – DavidG Jan 22 '18 at 23:20

1 Answers1

22

If the predesigned dark_background style does not match the expectations, one may set the respective rcParams manually. The following might produce the desired plot.

import matplotlib.pyplot as plt

plt.rcParams.update({
    "lines.color": "white",
    "patch.edgecolor": "white",
    "text.color": "black",
    "axes.facecolor": "white",
    "axes.edgecolor": "lightgray",
    "axes.labelcolor": "white",
    "xtick.color": "white",
    "ytick.color": "white",
    "grid.color": "lightgray",
    "figure.facecolor": "black",
    "figure.edgecolor": "black",
    "savefig.facecolor": "black",
    "savefig.edgecolor": "black"})

x = range(1,25)
y = range(60,108)[::-2]
y2 = range(16,40)[::-1]

plt.bar(x, y,  label='One')
plt.bar(x, y2, label='Two')
plt.grid()
plt.xticks(x)

plt.xlabel('Hour of day')
plt.ylabel('Value')
plt.title('Experimental results', color="w")
plt.legend()
plt.tight_layout()
plt.show()

enter image description here

Note that you would need to set the title color manually, because the default text color is set to black, such that the legend text is black. Else, one could of course do the inverse and set the legend text color manually.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • This is great having all the parameters in one answer. Thank you. – stackoverflowuser2010 Jan 22 '18 at 23:28
  • the plot background color (background behind the bars) is missing.. – alexzander Mar 08 '21 at 16:04
  • 3
    @alexzander If you want a black background for the axes, set 'axes.facecolor' to black. The answer has it set to white. You might find that the choice of colors doesn't show up well over black and that `plt.style.use("dark_background")` actually works better in many cases. – feedMe Mar 18 '21 at 16:40
  • 1
    yea that one works. also you can use `plt.style.use('Solarize_Light2')`, its very warm and healthy for eyes. – alexzander Mar 19 '21 at 11:56