|
1 | 1 | import plotly.express as px
|
2 | 2 | import plotly.graph_objects as go
|
3 | 3 | from numpy.testing import assert_array_equal
|
| 4 | +import numpy as np |
4 | 5 |
|
5 | 6 |
|
6 | 7 | def _compare_figures(go_trace, px_fig):
|
@@ -52,29 +53,45 @@ def test_pie_like_px():
|
52 | 53 | _compare_figures(trace, fig)
|
53 | 54 |
|
54 | 55 |
|
55 |
| -def test_pie_like_colors(): |
| 56 | +def test_sunburst_treemap_colorscales(): |
56 | 57 | labels = ["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"]
|
57 | 58 | parents = ["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve"]
|
58 | 59 | values = [10, 14, 12, 10, 2, 6, 6, 4, 4]
|
59 |
| - # Sunburst |
60 |
| - fig = px.sunburst( |
61 |
| - names=labels, |
62 |
| - parents=parents, |
63 |
| - values=values, |
64 |
| - color=values, |
65 |
| - color_continuous_scale="Viridis", |
66 |
| - range_color=(5, 15), |
67 |
| - ) |
68 |
| - assert fig.layout.coloraxis.cmin, fig.layout.coloraxis.cmax == (5, 15) |
69 |
| - assert fig.layout.coloraxis.colorscale[0] == (0.0, "#440154") |
70 |
| - # Treemap |
71 |
| - fig = px.treemap( |
72 |
| - names=labels, |
73 |
| - parents=parents, |
74 |
| - values=values, |
75 |
| - color=values, |
76 |
| - color_continuous_scale="Viridis", |
77 |
| - range_color=(5, 15), |
78 |
| - ) |
79 |
| - assert fig.layout.coloraxis.cmin, fig.layout.coloraxis.cmax == (5, 15) |
80 |
| - assert fig.layout.coloraxis.colorscale[0] == (0.0, "#440154") |
| 60 | + for func, colorway in zip( |
| 61 | + [px.sunburst, px.treemap], ["sunburstcolorway", "treemapcolorway"] |
| 62 | + ): |
| 63 | + # Continuous colorscale |
| 64 | + fig = func( |
| 65 | + names=labels, |
| 66 | + parents=parents, |
| 67 | + values=values, |
| 68 | + color=values, |
| 69 | + color_continuous_scale="Viridis", |
| 70 | + range_color=(5, 15), |
| 71 | + ) |
| 72 | + assert fig.layout.coloraxis.cmin, fig.layout.coloraxis.cmax == (5, 15) |
| 73 | + # Discrete colorscale, color arg passed |
| 74 | + color_seq = px.colors.sequential.Reds |
| 75 | + fig = func( |
| 76 | + names=labels, |
| 77 | + parents=parents, |
| 78 | + values=values, |
| 79 | + color=labels, |
| 80 | + color_discrete_sequence=color_seq, |
| 81 | + ) |
| 82 | + assert np.all([col in color_seq for col in fig.data[0].marker.colors]) |
| 83 | + # Numerical color arg passed, fall back to continuous |
| 84 | + fig = func(names=labels, parents=parents, values=values, color=values,) |
| 85 | + assert [ |
| 86 | + el[0] == px.colors.sequential.Viridis |
| 87 | + for i, el in enumerate(fig.layout.coloraxis.colorscale) |
| 88 | + ] |
| 89 | + # Discrete colorscale, no color arg passed |
| 90 | + color_seq = px.colors.sequential.Reds |
| 91 | + fig = func( |
| 92 | + names=labels, |
| 93 | + parents=parents, |
| 94 | + values=values, |
| 95 | + color_discrete_sequence=color_seq, |
| 96 | + ) |
| 97 | + assert list(fig.layout[colorway]) == color_seq |
0 commit comments