Skip to content

Use matplotlib background colors when converting from matplotlib - #5285

Open
robertoffmoura wants to merge 2 commits into
plotly:mainfrom
robertoffmoura:rm/default-plot_bgcolor-white
Open

Use matplotlib background colors when converting from matplotlib#5285
robertoffmoura wants to merge 2 commits into
plotly:mainfrom
robertoffmoura:rm/default-plot_bgcolor-white

Conversation

@robertoffmoura

Copy link
Copy Markdown
Contributor

Code PR

  • I have read through the contributing notes and understand the structure of the package. In particular, if my PR modifies code of plotly.graph_objects, my modifications concern the code generator and not the generated files.
  • I have added tests or modified existing tests.
  • For a new feature, I have added documentation examples (please see the doc checklist as well).
  • I have added a CHANGELOG entry if changing anything substantial.
  • For a new feature or a change in behavior, I have updated the relevant docstrings in the code.

@gvwilson gvwilson self-assigned this Aug 1, 2025
@gvwilson gvwilson added feature something new P1 needed for current cycle community community contribution labels Aug 1, 2025
@robertoffmoura
robertoffmoura force-pushed the rm/default-plot_bgcolor-white branch 2 times, most recently from 373d9e7 to 18abcb6 Compare August 12, 2025 09:31
@emilykl

emilykl commented Oct 1, 2025

Copy link
Copy Markdown
Contributor

Hi @robertoffmoura , the default for layout.plot_bgcolor is already white, do you have an example of where the current default is not working?

@robertoffmoura
robertoffmoura force-pushed the rm/default-plot_bgcolor-white branch from 18abcb6 to 2ac7771 Compare October 9, 2025 15:42
@robertclaus

Copy link
Copy Markdown

@robertoffmoura given the age and last comment in this PR - should it be closed?

@robertoffmoura
robertoffmoura force-pushed the rm/default-plot_bgcolor-white branch from 2ac7771 to 644d12c Compare August 10, 2026 11:31
@robertoffmoura robertoffmoura changed the title Set plot_bgcolor to white by default when converting from matplotlib Use matplotlib background colors when converting from matplotlib Aug 10, 2026
@robertoffmoura

Copy link
Copy Markdown
Contributor Author

Hi @robertclaus,

I updated the PR to set plot_bgcolor and paper_bgcolor based on fig.patch.get_facecolor() and props["axesbg"].

The below snippet prints None None for both figures before the change, and prints the expected color after the change.

Snippet:

import matplotlib.pyplot as plt
import plotly.tools as tls

# default matplotlib backgrounds -> white
fig1, ax1 = plt.subplots()
ax1.plot([0, 1], [0, 1])
p1 = tls.mpl_to_plotly(fig1)
print(p1.layout.plot_bgcolor, p1.layout.paper_bgcolor)  # #FFFFFF #FFFFFF
fig1.savefig("matplotlib.png")
p1.write_image("plotly.png")

# custom backgrounds -> faithfully preserved
fig2, ax2 = plt.subplots()
fig2.patch.set_facecolor("lightyellow")
ax2.set_facecolor("lightgray")
ax2.plot([0, 1], [0, 1])
p2 = tls.mpl_to_plotly(fig2)
print(p2.layout.plot_bgcolor, p2.layout.paper_bgcolor)  # #D3D3D3 #FFFFE0

Here's also a screenshot of the matplotlib figure
matplotlib

And the plotly converted figure before the change (unexpected light blue grey background)
plotly

And after the change (expected white background)
plotly2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community community contribution feature something new P1 needed for current cycle

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants