| 20 | |
| 21 | |
| 22 | def test_custom_data_scatter(backend): |
| 23 | iris = nw.from_native(px.data.iris(return_type=backend)) |
| 24 | # No hover, no custom data |
| 25 | fig = px.scatter( |
| 26 | iris.to_native(), x="sepal_width", y="sepal_length", color="species" |
| 27 | ) |
| 28 | assert fig.data[0].customdata is None |
| 29 | # Hover, no custom data |
| 30 | fig = px.scatter( |
| 31 | iris.to_native(), |
| 32 | x="sepal_width", |
| 33 | y="sepal_length", |
| 34 | color="species", |
| 35 | hover_data=["petal_length", "petal_width"], |
| 36 | ) |
| 37 | for data in fig.data: |
| 38 | assert np.all( |
| 39 | np.isin(data.customdata[:, 1], iris.get_column("petal_width").to_numpy()) |
| 40 | ) |
| 41 | # Hover and custom data, no repeated arguments |
| 42 | fig = px.scatter( |
| 43 | iris.to_native(), |
| 44 | x="sepal_width", |
| 45 | y="sepal_length", |
| 46 | hover_data=["petal_length", "petal_width"], |
| 47 | custom_data=["species_id", "species"], |
| 48 | ) |
| 49 | assert np.all( |
| 50 | fig.data[0].customdata[:, 0] == iris.get_column("species_id").to_numpy() |
| 51 | ) |
| 52 | assert fig.data[0].customdata.shape[1] == 4 |
| 53 | # Hover and custom data, with repeated arguments |
| 54 | fig = px.scatter( |
| 55 | iris.to_native(), |
| 56 | x="sepal_width", |
| 57 | y="sepal_length", |
| 58 | hover_data=["petal_length", "petal_width", "species_id"], |
| 59 | custom_data=["species_id", "species"], |
| 60 | ) |
| 61 | assert np.all( |
| 62 | fig.data[0].customdata[:, 0] == iris.get_column("species_id").to_numpy() |
| 63 | ) |
| 64 | assert fig.data[0].customdata.shape[1] == 4 |
| 65 | assert ( |
| 66 | fig.data[0].hovertemplate |
| 67 | == "sepal_width=%{x}<br>sepal_length=%{y}<br>petal_length=%{customdata[2]}<br>petal_width=%{customdata[3]}<br>species_id=%{customdata[0]}<extra></extra>" |
| 68 | ) |
| 69 | |
| 70 | |
| 71 | def test_labels(backend): |