![]() # Compute the ratio between the pixel offset and the total amount of pixelsĪnd then set the legend with: ax.legend(loc=(pad_xaxis,0. Or # Find how may pixels there are on the x-axis ![]() Pad_axes = inv.transform((pad_pixels, 0)) - inv.transform((0,0)) # Inverse transform two points on the display and find the relative distance I have tried several techniques, and even some that has me calling on each individual subplot but am sure there is a simple solution. I would like to keep the titles of each subplot and remove the legends since they show the same verbiage. Then, I found that both of the following work and give the same value for the padding: # Define inverse transform, transforms display coordinates (pixels) to axes coordinates I would like to remove legends from individual subplots in panda. Pad_pixels = pad_inches*fig.dpi #convert from inches to pixels using the figure's dpi Pad_inches = pad_points/72.0 #convert from points to inches Pad_points = bapad*fontsize + axline #padding is defined in relative to font size y_value determines the height of the legend.Īfter spending way too much time on this, I've come up with the following satisfactory solution (the Transformations Tutorial definitely helped): bapad = plt.rcParamsĪxline = plt.rcParams #need this, otherwise the result will be off by a few pixels This value can be changed at your desire. x_value has been eyeballed for a good correspondence with the "normal" legend. With the values for x_value and y_value the legend can be positioned in the subplot. ![]() Hence, the figure size does not change anything to the legend positioning anymore. I then create the legend based on the dimensions of the entire figure. So what I am now doing is basically getting the coordinates from the subplot. The problem however is that it is also depended on the figure size.įig, ax = plt.subplots(1, 2, sharex = False, sharey = False)įig.legend(,, loc = (axbox.x0 + x_value, axbox.y0 + y_value)) I saw the answer you posted and tried it out. ![]() What I'd like is that the legend in the right plot is moved down somewhat so it no longer overlaps with the line.Īs a last resort I could change the axis limits, but I would very much like to avoid that. Would it be possible to somehow use the borderaxespad property of the first legend? Though I'm not succeeding at getting that to work.Įdit: Here is a (very simplified) illustration of the problem: import matplotlib.pyplot as pltįig, ax = plt.subplots(1, 2, sharex=False, sharey=False) I know I can specify a custom position by using loc=(x,y), but then I can't figure out how to get the padding between the legend and the y-axis to be equal to that used by the other legends. What I'd like to do is to have the legend placed somewhere in between the 'upper left' and 'center left' locations, while keeping the padding between it and the y-axis equal to the legends in the other subplots (that are placed using one of the predefined legend location keywords). One of these subplots is giving me some trouble, as none of the axes corners or centers are free (or can be freed up) for placing the legend. Using Python 3.8 import matplotlib.pyplot as pltįig, axes = plt.subplots(4,2, figsize=(6.5,4.0), constrained_layout=True)Īx.hist(data, color=colors,edgecolor='black', alpha=0.I'm creating a figure with multiple subplots. How would I add the global legend? I tried using fig.legend((v1, v2, v3), ('v1', 'v2', 'v3'), 'lower left') as suggested here, but I don't think this works with histograms. It can be left justified, centered, or spread out. I am thinking along the bottom, but would consider better answers. The code below mimics some data, and I'd like to have a global legend somewhere on the figure. How can I add a global legend for all the histograms in the subplots?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |