-
-
Notifications
You must be signed in to change notification settings - Fork 981
ENH: Easier categorical plots (WIP) #2012
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
base: main
Are you sure you want to change the base?
Conversation
@TLouf Thanks for looking into it. I like this approach a lot! If you can finish support for other geom types, add tests, and document this in the User Gude this would be a super valuable contribution! |
Just extended to other geometry types. Not as easy as I thought initially. In particular, for polygons the default Anyway I'm quite satisfied with the kind of results I get from shapely.geometry import LineString, Point, Polygon
import shapely.wkt as wkt
import geopandas as gpd
linea = LineString([(1, 1), (2, 2), (3, 2), (5, 3)])
lineb = LineString([(3, 4), (5, 7), (12, 2), (10, 5), (9, 7.5)])
linec = LineString([(3, 3), (5,5), (9, 7.5)])
pointa = Point(2, 1.7)
pointb = Point(3, 4)
lined = wkt.loads('LINESTRING EMPTY')
gdf = gpd.GeoDataFrame(
{'num': [1, 8, 2, 5, 1, 5], 'type': ["M", "S", None, "S", "M", "S"]},
geometry=[Polygon(linea), lined, lineb, linec, pointa, pointb], crs="epsg:4326"
)
gdf.plot(
column='type', legend=True, edgecolor={'M': 'r', 'S': 'k'},
hatch={'M': 'xx', 'S': '//'}, alpha={'M': 0.5, 'S': 1},
color={'M': 'b', 'S': 'r'}, marker={'M': '+', 'S': '+'},
missing_kwds={'color': 'gray'}, linewidth=[1, 4, 1, 5, 1, 1]
) I didn't consider the implications to |
Nice job!
Makes sense.
Just pick the most sensible to cover all geometry tupes. So (I'll do the code review later) |
I've rebased onto master to integrate #2019 and I get your new test failing on the legend part @martinfleis. For instance I get the following for the first test plot: So only appears in the legend what's actually plotted, because the legend is created automatically. IMO this makes sense, but I never actually use The two other tests which are failing are because I took out all the By the way I also enabled the dictionary style keyword arguments to not contain all categories, yielding the default style for the geometries which are not part of the given categories. In practice this enables to do this:
|
Actually I've been thinking, it would make more sense to me if the legend of a plot with a classifying |
Generally, yes. But we should allow both options because in some cases, the colorbar is not fully legible. With |
fixes #1269 #1379
Here's a proposal to handle more easily categorical plots and their legends. The main idea is to give the proper label to each collection that we plot, so that the legend can be created automatically by calling
ax.legend
without manually creating handles and labels. This allows lines to be represented by lines, points by points and polygons by squares in the legend directly. Now some may prefer to have each geometry type in its own legend, but I went for the simplest here.When provided with a
color
that maps values to colors as suggested in the issue, we treat it like a categorical plot with a custom colormap, which makes sense I think. I imagined a user would intuitively try to do the same with all kinds of styling arguments, so I also tried to implement that.Here are two example plots:
This is a WIP as I've only adapted
_plot_linestring_collection
for now. Also, I quickly fixed the usage of "scheme" but didn't test thoroughly yet, and there are still some broken edge cases, in particular when the style mappings provided toplot
exclude some of the present categories.Anyway I submitted at this stage to get feedback on the approach, so let me know what you think!