Label of the colorbar using matplotlib [duplicate] - python

By default matplotlib would position colorbar labels alongside the vertical colorbars. What is the best way to force the label to be on top of a colorbar? Currently my solution needs adjusting labelpad and y values depending on size of the label:
import numpy as np
import matplotlib.pylab as plt
dat = np.random.randn(10,10)
plt.imshow(dat, interpolation='none')
clb = plt.colorbar()
clb.set_label('label', labelpad=-40, y=1.05, rotation=0)
plt.show()
Is there a better, more generic way to do this?

You could set the title of the colorbar axis (which appears above the axis), rather than the label (which appears along the long axis). To access the colorbar's Axes, you can use clb.ax. You can then use set_title, in the same way you can for any other Axes instance.
For example:
import numpy as np
import matplotlib.pylab as plt
dat = np.random.randn(10,10)
plt.imshow(dat, interpolation='none')
clb = plt.colorbar()
clb.ax.set_title('This is a title')
plt.show()

Related

Colorbar at the top of a figure: matplotlib [duplicate]

I have a matplotlib plot with a colorbar attached. I want to position the colorbar so that it is horizontal, and underneath my plot.
I have almost done this via the following:
plt.colorbar(orientation="horizontal",fraction=0.07,anchor=(1.0,0.0))
But the colorbar is still overlapping with the plot slightly (and the labels of the x axis). I want to move the colorbar further down, but I can't figure out how to do it.
using padding pad
In order to move the colorbar relative to the subplot, one may use the pad argument to fig.colorbar.
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)
fig, ax = plt.subplots(figsize=(4,4))
im = ax.imshow(np.random.rand(11,16))
ax.set_xlabel("x label")
fig.colorbar(im, orientation="horizontal", pad=0.2)
plt.show()
using an axes divider
One can use an instance of make_axes_locatable to divide the axes and create a new axes which is perfectly aligned to the image plot. Again, the pad argument would allow to set the space between the two axes.
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np; np.random.seed(1)
fig, ax = plt.subplots(figsize=(4,4))
im = ax.imshow(np.random.rand(11,16))
ax.set_xlabel("x label")
divider = make_axes_locatable(ax)
cax = divider.new_vertical(size="5%", pad=0.7, pack_start=True)
fig.add_axes(cax)
fig.colorbar(im, cax=cax, orientation="horizontal")
plt.show()
using subplots
One can directly create two rows of subplots, one for the image and one for the colorbar. Then, setting the height_ratios as gridspec_kw={"height_ratios":[1, 0.05]} in the figure creation, makes one of the subplots much smaller in height than the other and this small subplot can host the colorbar.
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)
fig, (ax, cax) = plt.subplots(nrows=2,figsize=(4,4),
gridspec_kw={"height_ratios":[1, 0.05]})
im = ax.imshow(np.random.rand(11,16))
ax.set_xlabel("x label")
fig.colorbar(im, cax=cax, orientation="horizontal")
plt.show()
Edit: Updated for matplotlib version >= 3.
Three great ways to do this have already been shared in this answer.
The matplotlib documentation advises to use inset_locator. This would work as follows:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import numpy as np
rng = np.random.default_rng(1)
fig, ax = plt.subplots(figsize=(4,4))
im = ax.imshow(rng.random((11, 16)))
ax.set_xlabel("x label")
axins = inset_axes(ax,
width="100%",
height="5%",
loc='lower center',
borderpad=-5
)
fig.colorbar(im, cax=axins, orientation="horizontal")

How can I use SymLogNorm with matplotlib but still have colorbar appear linear?

Is there a way to use SymLogNorm with imshow, but make the colorbar basically stretch the colors so that the colorbar actually appears linear?
Below is a short code
from pylab import *
import numpy as np
from matplotlib.colors import SymLogNorm
data = np.random.uniform(low=-10, high=10, size=(10,10))
norm = SymLogNorm(2,vmin=-10,vmax=10)
fig, axes = plt.subplots()
im = axes.imshow(data,extent=[-10,10,-10,10],cmap=plt.cm.jet,norm=norm)
cb = fig.colorbar(im)
that produces this
I basically want this image, but want to stretch the colorbar so the ticks appear linear, not log.

How to change the frequency of labeling the x and y axis in matplotlib in python?

I am trying to plot a circle with a grid being shown. I wrote the following script which gives the below picture. However, the labels on the axes are interfering together. How to make the label appear (..,-10,-5,0,5,10,...) KEEPING the grid as it appears in the below figure?. I want to keep the dimension of the grid cell as 1*1 dimension.
I tried to use plt.locator_params(), but the dimension of the grid cell changed and became bigger.
import numpy as np
import matplotlib.pyplot as plt
import math
from matplotlib.pyplot import figure
R1=28
n=64
t=np.linspace(0, 2*np.pi, n)
x1=R1*np.cos(t)
y1=R1*np.sin(t)
plt.axis("square")
plt.grid(True, which='both', axis='both')
plt.xticks(np.arange(min(x1)-2,max(x1)+2, step=1))
plt.yticks(np.arange(min(y1)-2,max(y1)+2, step=1))
#plt.locator_params(axis='x', nbins=5)
#plt.locator_params(axis='y', nbins=5)
plt.plot(x1,y1)
plt.legend()
plt.show()
Not a matplotlib expert, so there may be a better way to do this, but perhaps like the following:
from matplotlib.ticker import MultipleLocator
...
fig, ax = plt.subplots(figsize=(6, 6))
ax.plot(x1,y1)
ax.xaxis.set_minor_locator(MultipleLocator())
ax.xaxis.set_major_locator(MultipleLocator(5))
ax.yaxis.set_minor_locator(MultipleLocator())
ax.yaxis.set_major_locator(MultipleLocator(5))
ax.grid(True, which='both', axis='both')
plt.show()

How to avoid dataseries to fall under legend in matplotlib plots?

Is there any automatic way of manipulating legend in matplotlib to avoid overlapping data points and legend? I have lots of data points and fixed y axis range, can I instruct matplotlib move the legend to left or down if it's on datapoints. Thanks
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(1, 100)
y = -1/x
plt.plot(x,y, label='x and y', linewidth=30)
plt.legend()
plt.show()
You can manually position the legend to the position that you want it, the documentation can be found here.. You can also move it off the plotting area of the graph to avoid any chance of an overlap as shown in the code below:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1, 100)
y = -1/x
plt.plot(x,y, label='x and y', linewidth=30)
#adjust the plot to allow the legend to fit nicely
plt.subplots_adjust(left=0.1,right = 0.75)
plt.legend(bbox_to_anchor=(1.01, 0.5), loc=2) # move the legend
plt.show()
The resulting image is shown below:
Note: If you plan on moving the legend outside of the plotting area then you may have to adjust your plots as I have done in the code above.

How can I create a discrete colorbar using a colormap from mpl_toolkits.basemap.cm?

When I plot the pcolormesh plot use the colormap from matplotlib.cm (like "jet", "Set2", etc), I can use:
cMap = plt.cm.get_cmap("jet",lut=6)
The colorbar shows like this:
But if I want to call the colormap from the Basemap package (like GMT_drywet, GMT_no_green, etc). I can't use plt.cm,get_cmap to get these colormap and divide them.
Does mpl_toolkits.basemap.cm have a similiar function like lut?
Expanding on #tacaswell's comment above, you can achieve the same functionality using the _resample method. This will produce segmented colormaps for pcolor/pcolormesh plots which don't generate discrete-stepped colorbars like contourf. To achieve the same effect as you did with jet in your question:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import cm
plt.figure()
cmap = cm.GMT_drywet._resample(6)
pm = plt.pcolormesh(np.random.rand(10,8), cmap=cmap)
plt.colorbar(pm, orientation='horizontal')
plt.show()
As long as the plot you are making has discrete color values (e.g. contour or contourf), then colorbar should automatically generate a colorbar with discrete steps. Here's a plot based on the first example from the basemap documentation:
from mpl_toolkits.basemap import Basemap, cm
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(1, 1)
ax.hold(True)
map = Basemap(projection='ortho',lat_0=45,lon_0=-100,resolution='l')
map.drawcoastlines(linewidth=0.25)
map.drawcountries(linewidth=0.25)
map.fillcontinents(color='coral',lake_color='aqua')
map.drawmapboundary(fill_color='aqua')
map.drawmeridians(np.arange(0,360,30))
map.drawparallels(np.arange(-90,90,30))
nlats = 73; nlons = 145; delta = 2.*np.pi/(nlons-1)
lats = (0.5*np.pi-delta*np.indices((nlats,nlons))[0,:,:])
lons = (delta*np.indices((nlats,nlons))[1,:,:])
wave = 0.75*(np.sin(2.*lats)**8*np.cos(4.*lons))
mean = 0.5*np.cos(2.*lats)*((np.sin(2.*lats))**2 + 2.)
x, y = map(lons*180./np.pi, lats*180./np.pi)
map.contourf(x,y,wave+mean,15, alpha=0.5, cmap=cm.GMT_drywet)
cb = map.colorbar()
plt.show()

Categories

Resources