-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Categorical choropleths with legends #3468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
That's correct. |
You can shrink down the colorbars (one for each trace) to do something like: |
So, you want
By a similar argument, should we rename the Now, I'm not saying we shouldn't try to make trace-to-trace correspondences easier. This has been discussed during previous work on
Maybe we can find a way to declare those correspondence in the plot-schema. Thoughts? |
X and Y are terrible attribute names for pie, in just the same way as Z is a much worse attribute name than color for controlling the color of a choropleth marker, so no, obviously I would not favour those for pie! However, since you bring it up, pie’s label/value would be a great fit for bar, since they would not require transposition to swap orientation, as x and y currently do. Straw man aside: yes I would favour grandfathering in z as a legacy way of specifying marker.color if we added it to choropleth. |
The problem with playing discretization tricks with the colorbar is that it doesn’t have the same behaviour as the legend with respect to clicking to hide etc, so it’s inconsistent with the rest of the traces. This is a problem with parcats, parcoords etc as well. |
So this ⏫ isn't good enough? |
My main goal is to be able to specify the color of the country directly like we can specify the color of a marker in scatterX. Z doesn’t permit this atm, so no, declaring a correspondence doesn’t help :) Conversely, allowing CSS color names in an array called Z seems awkward. |
We could make |
We could, yes. And we could allow traces to be added to the legend. That would indeed allow me to make the figures I want. |
Thinking about this again maybe That way, user that inputs |
Heh ok that might work also. OTOH I could also see an argument that a true choropleth by definition expresses continuous values and that we should have a new trace type called indicatormap or something that accepts only categorical colors. Also: choropleth has a “showlegend” attribute atm that doesn’t do anything, which initially led me down this path ;) |
Right, all traces have a Related: |
Just to drill in a bit on the
OK, but how is this different from something like I think it would be quite weird to have |
I'd say the "primary" data arrays for
True, but it would also be weird to have |
Like I said, |
OK, so then the primary data array for |
Voting for keeping |
I'm not advocating for getting rid of |
OK, then should |
All in all, this whole debate comes down to: Is choropleth more like a heatmap or more like a scattergeo trace? I think it's more like a heatmap, @nicolaskruchten probably thinks it's more like scattergeo. Not sure who will win out 🐎 |
I would not oppose this. I think
Not really: I think attributes which control color should be called |
I'd argue that |
As I said above re choropleth traces without
|
Slipping in @nicolaskruchten 's creation for everyone's enjoyment 😄 |
It looks like px.choropleth z field still does not support categorical legend items? I'm not sure where this discussion ended, but at present setting z to a categorical series simply produces a blank map. Thanks! |
That’s right, z still supports only continuous color but you can now add multiple single-color choropleth traces with legend items to get the same effect. See https://plot.ly/python/choropleth-maps/ for an example with Plotly Express in Python. |
Hi! Thanks for getting back to me. Which section should I be looking at? The figure under "Discrete Colors" is essentially what I am aiming to create. However, the example code provided no longer runs. |
It definitely runs but it requires version 4.5.0 of Plotly.py which came out a couple of weeks ago. |
Hi, I want to know how to reorder the legeng items. Because sometimes these legends may have order meaning, for example bins, 1-10, 1-20, 21-30... |
Legend order depends on trace order in the |
@nicolaskruchten I saw that this topic was closed and included in #4386 but I still don't understand how to achieve a categorical choropleth using the documentation . My data is a Pandas DataFrame with two columns: the country name and the cluster it belongs in. I want to color the world map based on the cluster of each country - very similar to your examples. I had tried coloring a map by cluster in the past and this is as close as I was able to get using plotly.graph_objects. The issue with this is that the legend was still a continuous color scale, not a list of categories(i.e. cluster labels). |
@ahertel if you're looking for information on how to do this in Python I would recommend opening an issue in the Plotly.py repo, but for completeness, here's a PX example without GeoJSON for categorically-coloring statewise choropleths... it's basically the same as the one here https://plotly.com/python/choropleth-maps/#using-builtin-country-and-state-geometries but with categorical instead of continuous color: import plotly.express as px
fig = px.choropleth(locations=["CA", "TX", "NY", "AK"],
color=["yes", "no", "yes", "no"],
scope="usa", locationmode="USA-states")
fig.show() |
@nicolaskruchten Ah right, I forgot this was the JS page. Thank you so much for responding so quickly and taking the time to write the example code! It worked and it's so much simpler than all the stuff I was playing around with. Here's the result. |
Right now it doesn't seem like we can make a categorical choropleth by adding multiple traces with the same color and having legend items appear for them: https://codepen.io/nicolaskruchten/pen/MLwyOZ?editors=0010
Ideally I'd want to be able to:
marker.color
, instead of doing a dance withz
andcolorscale
So that I could easily switch between
choropleth
andscattergeo
like with this one: https://codepen.io/nicolaskruchten/pen/QYbNax?editors=0010The text was updated successfully, but these errors were encountered: