Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib - Creating plot for black background presentation slides

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

like image 613
stackoverflowuser2010 Avatar asked Jan 22 '18 22:01

stackoverflowuser2010


People also ask

How do I plot black and white in matplotlib?

To plot black-and-white binary map in matplotlib, we can create and add two subplots to the current figure using subplot() method, where nrows=1 and ncols=2. To display the data as a binary map, we can use greys colormap in imshow() method.

How do I change the color on a matplotlib plot?

The usual way to set the line color in matplotlib is to specify it in the plot command. This can either be done by a string after the data, e.g. "r-" for a red line, or by explicitely stating the color argument.


1 Answers

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.

like image 119
ImportanceOfBeingErnest Avatar answered Oct 16 '22 09:10

ImportanceOfBeingErnest