def test_update_list_make_copies_false(self): trace1 = Scatter(x=[1, 2, 3], y=[2, 1, 2]) trace2 = Scatter(x=[1, 2, 3], y=[3, 2, 1]) data = Data([trace1, trace2]) update = dict(x=[2, 3, 4], y=[1, 2, 3], line=Line()) data.update(update, make_copies=False) assert data[0]["line"] is data[1]["line"]
def test_append_scatter(): expected = Figure( data=Data([Scatter(x=[1, 2, 3], y=[2, 3, 4], xaxis="x5", yaxis="y5")]), layout=Layout( xaxis1=XAxis(domain=[0.0, 0.2888888888888889], anchor="y1"), xaxis2=XAxis(domain=[0.35555555555555557, 0.6444444444444445], anchor="y2"), xaxis3=XAxis(domain=[0.7111111111111111, 1.0], anchor="y3"), xaxis4=XAxis(domain=[0.0, 0.2888888888888889], anchor="y4"), xaxis5=XAxis(domain=[0.35555555555555557, 0.6444444444444445], anchor="y5"), xaxis6=XAxis(domain=[0.7111111111111111, 1.0], anchor="y6"), yaxis1=YAxis(domain=[0.575, 1.0], anchor="x1"), yaxis2=YAxis(domain=[0.575, 1.0], anchor="x2"), yaxis3=YAxis(domain=[0.575, 1.0], anchor="x3"), yaxis4=YAxis(domain=[0.0, 0.425], anchor="x4"), yaxis5=YAxis(domain=[0.0, 0.425], anchor="x5"), yaxis6=YAxis(domain=[0.0, 0.425], anchor="x6"), ), ) trace = Scatter(x=[1, 2, 3], y=[2, 3, 4]) fig = tls.make_subplots(rows=2, cols=3) fig.append_trace(trace, 2, 2) d1, d2 = strip_dict_params(fig["data"][0], expected["data"][0]) assert d1 == d2 d1, d2 = strip_dict_params(fig["layout"], expected["layout"]) assert d1 == d2
def test_update_list_empty(self): trace1 = Scatter(x=[1, 2, 3], y=[2, 1, 2]) trace2 = Scatter(x=[1, 2, 3], y=[3, 2, 1]) fig = Figure([trace1, trace2]) fig.update([]) d1, d2 = strip_dict_params(fig.data[0], Scatter(x=[1, 2, 3], y=[2, 1, 2])) assert d1 == d2 d1, d2 = strip_dict_params(fig.data[1], Scatter(x=[1, 2, 3], y=[3, 2, 1])) assert d1 == d2
def test_update_list(self): trace1 = Scatter(x=[1, 2, 3], y=[2, 1, 2]) trace2 = Scatter(x=[1, 2, 3], y=[3, 2, 1]) fig = Figure([trace1, trace2]) update = dict(x=[2, 3, 4], y=[1, 2, 3]) update_res1 = fig.data[0].update(update) update_res2 = fig.data[1].update(update) d1, d2 = strip_dict_params(fig.data[0], Scatter(x=[2, 3, 4], y=[1, 2, 3])) assert d1 == d2 d1, d2 = strip_dict_params(fig.data[1], Scatter(x=[2, 3, 4], y=[1, 2, 3])) assert d1 == d2 assert update_res1 is fig.data[0] assert update_res2 is fig.data[1]
def get_traces(trace,value,type,color=None,width=.2,opacity=.3): if not is_list(value): value=[value]*len(trace['y']) if values_minus: if is_list(values_minus): min_value=values_minus else: min_value=[values_minus]*len(trace['y']) else: min_value=value if 'percent' in type: y_up=[trace['y'][_]*(1+value[_]/100.00) for _ in range(len(value))] y_down=[trace['y'][_]*(1-min_value[_]/100.00) for _ in range(len(min_value))] else: y_up=[trace['y'][_]+value[_] for _ in range(len(value))] y_down=[trace['y'][_]-min_value[_] for _ in range(len(min_value))] upper=Scatter(y=y_up,mode='lines',showlegend=False, line=Line(width=width),x=trace['x']) if 'yaxis' in trace: upper['yaxis']=trace['yaxis'] if color: color=normalize(color) else: if 'color' in trace['line']: color=trace['line']['color'] else: color='charcoal' color=to_rgba(color,opacity) if color else None upper['line']['color']=color lower=upper.copy() name=trace['name']+'_' if 'name' in trace else '' upper.update(name=name+'upper') color=to_rgba(normalize(color),opacity) lower.update(fill='tonexty',fillcolor=color,name=name+'lower',y=y_down) return upper,lower
def test_figure_json_encoding(self): df = pd.DataFrame(columns=["col 1"], data=[1, 2, 3]) s1 = Scatter3d(x=numeric_list, y=np_list, z=mixed_list) s2 = Scatter(x=df["col 1"]) data = Data([s1, s2]) figure = Figure(data=data) js1 = _json.dumps(s1, cls=utils.PlotlyJSONEncoder, sort_keys=True) js2 = _json.dumps(s2, cls=utils.PlotlyJSONEncoder, sort_keys=True) assert ( js1 == '{"type": "scatter3d", "x": [1, 2, 3], ' '"y": [1, 2, 3, null, null, null, "2014-01-05T00:00:00"], ' '"z": [1, "A", "2014-01-05T00:00:00", ' '"2014-01-05T01:01:01", "2014-01-05T01:01:01.000001"]}' ) assert js2 == '{"type": "scatter", "x": [1, 2, 3]}' # Test JSON encoding works _json.dumps(data, cls=utils.PlotlyJSONEncoder, sort_keys=True) _json.dumps(figure, cls=utils.PlotlyJSONEncoder, sort_keys=True) # Test data wasn't mutated np_array = np.array([1, 2, 3, np.NaN, np.NAN, np.Inf, dt(2014, 1, 5)]) for k in range(len(np_array)): if k in [3, 4]: # check NaN assert np.isnan(np_list[k]) and np.isnan(np_array[k]) else: # non-NaN assert np_list[k] == np_array[k] assert set(data[0]["z"]) == set( [ 1, "A", dt(2014, 1, 5), dt(2014, 1, 5, 1, 1, 1), dt(2014, 1, 5, 1, 1, 1, 1), ] )
def test_append_trace_col_out_of_range(): trace = Scatter(x=[1, 2, 3], y=[2, 3, 4]) fig = tls.make_subplots(rows=2, cols=3) fig.append_trace(trace, 2, 0)
def test_append_trace_before_make_subplots(): trace = Scatter(x=[1, 2, 3], y=[2, 3, 4]) fig = Figure() fig.append_trace(trace, 2, 2)
def test_trivial(): print(Scatter()) assert Scatter().to_plotly_json() == dict(type="scatter")