From f8c1465d352d380e1501bbe3be0ead20dfbd21d4 Mon Sep 17 00:00:00 2001 From: Nicolas Kruchten Date: Sat, 21 Mar 2020 21:14:58 -0400 Subject: [PATCH] hide name from within hovertemplate --- .../python/plotly/plotly/express/_core.py | 3 +- .../plotly/tests/test_core/test_px/test_px.py | 2 +- .../tests/test_core/test_px/test_px_input.py | 52 ++++++++++++------- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index de2af43131d..ff247942a3d 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -389,6 +389,7 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref): ]: hover_lines = [k + "=" + v for k, v in mapping_labels.items()] trace_patch["hovertemplate"] = hover_header + "
".join(hover_lines) + trace_patch["hovertemplate"] += "" return trace_patch, fit_results @@ -1445,8 +1446,6 @@ def make_figure(args, constructor, trace_patch={}, layout_patch={}): ) if trace_spec.constructor in [go.Bar, go.Violin, go.Box, go.Histogram]: trace.update(alignmentgroup=True, offsetgroup=trace_name) - if trace_spec.constructor not in [go.Parcats, go.Parcoords]: - trace.update(hoverlabel=dict(namelength=0)) trace_names.add(trace_name) # Init subplot row/col diff --git a/packages/python/plotly/plotly/tests/test_core/test_px/test_px.py b/packages/python/plotly/plotly/tests/test_core/test_px/test_px.py index b4880899e95..60699e6e21d 100644 --- a/packages/python/plotly/plotly/tests/test_core/test_px/test_px.py +++ b/packages/python/plotly/plotly/tests/test_core/test_px/test_px.py @@ -50,7 +50,7 @@ def test_custom_data_scatter(): assert fig.data[0].customdata.shape[1] == 4 assert ( fig.data[0].hovertemplate - == "sepal_width=%{x}
sepal_length=%{y}
petal_length=%{customdata[2]}
petal_width=%{customdata[3]}
species_id=%{customdata[0]}" + == "sepal_width=%{x}
sepal_length=%{y}
petal_length=%{customdata[2]}
petal_width=%{customdata[3]}
species_id=%{customdata[0]}" ) diff --git a/packages/python/plotly/plotly/tests/test_core/test_px/test_px_input.py b/packages/python/plotly/plotly/tests/test_core/test_px/test_px_input.py index 8062dd78fc3..e3786f6af90 100644 --- a/packages/python/plotly/plotly/tests/test_core/test_px/test_px_input.py +++ b/packages/python/plotly/plotly/tests/test_core/test_px/test_px_input.py @@ -29,17 +29,23 @@ def test_numpy_labels(): fig = px.scatter( x=[1, 2, 3], y=[2, 3, 4], labels={"x": "time"} ) # other labels will be kw arguments - assert fig.data[0]["hovertemplate"] == "time=%{x}
y=%{y}" + assert fig.data[0]["hovertemplate"] == "time=%{x}
y=%{y}" def test_with_index(): tips = px.data.tips() fig = px.scatter(tips, x=tips.index, y="total_bill") - assert fig.data[0]["hovertemplate"] == "index=%{x}
total_bill=%{y}" + assert ( + fig.data[0]["hovertemplate"] == "index=%{x}
total_bill=%{y}" + ) fig = px.scatter(tips, x=tips.index, y=tips.total_bill) - assert fig.data[0]["hovertemplate"] == "index=%{x}
total_bill=%{y}" + assert ( + fig.data[0]["hovertemplate"] == "index=%{x}
total_bill=%{y}" + ) fig = px.scatter(tips, x=tips.index, y=tips.total_bill, labels={"index": "number"}) - assert fig.data[0]["hovertemplate"] == "number=%{x}
total_bill=%{y}" + assert ( + fig.data[0]["hovertemplate"] == "number=%{x}
total_bill=%{y}" + ) # We do not allow "x=index" with pytest.raises(ValueError) as err_msg: fig = px.scatter(tips, x="index", y="total_bill") @@ -49,28 +55,34 @@ def test_with_index(): tips = px.data.tips() tips.index.name = "item" fig = px.scatter(tips, x=tips.index, y="total_bill") - assert fig.data[0]["hovertemplate"] == "item=%{x}
total_bill=%{y}" + assert fig.data[0]["hovertemplate"] == "item=%{x}
total_bill=%{y}" def test_pandas_series(): tips = px.data.tips() before_tip = tips.total_bill - tips.tip fig = px.bar(tips, x="day", y=before_tip) - assert fig.data[0].hovertemplate == "day=%{x}
y=%{y}" + assert fig.data[0].hovertemplate == "day=%{x}
y=%{y}" fig = px.bar(tips, x="day", y=before_tip, labels={"y": "bill"}) - assert fig.data[0].hovertemplate == "day=%{x}
bill=%{y}" + assert fig.data[0].hovertemplate == "day=%{x}
bill=%{y}" # lock down that we can pass df.col to facet_* fig = px.bar(tips, x="day", y="tip", facet_row=tips.day, facet_col=tips.day) - assert fig.data[0].hovertemplate == "day=%{x}
tip=%{y}" + assert fig.data[0].hovertemplate == "day=%{x}
tip=%{y}" def test_several_dataframes(): df = pd.DataFrame(dict(x=[0, 1], y=[1, 10], z=[0.1, 0.8])) df2 = pd.DataFrame(dict(time=[23, 26], money=[100, 200])) fig = px.scatter(df, x="z", y=df2.money, size="x") - assert fig.data[0].hovertemplate == "z=%{x}
y=%{y}
x=%{marker.size}" + assert ( + fig.data[0].hovertemplate + == "z=%{x}
y=%{y}
x=%{marker.size}" + ) fig = px.scatter(df2, x=df.z, y=df2.money, size=df.z) - assert fig.data[0].hovertemplate == "x=%{x}
money=%{y}
size=%{marker.size}" + assert ( + fig.data[0].hovertemplate + == "x=%{x}
money=%{y}
size=%{marker.size}" + ) # Name conflict with pytest.raises(NameError) as err_msg: fig = px.scatter(df, x="z", y=df2.money, size="y") @@ -85,7 +97,7 @@ def test_several_dataframes(): fig = px.scatter(x=df.y, y=df2.y) assert np.all(fig.data[0].x == np.array([3, 4])) assert np.all(fig.data[0].y == np.array([23, 24])) - assert fig.data[0].hovertemplate == "x=%{x}
y=%{y}" + assert fig.data[0].hovertemplate == "x=%{x}
y=%{y}" df = pd.DataFrame(dict(x=[0, 1], y=[3, 4])) df2 = pd.DataFrame(dict(x=[3, 5], y=[23, 24])) @@ -93,7 +105,10 @@ def test_several_dataframes(): fig = px.scatter(x=df.y, y=df2.y, size=df3.y) assert np.all(fig.data[0].x == np.array([3, 4])) assert np.all(fig.data[0].y == np.array([23, 24])) - assert fig.data[0].hovertemplate == "x=%{x}
y=%{y}
size=%{marker.size}" + assert ( + fig.data[0].hovertemplate + == "x=%{x}
y=%{y}
size=%{marker.size}" + ) df = pd.DataFrame(dict(x=[0, 1], y=[3, 4])) df2 = pd.DataFrame(dict(x=[3, 5], y=[23, 24])) @@ -102,7 +117,8 @@ def test_several_dataframes(): assert np.all(fig.data[0].x == np.array([3, 4])) assert np.all(fig.data[0].y == np.array([23, 24])) assert ( - fig.data[0].hovertemplate == "x=%{x}
y=%{y}
hover_data_0=%{customdata[0]}" + fig.data[0].hovertemplate + == "x=%{x}
y=%{y}
hover_data_0=%{customdata[0]}" ) @@ -111,7 +127,7 @@ def test_name_heuristics(): fig = px.scatter(df, x=df.y, y=df.x, size=df.y) assert np.all(fig.data[0].x == np.array([3, 4])) assert np.all(fig.data[0].y == np.array([0, 1])) - assert fig.data[0].hovertemplate == "y=%{marker.size}
x=%{y}" + assert fig.data[0].hovertemplate == "y=%{marker.size}
x=%{y}" def test_repeated_name(): @@ -133,7 +149,7 @@ def test_arrayattrable_numpy(): ) assert ( fig.data[0]["hovertemplate"] - == "total_bill=%{x}
tip=%{y}
hover_data_0=%{customdata[0]}" + == "total_bill=%{x}
tip=%{y}
hover_data_0=%{customdata[0]}" ) tips = px.data.tips() fig = px.scatter( @@ -145,7 +161,7 @@ def test_arrayattrable_numpy(): ) assert ( fig.data[0]["hovertemplate"] - == "total_bill=%{x}
tip=%{y}
suppl=%{customdata[0]}" + == "total_bill=%{x}
tip=%{y}
suppl=%{customdata[0]}" ) @@ -275,7 +291,7 @@ def test_int_col_names(): def test_data_frame_from_dict(): fig = px.scatter({"time": [0, 1], "money": [1, 2]}, x="time", y="money") - assert fig.data[0].hovertemplate == "time=%{x}
money=%{y}" + assert fig.data[0].hovertemplate == "time=%{x}
money=%{y}" assert np.all(fig.data[0].x == [0, 1]) @@ -306,4 +322,4 @@ def test_pass_df_columns(): def test_size_column(): df = px.data.tips() fig = px.scatter(df, x=df["size"], y=df.tip) - assert fig.data[0].hovertemplate == "size=%{x}
tip=%{y}" + assert fig.data[0].hovertemplate == "size=%{x}
tip=%{y}"