Show Sidebar Hide Sidebar

Backgrounds in matplotlib

How to customize backgrounds in matplotlib. Examples of different background options available in matplotlib and plotly.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Plotly's python package is updated frequently. Run pip install plotly --upgrade to use the latest version.

In [1]:
import plotly
plotly.__version__
Out[1]:
'3.1.1'

Background Color

In [2]:
import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

mpl_fig = plt.figure()
ax = mpl_fig.add_subplot(111)

x = np.linspace(-10,10,num=50)
y = np.sin(x)

line, = ax.plot(x, y, lw=1)

ax.set_title("Plot With Background Color")

plotly_fig = tls.mpl_to_plotly( mpl_fig )
plotly_fig["layout"].update({"plot_bgcolor":"rgb(182, 215, 168)"})

py.iplot(plotly_fig)
Out[2]:

Transparent Background

In [3]:
import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

mpl_fig = plt.figure()
ax = mpl_fig.add_subplot(111)

x = np.linspace(-10,10,num=50)
y = np.sin(x)

line, = ax.plot(x, y, lw=1)

ax.set_title("Plot With Transparent Background")

plotly_fig = tls.mpl_to_plotly( mpl_fig )
plotly_fig["layout"].update({
                             "plot_bgcolor":"rgba(0, 0, 0, 0)", 
                             "paper_bgcolor":"rgba(0,0,0,0)"
                             })


py.iplot(plotly_fig)
Out[3]:

Reference

See https://plot.ly/python/images/ for more information and chart attribute options!

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.