Skip to content

Commit 6a44329

Browse files
committed
Add update_layout figure method to update a figure's layout
1 parent 641005f commit 6a44329

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

packages/python/plotly/plotly/basedatatypes.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,29 @@ def update_traces(
859859
trace.update(patch, **kwargs)
860860
return self
861861

862+
def update_layout(self, dict1=None, **kwargs):
863+
"""
864+
Update the properties of the figure's layout with a dict and/or with
865+
keyword arguments.
866+
867+
This recursively updates the structure of the original
868+
layout with the values in the input dict / keyword arguments.
869+
870+
Parameters
871+
----------
872+
dict1 : dict
873+
Dictionary of properties to be updated
874+
kwargs :
875+
Keyword/value pair of properties to be updated
876+
877+
Returns
878+
-------
879+
BaseFigure
880+
The Figure object that the update_layout method was called on
881+
"""
882+
self.layout.update(dict1, **kwargs)
883+
return self
884+
862885
def _select_layout_subplots_by_prefix(
863886
self,
864887
prefix,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from unittest import TestCase
2+
import plotly.graph_objects as go
3+
4+
5+
class TestUpdateLayout(TestCase):
6+
7+
def test_update_layout_kwargs(self):
8+
# Create initial figure
9+
fig = go.Figure()
10+
fig.layout.title.font.size = 10
11+
12+
# Grab copy of original figure
13+
orig_fig = go.Figure(fig)
14+
15+
fig.update_layout(title_font_family='Courier New')
16+
orig_fig.layout.update(title_font_family='Courier New')
17+
self.assertEqual(fig, orig_fig)
18+
19+
def test_update_layout_dict(self):
20+
# Create initial figure
21+
fig = go.Figure()
22+
fig.layout.title.font.size = 10
23+
24+
# Grab copy of original figure
25+
orig_fig = go.Figure(fig)
26+
27+
fig.update_layout(dict(title=dict(font=dict(family='Courier New'))))
28+
orig_fig.layout.update(title_font_family='Courier New')
29+
self.assertEqual(fig, orig_fig)

0 commit comments

Comments
 (0)