Show Sidebar Hide Sidebar

Backgrounds in matplotlib

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

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.plot(plotly_fig)
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.plot(plotly_fig)
Still need help?
Contact Us

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