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_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(templated_fig.to_dict(), expected_fig.to_dict())
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_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_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_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_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_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_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_nested_trace_properties_existing_traces(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', 'template': { 'data': { 'bar': [go.Bar(marker={'line': { 'color': 'purple' }})] } } }) 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', 'line': { 'color': 'purple' } }), go.Bar(marker={'opacity': 0.4}) ] }, 'layout': { 'barmode': 'group' } } }) self.assertEqual(pio.to_json(templated_fig), pio.to_json(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_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_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_existing_traces(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', 'template': { 'data': { 'bar': [go.Bar(marker={ 'line': {'color': 'purple'}})] } }} ) 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', 'line': { 'color': 'purple' }}), go.Bar(marker={'opacity': 0.4}) ]}, 'layout': { 'barmode': 'group' } } } ) self.assertEqual(pio.to_json(templated_fig), pio.to_json(expected_fig))
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 set_plotly_template(): layout = go.Layout(title='Figure Title', font=dict(size=14, family='Roboto', color='white'), paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', showlegend=False, xaxis=dict( autorange=True, showgrid=False, ), yaxis=dict( autorange=True, showgrid=False, )) fig = go.Figure(layout=layout) templated_fig = pio.to_templated(fig) pio.templates['no_background'] = templated_fig.layout.template return 'no_background'
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_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)
from statsmodels.tsa.stattools import adfuller from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot import plotly as py import plotly.io as pio import plotly.graph_objs as go from plotly.subplots import make_subplots # Show charts when running kernel init_notebook_mode(connected=True) # Change default background color for all visualizations layout = go.Layout(paper_bgcolor="rgba(0,0,0,0)", plot_bgcolor="rgba(250,250,250,0.8)") fig = go.Figure(layout=layout) templated_fig = pio.to_templated(fig) pio.templates["my_template"] = templated_fig.layout.template pio.templates.default = "my_template" # Test for staionarity def test_stationarity(timeseries): # Determing rolling statistics rolmean = timeseries.rolling(24).mean() # 4 days rolstd = timeseries.rolling(24).std() # 4 days # Plot rolling statistics: plt.plot(timeseries, color="blue", label="Original") plt.plot(rolmean, color="red", label="Rolling Mean") plt.plot(rolstd, color="black", label="Rolling Std") plt.legend(loc="best") plt.title("Rolling Mean and Standard Deviation")