plotly / plotly.py

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
https://plotly.com/python/
MIT License
16.39k stars 2.56k forks source link

make error messages more helpful when choropleth features and data don't match #4529

Open afeld opened 9 months ago

afeld commented 9 months ago

I teach a class for students that are new to coding. We use plotly, and I'm happy to have a package that gets students good-looking graphs in so little code.

I introduce plotly's choropleth maps, and have repeatedly run into the issue where the students get confused by what the featureidkey is referring to and how that relates to the locations. I do my best to explain this, but they are (understandably) confused working across CSVs and GeoJSON. One student just hit an issue where they were trying to use FIPS codes, but their locations column had leading whitespace. When the rows are unable to be matched to a feature, it fails silently.

My feature request is to provide a warning when the featureidkey and locations have zero matches.

I haven't contributed to the plotly codebase before, but I'd be open to doing so if the maintainers are open to receiving this. Presumably the place to add this check is one of the validators?

Thanks!

alexcjohnson commented 9 months ago

Thanks @afeld - I love those class notes, beautiful!

It would certainly be useful to have a warning in this case, but it's a little tricky to figure out how to make that happen. The Python-side validators are all codegen from the JavaScript schema, and anyway they aren't intended to capture interactions between different attributes. The obvious place to put this is in JavaScript where we put this all together, and some error situations already do generate messages there, though it's not clear to me if the cases you're describing would: https://github.com/plotly/plotly.js/blob/master/src/lib/geo_location_utils.js - that said even if we do get the right messages there you'll only see them in the JS console, not in Python or in the main browser window, and most Python users won't know to look there.

afeld commented 9 months ago

Thanks, and thanks for the quick response!

After a bunch of experimentation, reading source code, and some help from ChatGPT, I've learned that the file you pointed me to does have that functionality already, but it's disabled by default. It can be enabled with:

Plotly.setPlotConfig({ logging: 2 });

…but that isn't documented 🙃 Created an example in Codepen.

Screenshot 2024-02-24 at 2 53 02 AM

Unfortunately, it's not clear to me how to do that from Python/Colab. Configuration options can be passed to .show() … but logging and notifyOnLogging "should ONLY be set via Plotly.setPlotConfig". Created a corresponding example in Colab.

Thoughts?

afeld commented 9 months ago

even if we do get the right messages there you'll only see them in the JS console, not in Python or in the main browser window, and most Python users won't know to look there.

notifyOnLogging: 2 sidesteps that problem by temporarily displaying them on the map. I'd argue that should be the default setting to avoid confusion, despite the risk of causing visual noise.

afeld commented 9 months ago

Issue around allowing logging to be configured via newPlot() in JavaScript (and thus show() in Python): https://github.com/plotly/plotly.js/issues/4555