![]() ![]() 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. With this approach, the color bar can be placed on any of the 3 axes, but you have to change the code accordingly to get it done.Legends in python and matplotlib can be a little tricky (it is quite a web of different objects). # When cax id specified, 'shrink' and 'aspect' properties are ignoredĬb = fig.colorbar(im0, cax=ax, orientation='vertical') # The third axes, ax is exclusive to the color bar # the width ratio of the 3rd axes must be small to get good result (here = 0.03)įig, ax = plt.subplots(1, 3, figsize=(9.5, 4), gridspec_kw=) # figsize plays important role must set it wide enough Here is the runnable code and the resulting plot. Note the use of gridspec_kw with proper values of width_ratios to obtain required widths of the subplots. So 3 axes are created for 2 required plots. One approach is to create an exclusive axes for plotting the color bar. In this case you have to pay attention to the position and the size of the two axis with these lines: ax1 = fig.add_axes() I know this is not the most elegant solution, for sure.Ī more robust and elegant solution is to keep 2 axes, but set their size and position when you define them: import matplotlib.pyplot as plt You need to pay attention to the position of this third axis with the parameters inside the method: fig.add_axes(). I solved with this turnaround: import matplotlib.pyplot as pltĪx3 = fig.add_axes()īasically, I add a third axis, turn it off and add to it the colorbar. import matplotlib.pyplot as pltįig, (ax1, ax2) = plt.subplots(1, 2, figsize = (16,8)) In order to reproduce you issue, I used this code, which basically shows the same result: the image on the right is slightly smaller than the left one due to the colorbar. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |