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.Python Dictionaries Access Items Change Items Add Items Remove Items Loop Dictionaries Copy Dictionaries Nested Dictionaries Dictionary Methods Dictionary Exercise Python If.Else Python While Loops Python For Loops Python Functions Python Lambda Python Arrays Python Classes/Objects Python Inheritance Python Iterators Python Polymorphism Python Scope Python Modules Python Dates Python Math Python JSON Python RegEx Python PIP Python Try. # 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 import numpy as np import matplotlib.pyplot as plt def functions (c,x. To plot data and draw a colorbar or legend in one go, pass a location (e.g. I want to create a legend where each value in np.arange (0,20,5) is shown in both y and z figures as c XXX where XXX is the c used inside the for loop. In matplotlib, colorbars are added to the edges of subplots using the figure. # 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=) The function outputs two graphs y and z which are plotted in different figures. # 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() For example, you can use the following syntax to place the legend in the upper left corner of the plot: plt. ) This works, except that the legend is left aligned so that loc specifies the left edge/corner of the legend box and not the center. To change the position of a legend in Matplotlib, you can use the plt.legend () function. If True, legend marker is placed to the left of the legend label. This option can be quite slow for plots with large amounts of data your plotting speed may benefit from providing a specific location. Default is None, which means using rcParams'legend.markerscale' (default: 1.0). from matplotlib.lines import Line2D customlines Line2D (0, 0, color cmap (0.), lw 4), Line2D (0, 0, color cmap (.5), lw 4), Line2D (0, 0, color cmap (1.), lw 4) fig, ax plt. The string 'best' places the legend at the location, among the nine locations defined so far, with the minimum overlap with other drawn artists. 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 and in theory I should be able to position the legend by specifying the loc keyword with a tuple: fig.legend (., loc (axbox.x0+0.5axbox.width, axbox.y0-0.08). The relative size of legend markers compared with the originally drawn ones. You need to pay attention to the position of this third axis with the parameters inside the method: fig.add_axes(). It does not accept legend position strings. In its current implementation (version 1.2.3) the 'legend' argument of plot accepts only a boolean or the string 'reverse': legend : False/True/'reverse'. 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. To clarify the original answer, there is presently no way to do this through. 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 |