def assert_update(self, prop, inds, patch, selector=None, row=None, col=None, secondary_y=None): # Copy figure and perform update prefix = "layout_" if prop == "images" else "" fig_orig = go.Figure(self.fig) fig = go.Figure(self.fig) fn = getattr(fig, "update_" + prefix + prop) fn(patch, selector=selector, row=row, col=col, secondary_y=secondary_y) # Get original up updated object lis objs_orig = fig_orig.layout[prop] objs = fig.layout[prop] for i, (obj, obj_orig) in enumerate(zip(objs, objs_orig)): if i in inds: # Check that object changed from original self.assertNotEqual(obj, obj_orig) # Apply update to original and check that they match now obj_orig.update(patch) self.assertEqual(obj, obj_orig) else: # Check object unchanged self.assertEqual(obj, obj_orig)
def test_move_layout_nested_with_existing_template(self): fig = go.Figure( layout={ "font": { "family": "Courier New" }, "title": "Hello", "template": { "layout": { "font": { "family": "Arial", "size": 10 } } }, }) templated_fig = pio.to_templated(fig) expected_fig = go.Figure( layout={ "template": { "layout": { "font": { "family": "Courier New", "size": 10 } } }, "title": "Hello", }) self.assertEqual(templated_fig, expected_fig)
def test_move_layout_nested_properties(self): fig = go.Figure( layout={ "font": { "family": "Courier New" }, "paper_bgcolor": "yellow", "title": "Hello", }) templated_fig = pio.to_templated(fig) # Note that properties named 'title' are not moved to template by # default expected_fig = go.Figure( layout={ "template": { "layout": { "font": { "family": "Courier New" }, "paper_bgcolor": "yellow", } }, "title": "Hello", }) self.assertEqual(templated_fig, expected_fig)
def test_move_layout_nested_properties_no_skip(self): fig = go.Figure( layout={ "font": { "family": "Courier New" }, "paper_bgcolor": "yellow", "title": "Hello", }) templated_fig = pio.to_templated(fig, skip=None) # With skip=None properties named 'title' should be moved to template expected_fig = go.Figure( layout={ "template": { "layout": { "font": { "family": "Courier New" }, "paper_bgcolor": "yellow", "title": "Hello", } } }) self.assertEqual(templated_fig, expected_fig)
def test_move_named_annotation_property(self): fig = go.Figure( layout={ "annotations": [ { "arrowcolor": "blue", "text": "First one", "font": { "size": 23 } }, { "arrowcolor": "green", "text": "Second one", "font": { "family": "Rockwell" }, "name": "First", }, ] }) templated_fig = pio.to_templated(fig) expected_fig = go.Figure( layout={ "annotations": [{ "text": "First one" }, { "name": "First" }], "template": { "layout": { "annotationdefaults": { "arrowcolor": "blue", "font": { "size": 23 }, }, "annotations": [{ "arrowcolor": "green", "font": { "family": "Rockwell" }, "text": "Second one", "name": "First", }], } }, }) self.assertEqual(templated_fig, expected_fig)
def test_move_nested_trace_properties(self): fig = go.Figure( data=[ go.Bar(y=[1, 2, 3], marker={ "opacity": 0.6, "color": "green" }), go.Scatter(x=[1, 3, 2], marker={ "size": 30, "color": [1, 1, 0] }), go.Bar(y=[3, 2, 1], marker={ "opacity": 0.4, "color": [1, 0.5, 0] }), ], layout={"barmode": "group"}, ) templated_fig = pio.to_templated(fig) expected_fig = go.Figure( data=[ go.Bar(y=[1, 2, 3]), go.Scatter(x=[1, 3, 2], marker={"color": [1, 1, 0]}), go.Bar(y=[3, 2, 1], marker={"color": [1, 0.5, 0]}), ], layout={ "template": { "data": { "scatter": [go.Scatter(marker={"size": 30})], "bar": [ go.Bar(marker={ "opacity": 0.6, "color": "green" }), go.Bar(marker={"opacity": 0.4}), ], }, "layout": { "barmode": "group" }, } }, ) self.assertEqual(pio.to_json(templated_fig), pio.to_json(expected_fig))
def scatter3D_MESH(*, x, y, z, col, cmax=100, cmin=0, scat=None, onlyMesh=False, visible=True): data = ( [go.Scatter3d(marker=dict(color=col), x=x, y=y, z=z, mode="markers")] if scat is None else [scat]) if not onlyMesh else [] return go.Figure(data=data + [ go.Mesh3d(cmax=cmax, cmin=cmin, colorscale="BlueRed", showscale=False, x=x, y=y, z=z, intensity=col, hoverinfo="none", visible=visible) ])
def test_update_data_empty(self): # Create figure with empty data (no traces) figure = go.Figure(layout={"width": 1000}) # Update data with new traces figure.update(data=[go.Scatter(y=[2, 1, 3]), go.Bar(y=[1, 2, 3])]) # Build expected dict expected = { "data": [ { "y": [2, 1, 3], "type": "scatter" }, { "y": [1, 2, 3], "type": "bar" }, ], "layout": { "width": 1000 }, } # Compute expected figure dict (pop uids for comparison) result = figure.to_dict() # Perform comparison self.assertEqual(result, expected)
def test_init_in_property_assignment(self): fig = go.Figure() fig.layout.template = go.layout.Template( layout={"title": { "text": "Hello, world" }}) self.assertEqual( fig.layout.template, go.layout.Template(layout={"title": { "text": "Hello, world" }}), ) self.assertEqual( fig.to_dict(), { "data": [], "layout": { "template": { "layout": { "title": { "text": "Hello, world" } } } }, }, )
def test_defaults_in_constructor(self): fig = go.Figure( layout={"template": { "layout": { "imagedefaults": { "sizex": 500 } } }}) self.assertEqual(fig.layout.template.layout.imagedefaults, go.layout.Image(sizex=500)) self.assertEqual( fig.to_dict(), { "data": [], "layout": { "template": { "layout": { "imagedefaults": { "sizex": 500 } } } }, }, )
def test_init_in_figure_constructor(self): fig = go.Figure(layout={ "template": { "layout": { "title": { "text": "Hello, world" } } } }) self.assertEqual( fig.layout.template, go.layout.Template(layout={"title": { "text": "Hello, world" }}), ) self.assertEqual( fig.to_dict(), { "data": [], "layout": { "template": { "layout": { "title": { "text": "Hello, world" } } } }, }, )
def test_move_unnamed_annotation_property(self): fig = go.Figure( layout={ "annotations": [ { "arrowcolor": "blue", "text": "First one", "font": { "size": 23 } }, { "arrowcolor": "green", "text": "Second one", "font": { "family": "Rockwell" }, }, ] }) templated_fig = pio.to_templated(fig) # Note that properties named 'text' are not moved to template by # default, unless they are part of a named array element expected_fig = go.Figure( layout={ "annotations": [{ "text": "First one" }, { "text": "Second one" }], "template": { "layout": { "annotationdefaults": { "arrowcolor": "green", "font": { "size": 23, "family": "Rockwell" }, } } }, }) self.assertEqual(templated_fig, expected_fig)
def test_set_default_template(self): orig_default = pio.templates.default pio.templates.default = "plotly" fig = go.Figure() self.assertEqual( fig.layout.template.to_plotly_json(), pio.templates["plotly"].to_plotly_json(), ) pio.templates.default = orig_default
def test_invalid_defaults_property_name_constructor(self): go.Figure( layout={"template": { "layout": { "imagedefaults": { "bogus": 500 } } }})
def test_invalid_defaults_property_value_constructor(self): go.Figure(layout={ "template": { "layout": { "imagedefaults": { "sizex": "str not number" } } } })
def test_invalid_defaults_property_value_constructor(self): go.Figure(layout={ "template": { "layout": { "xaxis": { "range": "str not tuple" } } } })
def fig1(request): return go.Figure( data=[ { "type": "scatter", "y": np.array([2, 1, 3, 2, 4, 2]), "marker": {"color": "green"}, } ], layout={"title": {"text": "Figure title"}}, )
def test_add_annotation_no_grid(self): # Paper annotation fig = go.Figure() fig.add_annotation(text="A", yref="paper") annot = fig.layout.annotations[-1] self.assertEqual(annot.text, "A") self.assertEqual(annot.xref, None) self.assertEqual(annot.yref, "paper") # Not valid to add annotation by row/col with self.assertRaisesRegexp(Exception, "make_subplots"): fig.add_annotation(text="B", row=1, col=1)
def test_update_subplot_overwrite(self): fig = go.Figure(layout_xaxis_title_text="Axis title") fig.update_xaxes(overwrite=True, title={"font": {"family": "Courier"}}) self.assertEqual( fig.layout.xaxis.to_plotly_json(), {"title": { "font": { "family": "Courier" } }}, )
def setUp(self): # Construct with mocked _send_restyle_msg method self.figure = go.Figure(data=[ go.Scatter(), go.Bar(), go.Parcoords(dimensions=[{}, { "label": "dim 2" }, {}]), ]) # Mock out the message method self.figure._send_restyle_msg = MagicMock()
def fig1(request): return go.Figure( data=[ {"type": "scattergl", "marker": {"color": "green"}}, { "type": "parcoords", "dimensions": [{"values": [1, 2, 3]}, {"values": [3, 2, 1]}], "line": {"color": "blue"}, }, ], layout={"title": "Figure title"}, )
def setUp(self): # Construct initial scatter object self.figure = go.Figure( data=[ go.Scatter(y=[3, 2, 1], marker={"color": "green"}), go.Bar(y=[3, 2, 1, 0, -1], marker={"opacity": 0.5}), go.Sankey(arrangement="snap"), ] ) # Mock out the message methods self.figure._send_moveTraces_msg = MagicMock() self.figure._send_deleteTraces_msg = MagicMock()
def test_update_traces_overwrite(self): fig = go.Figure( data=[go.Scatter(marker_line_color="red"), go.Bar(marker_line_color="red")] ) fig.update_traces(overwrite=True, marker={"line": {"width": 10}}) self.assertEqual( fig.to_plotly_json()["data"], [ {"type": "scatter", "marker": {"line": {"width": 10}}}, {"type": "bar", "marker": {"line": {"width": 10}}}, ], )
def setUp(self): # Disable default template pio.templates.default = None # Construct initial scatter object self.figure = go.Figure( data=[go.Scatter(y=[3, 2, 1], marker={"color": "green"})], layout={"xaxis": { "range": [-1, 4] }}, frames=[go.Frame(layout={"yaxis": { "title": "f1" }})], )
def setUp(self): # Construct initial scatter object self.figure = go.Figure( data=[ go.Scatter(y=[3, 2, 1], marker={"color": "green"}), go.Bar(y=[3, 2, 1, 0, -1], marker={"opacity": 0.5}), ], layout={"xaxis": { "range": [-1, 4] }}, frames=[go.Frame(layout={"yaxis": { "title": "f1" }})], ) # Mock out the message method self.figure._send_animate_msg = MagicMock()
def setUp(self): fig = make_subplots( rows=3, cols=3, specs=[ [{}, { "type": "scene" }, {}], [{ "secondary_y": True }, { "type": "polar" }, { "type": "polar" }], [{ "type": "xy", "colspan": 2 }, None, { "type": "ternary" }], ], ).update(layout={"height": 800}) fig.layout.xaxis.title.text = "A" fig.layout.xaxis2.title.text = "A" fig.layout.xaxis3.title.text = "B" fig.layout.xaxis4.title.text = "B" fig.layout.yaxis.title.text = "A" fig.layout.yaxis2.title.text = "B" fig.layout.yaxis3.title.text = "A" fig.layout.yaxis4.title.text = "B" fig.layout.polar.angularaxis.rotation = 45 fig.layout.polar2.angularaxis.rotation = 45 fig.layout.polar.radialaxis.title.text = "A" fig.layout.polar2.radialaxis.title.text = "B" fig.layout.scene.xaxis.title.text = "A" fig.layout.scene.yaxis.title.text = "B" fig.layout.ternary.aaxis.title.text = "A" self.fig = fig self.fig_no_grid = go.Figure(self.fig.to_dict())
def setUp(self): # Construct initial scatter object self.figure = go.Figure( data=[ go.Scatter(y=[3, 2, 1], marker={"color": "green"}), go.Bar(y=[3, 2, 1, 0, -1], marker={"opacity": 0.5}), ], layout={ "xaxis": { "range": [-1, 4] }, "width": 1000 }, frames=[go.Frame(layout={"yaxis": { "title": "f1" }})], )
def test_bug_1462(self): # https: // github.com / plotly / plotly_study.py / issues / 1462 fig = go.Figure( data=[ go.Scatter(x=[1, 2], y=[1, 2], xaxis="x"), go.Scatter(x=[2, 3], y=[2, 3], xaxis="x2"), ] ) layout_dict = { "grid": {"xaxes": ["x", "x2"], "yaxes": ["y"]}, "xaxis2": {"matches": "x", "title": {"text": "total_bill"}}, } fig.update(layout=layout_dict) updated_layout_dict = fig.layout.to_plotly_json() self.assertEqual(updated_layout_dict, layout_dict)
def latexfig(): pio.templates.default = None trace1 = go.Scatter(x=[1, 2, 3, 4], y=[1, 4, 9, 16]) trace2 = go.Scatter(x=[1, 2, 3, 4], y=[0.5, 2, 4.5, 8]) data = [trace1, trace2] layout = go.Layout( xaxis=dict(title="$\\sqrt{(n_\\text{c}(t|{T_\\text{early}}))}$", showticklabels=False), yaxis=dict(title="$d, r \\text{ (solar radius)}$", showticklabels=False), showlegend=False, font={ "family": "Arial", "size": 12 }, ) fig = go.Figure(data=data, layout=layout) yield fig pio.templates.default = "plotly"
def __init__(self): """Initialize PlotlyRenderer obj. PlotlyRenderer obj is called on by an Exporter object to draw matplotlib objects like figures, axes, text, etc. All class attributes are listed here in the __init__ method. """ self.plotly_fig = go.Figure() self.mpl_fig = None self.current_mpl_ax = None self.bar_containers = None self.current_bars = [] self.axis_ct = 0 self.x_is_mpl_date = False self.mpl_x_bounds = (0, 1) self.mpl_y_bounds = (0, 1) self.msg = "Initialized PlotlyRenderer\n"