![]() So it may be necessary to go back into the legend and rebuild it entirely. This will be sufficient for many peoples choropleth map needs, but often times in maps you superimpose additional things, such as roads or other types of boundaries. Patch = patches.Polygon(xy2, fc=face_col) Width, height = handlebox.width, handlebox.height X0, y0 = handlebox.xdescent, handlebox.ydescentįace_col = orig_handle.get_markerfacecolor() class MapHandler:ĭef legend_artist(self, legend, orig_handle, fontsize, handlebox): ![]() (Don’t take this as a nice example legend glyph, just threw something together very quick!) I also add in different labels to signify the boundary edges for the choropleth bins. So hacking together from a matplotlib doc tutorial, we can make a custom handler to draw our prespecified glyph. ![]() If you go down the rabbit hole of geopandas objects, in this scenario the matplotlib handler is actually the same Line2D type object as if you call ax.plot(). ArcGIS has the ability to use different glyphs in its legends, and it is common to use a blocky type glyph for geopolitical boundaries (which have unnatural straight lines). Plt.savefig('Leg05.png', dpi=1000, bbox_inches='tight')īut now we have circles. geopandas objects have a special set of arguments, where you can pass information to class the map and make a legend: # Make the legend not the raster scale The geopandas mapping user guide has an example of making a nicer sized continuous legend, but overall I find making classed choropleth maps much easier in general. I have a tough time with continuous color ramps. Nc.plot(column='wat_prop', edgecolor='grey', linewidth=0.2, ax=ax, legend=True) So default geopandas uses a continuous color ramp: # Get counties for all US Now for the second example in the post, is going to show some examples munging with geopandas objects/plots. You can just rearrange the objects you get back from combo_legend to sort them in the order you want. Note that the set() call makes it so the order may not be how you want. # Can pass these to legendĬomb = Īx.scatter(x,y1,c='blue', edgecolor='white', So they do not need to per se be collections of similar items, just have the same text label. I made a simple function combo_legend(ax) to combine items that have the same label in such matplotlib figures. Handler, labeler = ax.get_legend_handles_labels()Īx.legend(hd, lab, loc="center left", bbox_to_anchor=(1, 0.5)) And so combining the lines and polygons together, we can make the legend how we want it. You can think of handles as just points/lines/polygons that refer to individual parts of the legend. You can combine the legend items by scooping out the original objects, here via the ax.get_* function to get the labels and the “handles”. Plt.savefig('Leg01.png', dpi=500, loc="center left", bbox_inches='tight') First example will show superimposing lines and error bars – even though I only have two labels, they each get their own slot in the resulting legend. First, one thing python does not do, even if you name things the same, it does not combine them in a legend. ![]() Here are a few notes I have collected over different projects. Legends in python and matplotlib can be a little tricky (it is quite a web of different objects).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |