Salutations,
I want to change the OUTER colour of a plot in matplotlib. I can find MANY MANY MANY examples of how to change the inner bg colour but the outer bg colour refuses to change.
The white part of the image is what i mean by "outer" background. The colour on the extreme outside is just to constrast the white so it doesn't blend in with stack overflows background and is not part of the plot/chart.
I have tried using set_facecolor()
to change it but it doesn't work.
# Setting up data
x = list(history['daily'].keys())[-30*months:]
x_av = list(history['average'].keys())[-30*months:]
y = list(history['daily'].values())[-30*months:]
y_av = list(history['average'].values())[-30*months:]
# Setting up figure
fig = pyplot.figure()
fig.set_figheight(6)
fig.set_figwidth(12)
# Formatting figure
fig.patch.set_antialiased(True)
# Setting up axis
ax = fig.add_subplot(111, axisbg='#2e3136')
ax.plot(x, y, '#e1bb34', x_av, y_av, '#b2dbee')
# Formatting axis
ax.get_yaxis().grid(color='#3e4146', linestyle='-')
ax.get_xaxis().grid(color='#3e4146', linestyle='-')
ax.spines['bottom'].set_color('#1e2124')
ax.spines['top'].set_color('#1e2124')
ax.spines['left'].set_color('#1e2124')
ax.spines['right'].set_color('#1e2124')
ax.get_yaxis().set_major_formatter(ticker.FuncFormatter(lambda _y, p: '{:,}'.format(_y)))
ax.get_xaxis().set_major_formatter(ticker.FuncFormatter(lambda _x, p: datetime.fromtimestamp(_x/1000.0).strftime('%b %d')))
[i.set_color('white') for i in pyplot.gca().get_xticklabels()]
[i.set_color('white') for i in pyplot.gca().get_yticklabels()]
buf = BytesIO()
fig.savefig(buf, format='png', bbox_inches='tight')
pyplot.close()
buf.seek(0)
You should be able to set facecolor
as a kwarg to savefig
, this will ensure it is set when the image is written.
fig.savefig(buf, format='png', bbox_inches='tight', facecolor='#2e3136')
To pull the color from the axis:
fig.savefig(buf, format='png', bbox_inches='tight', facecolor=ax.get_axis_bgcolor())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With