def open_figure(self, fig, props): """Creates a new figure by beginning to fill out layout dict. The 'autosize' key is set to false so that the figure will mirror sizes set by mpl. The 'hovermode' key controls what shows up when you mouse around a figure in plotly, it's set to show the 'closest' point. Positional agurments: fig -- a matplotlib.figure.Figure object. props.keys(): [ 'figwidth', 'figheight', 'dpi' ] """ self.msg += "Opening figure\n" self.mpl_fig = fig self.plotly_fig["layout"] = go.Layout( width=int(props["figwidth"] * props["dpi"]), height=int(props["figheight"] * props["dpi"]), autosize=False, hovermode="closest", ) self.mpl_x_bounds, self.mpl_y_bounds = mpltools.get_axes_bounds(fig) margin = go.layout.Margin( l=int(self.mpl_x_bounds[0] * self.plotly_fig["layout"]["width"]), r=int((1 - self.mpl_x_bounds[1]) * self.plotly_fig["layout"]["width"]), t=int((1 - self.mpl_y_bounds[1]) * self.plotly_fig["layout"]["height"]), b=int(self.mpl_y_bounds[0] * self.plotly_fig["layout"]["height"]), pad=0, ) self.plotly_fig["layout"]["margin"] = margin
def setUp(self): # Construct initial scatter object self.parcoords = go.Parcoords(name="parcoords A") # Assert initial state d1, d2 = strip_dict_params( self.parcoords, {"type": "parcoords", "name": "parcoords A"} ) assert d1 == d2 # Construct expected results self.expected_toplevel = { "type": "parcoords", "name": "parcoords A", "dimensions": [ {"values": [2, 3, 1], "visible": True}, {"values": [1, 2, 3], "label": "dim1"}, ], } self.layout = go.Layout() self.expected_layout1 = {"updatemenus": [{}, {"font": {"family": "courier"}}]} self.expected_layout2 = { "updatemenus": [{}, {"buttons": [{}, {}, {"method": "restyle"}]}] }
def test_merge_by_flaglist_string(self): layout = go.Layout() layout.template = "template1+template2" result = layout.template expected = self.expected1_2 self.assertEqual(result, expected) # Make sure input templates weren't modified self.assertEqual(self.template1, self.template1_orig) self.assertEqual(self.template2, self.template2_orig)
def setUp(self): self.layout = go.Layout( width=1000, title={ "text": "the title", "font": { "size": 20 } }, annotations=[{}, {}], xaxis2={"range": [1, 2]}, )
def test_legacy_title_props_remapped(self): # plain Layout obj = go.Layout() self.assertIs(obj.titlefont, obj.title.font) self.assertIsNone(obj.title.font.family) # Set titlefont in constructor obj = go.Layout(titlefont={"family": "Courier"}) self.assertIs(obj.titlefont, obj.title.font) self.assertEqual(obj.titlefont.family, "Courier") self.assertEqual(obj.title.font.family, "Courier") # Property assignment obj = go.Layout() obj.titlefont.family = "Courier" self.assertIs(obj.titlefont, obj.title.font) self.assertEqual(obj["titlefont.family"], "Courier") self.assertEqual(obj.title.font.family, "Courier") # In/Iter self.assertIn("titlefont", obj) self.assertIn("titlefont.family", obj) self.assertIn("titlefont", iter(obj))
def test_attr_access(self): scatt_uid = self.figure.data[0].uid self.assertEqual( self.figure.data, (go.Scatter(y=[3, 2, 1], marker={"color": "green"}, uid=scatt_uid), ), ) self.assertEqual(self.figure.layout, go.Layout(xaxis={"range": [-1, 4]})) self.assertEqual(self.figure.frames, (go.Frame(layout={"yaxis": { "title": "f1" }}), ))
def test_update_uninitialized_list_with_list(self): """ If the original list is undefined, the updated list should be accepted in full. See GH1072 """ layout = go.Layout() layout.update(annotations=[ go.layout.Annotation(text="one"), go.layout.Annotation(text="two"), ]) expected = {"annotations": [{"text": "one"}, {"text": "two"}]} self.assertEqual(len(layout.annotations), 2) self.assertEqual(layout.to_plotly_json(), expected)
def test_subplot_props_in_constructor(self): layout = go.Layout( xaxis2=go.layout.XAxis(title={"text": "xaxis 2"}), yaxis3=go.layout.YAxis(title={"text": "yaxis 3"}), geo4=go.layout.Geo(bgcolor="blue"), ternary5=go.layout.Ternary(sum=120), scene6=go.layout.Scene(dragmode="zoom"), mapbox7=go.layout.Mapbox(zoom=2), polar8=go.layout.Polar(sector=[0, 90]), ) self.assertEqual(layout.xaxis2.title.text, "xaxis 2") self.assertEqual(layout.yaxis3.title.text, "yaxis 3") self.assertEqual(layout.geo4.bgcolor, "blue") self.assertEqual(layout.ternary5.sum, 120) self.assertEqual(layout.scene6.dragmode, "zoom") self.assertEqual(layout.mapbox7.zoom, 2) self.assertEqual(layout.polar8.sector, (0, 90))
def test_update_initialized_empty_list_with_list(self): """ If the original list is empty, treat is just as if it's undefined. This is a change in behavior from version 2 (where the input list would just be completly ignored), because in version 3 the difference between an uninitialized and empty list is not obvious to the user. """ layout = go.Layout(annotations=[]) layout.update(annotations=[ go.layout.Annotation(text="one"), go.layout.Annotation(text="two"), ]) expected = {"annotations": [{"text": "one"}, {"text": "two"}]} self.assertEqual(len(layout.annotations), 2) self.assertEqual(layout.to_plotly_json(), expected)
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 test_overwrite_compound_prop(self): layout = go.Layout(title_font_family="Courier") # First update with default (recursive) behavior layout.update(title={"text": "Fig Title"}) expected = { "title": { "text": "Fig Title", "font": { "family": "Courier" } } } self.assertEqual(layout.to_plotly_json(), expected) # Update with overwrite behavior layout.update(title={"text": "Fig Title2"}, overwrite=True) expected = {"title": {"text": "Fig Title2"}} self.assertEqual(layout.to_plotly_json(), expected)
def test_overwrite_tuple_prop(self): layout = go.Layout(annotations=[ go.layout.Annotation(text="one"), go.layout.Annotation(text="two"), ]) layout.update( overwrite=True, annotations=[ go.layout.Annotation(width=10), go.layout.Annotation(width=20), go.layout.Annotation(width=30), go.layout.Annotation(width=40), go.layout.Annotation(width=50), ], ) expected = { "annotations": [ { "width": 10 }, { "width": 20 }, { "width": 30 }, { "width": 40 }, { "width": 50 }, ] } self.assertEqual(layout.to_plotly_json(), expected) # Remove all annotations layout.update(overwrite=True, annotations=None) self.assertEqual(layout.to_plotly_json(), {})
def test_update_initialize_nonempty_list_with_list_extends(self): layout = go.Layout(annotations=[ go.layout.Annotation(text="one"), go.layout.Annotation(text="two"), ]) layout.update(annotations=[ go.layout.Annotation(width=10), go.layout.Annotation(width=20), go.layout.Annotation(width=30), go.layout.Annotation(width=40), go.layout.Annotation(width=50), ]) expected = { "annotations": [ { "text": "one", "width": 10 }, { "text": "two", "width": 20 }, { "width": 30 }, { "width": 40 }, { "width": 50 }, ] } self.assertEqual(layout.to_plotly_json(), expected)
def test_update_initialized_nonempty_list_with_dict(self): """ If the original list is defined, a dict from index numbers to property dicts may be used to update select elements of the existing list """ layout = go.Layout(annotations=[ go.layout.Annotation(text="one"), go.layout.Annotation(text="two"), ]) layout.update(annotations={1: go.layout.Annotation(width=30)}) expected = { "annotations": [{ "text": "one" }, { "text": "two", "width": 30 }] } self.assertEqual(len(layout.annotations), 2) self.assertEqual(layout.to_plotly_json(), expected)
def test_subplot_1_in_constructor(self): layout = go.Layout(xaxis1=go.layout.XAxis(title={"text": "xaxis 1"})) self.assertEqual(layout.xaxis1.title.text, "xaxis 1")
layout=go.Layout( width=640, height=480, autosize=False, margin=go.layout.Margin(l=80, r=63, b=52, t=57, pad=0), hovermode="closest", showlegend=False, xaxis1=go.layout.XAxis( domain=[0.0, 1.0], range=[0.64334677419354847, 8.3566532258064505], type="linear", showline=True, ticks="inside", nticks=10, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="y1", side="bottom", mirror="ticks", ), yaxis1=go.layout.YAxis( domain=[0.0, 1.0], range=[1.6410714285714287, 9.3589285714285726], type="linear", showline=True, ticks="inside", nticks=10, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="x1", side="left", mirror="ticks", ), ),
def setUp(self): # Construct initial scatter object self.layout = go.Layout() pio.templates.default = None
layout=go.Layout( width=640, height=480, autosize=False, margin=go.layout.Margin(l=80, r=63, b=52, t=57, pad=0), hovermode="closest", showlegend=False, bargap=0.2, xaxis1=go.layout.XAxis( domain=[0.0, 1.0], range=[-0.68999999999999995, 5.6899999999999995], type="linear", showline=True, ticks="inside", nticks=8, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="y1", side="bottom", mirror="ticks", ), yaxis1=go.layout.YAxis( domain=[0.0, 1.0], range=[0.0, 210.0], type="linear", showline=True, ticks="inside", nticks=10, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="x1", side="left", mirror="ticks", ), ),
layout=go.Layout( width=640, height=480, autosize=False, margin=go.layout.Margin(l=80, r=63, b=52, t=57, pad=0), hovermode="closest", showlegend=False, xaxis1=go.layout.XAxis( domain=[0.0, 1.0], range=[-0.25, 5.25], type="linear", showline=True, ticks="inside", nticks=8, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="y1", side="bottom", mirror="ticks", ), yaxis1=go.layout.YAxis( domain=[0.0, 1.0], range=[0.5, 209.5], type="linear", showline=True, ticks="inside", nticks=10, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="x1", side="left", mirror="ticks", ), ),
layout=go.Layout( width=640, height=480, autosize=False, margin=go.layout.Margin(l=80, r=63, b=47, t=47, pad=0), hovermode="closest", showlegend=False, annotations=[ go.layout.Annotation( x=0.000997987927565, y=0.9973865229110511, text="top-left", xref="paper", yref="paper", showarrow=False, align="left", font=dict(size=10.0, color="#000000"), opacity=1, xanchor="left", yanchor="top", ), go.layout.Annotation( x=0.000997987927565, y=0.0031525606469002573, text="bottom-left", xref="paper", yref="paper", align="left", showarrow=False, font=dict(size=10.0, color="#000000"), opacity=1, xanchor="left", yanchor="bottom", ), go.layout.Annotation( x=0.996989939638, y=0.9973865229110511, text="top-right", xref="paper", yref="paper", align="right", showarrow=False, font=dict(size=10.0, color="#000000"), opacity=1, xanchor="right", yanchor="top", ), go.layout.Annotation( x=0.996989939638, y=0.0031525606469002573, text="bottom-right", xref="paper", yref="paper", align="right", showarrow=False, font=dict(size=10.0, color="#000000"), opacity=1, xanchor="right", yanchor="bottom", ), ], xaxis1=go.layout.XAxis( domain=[0.0, 1.0], range=(0.0, 2.0), showline=True, ticks="inside", showgrid=False, zeroline=False, anchor="y1", mirror=True, ), yaxis1=go.layout.YAxis( domain=[0.0, 1.0], range=(1.0, 3.0), showline=True, ticks="inside", showgrid=False, zeroline=False, anchor="x1", mirror=True, ), ),
def test_title_as_string_layout(self): """ Prior to plotly_study.js 1.43.0 title properties were strings, in 1.43.0 these title properties became compound objects with a text property. For backwards compatibility, we still need to support setting this title object as a string or number """ layout_title_parents = [ go.Layout(), go.layout.XAxis(), go.layout.YAxis(), go.layout.ternary.Aaxis(), go.layout.ternary.Baxis(), go.layout.ternary.Caxis(), go.layout.scene.XAxis(), go.layout.scene.YAxis(), go.layout.scene.ZAxis(), go.layout.polar.RadialAxis(), go.scatter.marker.ColorBar(), go.cone.ColorBar(), ] for obj in layout_title_parents: obj.title = "A title" self.assertEqual(obj.title.text, "A title") self.assertEqual(obj.to_plotly_json(), {"title": { "text": "A title" }}) # And update obj.update(title="A title 2") self.assertEqual(obj.title.text, "A title 2") self.assertEqual(obj.to_plotly_json(), {"title": { "text": "A title 2" }}) # Update titlefont obj.update(titlefont={"size": 23}) self.assertEqual(obj.title.font.size, 23) self.assertEqual( obj.to_plotly_json(), {"title": { "text": "A title 2", "font": { "size": 23 } }}, ) # Pie obj = go.Pie() obj.title = "A title" self.assertEqual(obj.title.text, "A title") self.assertEqual(obj.to_plotly_json(), { "title": { "text": "A title" }, "type": "pie" }) # And update obj.update(title="A title 2") self.assertEqual(obj.title.text, "A title 2") self.assertEqual(obj.to_plotly_json(), { "type": "pie", "title": { "text": "A title 2" } }) # Update titlefont obj.update(titlefont={"size": 23}) self.assertEqual(obj.title.font.size, 23) self.assertEqual( obj.to_plotly_json(), { "type": "pie", "title": { "text": "A title 2", "font": { "size": 23 } } }, )
layout=go.Layout( width=640, height=480, autosize=False, margin=go.layout.Margin(l=168, r=63, b=52, t=57, pad=0), hovermode="closest", showlegend=False, xaxis1=go.layout.XAxis( domain=[0.0, 0.13513513513513517], range=[-0.050000000000000003, 1.05], type="linear", showline=True, ticks="inside", nticks=4, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="y1", side="bottom", mirror="ticks", ), xaxis2=go.layout.XAxis( domain=[0.0, 0.13513513513513517], range=[0.0, 1.0], type="linear", showline=True, ticks="inside", nticks=2, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="y2", side="bottom", mirror="ticks", ), xaxis3=go.layout.XAxis( domain=[0.0, 0.13513513513513517], range=[0.0, 1.0], type="linear", showline=True, ticks="inside", nticks=2, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="y3", side="bottom", mirror="ticks", ), xaxis4=go.layout.XAxis( domain=[0.2162162162162162, 1.0], range=[0.0, 1.0], type="linear", showline=True, ticks="inside", nticks=6, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="y4", side="bottom", mirror="ticks", ), xaxis5=go.layout.XAxis( domain=[0.2162162162162162, 0.56756756756756754], range=[0.0, 1.0], type="linear", showline=True, ticks="inside", nticks=3, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="y5", side="bottom", mirror="ticks", ), xaxis6=go.layout.XAxis( domain=[0.2162162162162162, 0.78378378378378377], range=[0.0, 1.0], type="linear", showline=True, ticks="inside", nticks=6, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="y6", side="bottom", mirror="ticks", ), xaxis7=go.layout.XAxis( domain=[0.64864864864864857, 1.0], range=[0.0, 1.0], type="linear", showline=True, ticks="inside", nticks=3, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="y7", side="bottom", mirror="ticks", ), xaxis8=go.layout.XAxis( domain=[0.8648648648648648, 1.0], range=[0.0, 1.0], type="linear", showline=True, ticks="inside", nticks=2, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="y8", side="bottom", mirror="ticks", ), yaxis1=go.layout.YAxis( domain=[0.82758620689655171, 1.0], range=[0.94999999999999996, 2.0499999999999998], type="linear", showline=True, ticks="inside", nticks=4, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="x1", side="left", mirror="ticks", ), yaxis2=go.layout.YAxis( domain=[0.55172413793103448, 0.72413793103448265], range=[0.0, 1.0], type="linear", showline=True, ticks="inside", nticks=3, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="x2", side="left", mirror="ticks", ), yaxis3=go.layout.YAxis( domain=[0.0, 0.44827586206896547], range=[0.0, 1.0], type="linear", showline=True, ticks="inside", nticks=6, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="x3", side="left", mirror="ticks", ), yaxis4=go.layout.YAxis( domain=[0.82758620689655171, 1.0], range=[0.0, 1.0], type="linear", showline=True, ticks="inside", nticks=3, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="x4", side="left", mirror="ticks", ), yaxis5=go.layout.YAxis( domain=[0.27586206896551713, 0.72413793103448265], range=[0.0, 1.0], type="linear", showline=True, ticks="inside", nticks=6, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="x5", side="left", mirror="ticks", ), yaxis6=go.layout.YAxis( domain=[0.0, 0.17241379310344826], range=[0.0, 1.0], type="linear", showline=True, ticks="inside", nticks=3, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="x6", side="left", mirror="ticks", ), yaxis7=go.layout.YAxis( domain=[0.27586206896551713, 0.72413793103448265], range=[0.0, 1.0], type="linear", showline=True, ticks="inside", nticks=6, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="x7", side="left", mirror="ticks", ), yaxis8=go.layout.YAxis( domain=[0.0, 0.17241379310344826], range=[0.0, 1.0], type="linear", showline=True, ticks="inside", nticks=3, showgrid=False, zeroline=False, tickfont=dict(size=10.0), anchor="x8", side="left", mirror="ticks", ), ),
def setUp(self): # Construct initial scatter object self.scatter = go.Scatter() self.scatter.name = "Scatter 1" self.layout = go.Layout()