matplotlib colorbar placement and size - python

I'm using quadmesh to create a simple polar projection plot. Here's a minimal script which produces basically what I'm trying to do:
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as plt
def make_plot(data,fig,subplot):
nphi,nt = data.shape
phi_coords = np.linspace(0,np.pi*2,nphi+1) - np.pi/2.
theta_coords = np.linspace(0,np.radians(35),nt+1)
ax = fig.add_subplot(subplot,projection='polar')
ax.set_thetagrids((45,90,135,180,225,270,315,360),(9,12,15,18,21,24,3,6))
ax.set_rgrids(np.arange(10,35,10),fmt='%s\u00b0')
theta,phi = np.meshgrid(phi_coords,theta_coords)
quadmesh = ax.pcolormesh(theta,phi,data)
ax.grid(True)
fig.colorbar(quadmesh,ax=ax)
return fig,ax
a = np.zeros((360,71)) + np.arange(360)[:,None]
b = np.random.random((360,71))
fig = plt.figure()
t1 = make_plot(a,fig,121)
t2 = make_plot(b,fig,122)
fig.savefig('test.png')
The above script creates a plot which looks like this:
I would like the colorbars to:
Not overlap the 6 label.
be scaled such that they are approximately the same height as the plot.
Is there any trick to make this work properly? (Note that this layout isn't the only one I will be using -- e.g. I might use a 1x2 layout, or a 4x4 layout ... It seems like there should be some way to scale the colorbar to the same height as the associated plot...)

This combination (and values near to these) seems to "magically" work for me to keep the colorbar scaled to the plot, no matter what size the display.
plt.colorbar(im,fraction=0.046, pad=0.04)

You can do this with a combination of the pad, shrink, and aspect kwargs:
from __future__ import unicode_literals
import numpy as np
import matplotlib.pyplot as plt
def make_plot(data,fig,subplot):
nphi,nt = data.shape
phi_coords = np.linspace(0,np.pi*2,nphi+1) - np.pi/2.
theta_coords = np.linspace(0,np.radians(35),nt+1)
ax = fig.add_subplot(subplot,projection='polar')
ax.set_thetagrids((45,90,135,180,225,270,315,360),(9,12,15,18,21,24,3,6))
ax.set_rgrids(np.arange(10,35,10),fmt='%s\u00b0')
theta,phi = np.meshgrid(phi_coords,theta_coords)
quadmesh = ax.pcolormesh(theta,phi,data)
ax.grid(True)
cb = fig.colorbar(quadmesh,ax=ax, shrink=.5, pad=.2, aspect=10)
return fig,ax,cb
a = np.zeros((360,71)) + np.arange(360)[:,None]
b = np.random.random((360,71))
fig = plt.figure()
t1 = make_plot(a,fig,121)
t2 = make_plot(b,fig,122)
figure.colorbar doc
The best value for these parameters will depend on the aspect ratio of the axes.
The size of the axes seems to not get shrink-wrapped to the polar plot, thus in the 1x2 arrangement there is a lot of space above and below the plot that are part in the axes object, but empty. The size of the color bar is keyed off of the rectangular size, not the round size, hence why the default values are not working well. There is probably a way to do the shrink-wrapping, but I do not know how to do that.
An alternate method is to force your figure to be the right aspect ratio ex:
fig.set_size_inches(10, 4) # for 1x2
fig.set_size_inches(4, 10) # for 2x1
which makes the sub plots square, so the default values more-or-less work.

Related

How to fix limits after multiple use of ax.voxels method in python

I'm doing a research with 3D point clouds that I receive from Lidar. I split huge amount of points (up to 10 - 100 millions) into cubes, investigate their position and display results in a seperate voxels using Axes3D.voxels method. However, I face some problems while setting appropriate limits of Axes3D after multiple use of this method.
I define add_voxels function in order to display voxels immediately from np.array of positions of cubes inputted:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import itertools
def add_voxels(true_ids, ax):
shape_of_filled = true_ids.max(axis=0) + 1 # shape of building
filled = np.zeros(shape_of_filled)
for n in true_ids:
filled[n] = 1
x, y, z = np.indices(np.array(shape_of_filled) + 1)
return ax.voxels(x,y,z, filled)```
Then use it to plot my two clouds of cubes:
fig = plt.gcf() # get a reference to the current figure instance
ax = fig.gca(projection='3d') # get a reference to the current axes instance
cubecloud1 = np.array(list(itertools.product(range(2,4), range(2,4), range(2,4))))
cubecloud2 = np.array(list(itertools.product(range(4,7), range(4,7), range(4,7))))
add_voxels(cubecloud2, ax)
add_voxels(cubecloud1, ax)
plt.show()
It results in bad limits of display of voxel's position:
I'd like to have all the components displayed in a correct bounding box like this:
Or, at least, this (assuming bounding box includes invisible voxels too):
I could only make this work by setting the axis limits explicitly:
# [...]
faces2 = add_voxels(cubecloud2, ax)
faces1 = add_voxels(cubecloud1, ax)
points = list(faces1.keys()) + list(faces2.keys())
data = list(zip(*points))
xmin = min(data[0])
xmax = max(data[0])
ymin = min(data[1])
ymax = max(data[1])
zmin = min(data[2])
zmax = max(data[2])
ax.set_xlim3d(xmin, xmax)
ax.set_ylim3d(ymin, ymax)
ax.set_zlim3d(zmin, zmax)
plt.show()

Add space around axes so x ticks don't get cut off in seaborn clustermap [duplicate]

I'm struggling to deal with my plot margins in matplotlib. I've used the code below to produce my chart:
plt.imshow(g)
c = plt.colorbar()
c.set_label("Number of Slabs")
plt.savefig("OutputToUse.png")
However, I get an output figure with lots of white space on either side of the plot. I've searched google and read the matplotlib documentation, but I can't seem to find how to reduce this.
One way to automatically do this is the bbox_inches='tight' kwarg to plt.savefig.
E.g.
import matplotlib.pyplot as plt
import numpy as np
data = np.arange(3000).reshape((100,30))
plt.imshow(data)
plt.savefig('test.png', bbox_inches='tight')
Another way is to use fig.tight_layout()
import matplotlib.pyplot as plt
import numpy as np
xs = np.linspace(0, 1, 20); ys = np.sin(xs)
fig = plt.figure()
axes = fig.add_subplot(1,1,1)
axes.plot(xs, ys)
# This should be called after all axes have been added
fig.tight_layout()
fig.savefig('test.png')
You can adjust the spacing around matplotlib figures using the subplots_adjust() function:
import matplotlib.pyplot as plt
plt.plot(whatever)
plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
This will work for both the figure on screen and saved to a file, and it is the right function to call even if you don't have multiple plots on the one figure.
The numbers are fractions of the figure dimensions, and will need to be adjusted to allow for the figure labels.
All you need is
plt.tight_layout()
before your output.
In addition to cutting down the margins, this also tightly groups the space between any subplots:
x = [1,2,3]
y = [1,4,9]
import matplotlib.pyplot as plt
fig = plt.figure()
subplot1 = fig.add_subplot(121)
subplot1.plot(x,y)
subplot2 = fig.add_subplot(122)
subplot2.plot(y,x)
fig.tight_layout()
plt.show()
Sometimes, the plt.tight_layout() doesn't give me the best view or the view I want. Then why don't plot with arbitrary margin first and do fixing the margin after plot?
Since we got nice WYSIWYG from there.
import matplotlib.pyplot as plt
fig,ax = plt.subplots(figsize=(8,8))
plt.plot([2,5,7,8,5,3,5,7,])
plt.show()
Then paste settings into margin function to make it permanent:
fig,ax = plt.subplots(figsize=(8,8))
plt.plot([2,5,7,8,5,3,5,7,])
fig.subplots_adjust(
top=0.981,
bottom=0.049,
left=0.042,
right=0.981,
hspace=0.2,
wspace=0.2
)
plt.show()
In case anybody wonders how how to get rid of the rest of the white margin after applying plt.tight_layout() or fig.tight_layout(): With the parameter pad (which is 1.08 by default), you're able to make it even tighter:
"Padding between the figure edge and the edges of subplots, as a fraction of the font size."
So for example
plt.tight_layout(pad=0.05)
will reduce it to a very small margin. Putting 0 doesn't work for me, as it makes the box of the subplot be cut off a little, too.
Just use ax = fig.add_axes([left, bottom, width, height])
if you want exact control of the figure layout. eg.
left = 0.05
bottom = 0.05
width = 0.9
height = 0.9
ax = fig.add_axes([left, bottom, width, height])
plt.savefig("circle.png", bbox_inches='tight',pad_inches=-1)
inspired by Sammys answer above:
margins = { # vvv margin in inches
"left" : 1.5 / figsize[0],
"bottom" : 0.8 / figsize[1],
"right" : 1 - 0.3 / figsize[0],
"top" : 1 - 1 / figsize[1]
}
fig.subplots_adjust(**margins)
Where figsize is the tuple that you used in fig = pyplot.figure(figsize=...)
With recent matplotlib versions you might want to try Constrained Layout:
constrained_layout automatically adjusts subplots and decorations like
legends and colorbars so that they fit in the figure window while
still preserving, as best they can, the logical layout requested by
the user.
constrained_layout is similar to tight_layout, but uses a constraint
solver to determine the size of axes that allows them to fit.
constrained_layout needs to be activated before any axes are added to
a figure.
Too bad pandas does not handle it well...
The problem with matplotlibs subplots_adjust is that the values you enter are relative to the x and y figsize of the figure. This example is for correct figuresizing for printing of a pdf:
For that, I recalculate the relative spacing to absolute values like this:
pyplot.subplots_adjust(left = (5/25.4)/figure.xsize, bottom = (4/25.4)/figure.ysize, right = 1 - (1/25.4)/figure.xsize, top = 1 - (3/25.4)/figure.ysize)
for a figure of 'figure.xsize' inches in x-dimension and 'figure.ysize' inches in y-dimension. So the whole figure has a left margin of 5 mm, bottom margin of 4 mm, right of 1 mm and top of 3 mm within the labels are placed. The conversion of (x/25.4) is done because I needed to convert mm to inches.
Note that the pure chart size of x will be "figure.xsize - left margin - right margin" and the pure chart size of y will be "figure.ysize - bottom margin - top margin" in inches
Other sniplets (not sure about these ones, I just wanted to provide the other parameters)
pyplot.figure(figsize = figureSize, dpi = None)
and
pyplot.savefig("outputname.eps", dpi = 100)
For me, the answers above did not work with matplotlib.__version__ = 1.4.3 on Win7. So, if we are only interested in the image itself (i.e., if we don't need annotations, axis, ticks, title, ylabel etc), then it's better to simply save the numpy array as image instead of savefig.
from pylab import *
ax = subplot(111)
ax.imshow(some_image_numpyarray)
imsave('test.tif', some_image_numpyarray)
# or, if the image came from tiff or png etc
RGBbuffer = ax.get_images()[0].get_array()
imsave('test.tif', RGBbuffer)
Also, using opencv drawing functions (cv2.line, cv2.polylines), we can do some drawings directly on the numpy array. http://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html
# import pyplot
import matplotlib.pyplot as plt
# your code to plot the figure
# set tight margins
plt.margins(0.015, tight=True)

Function like plt.tight_layout() that changes figure size instead of axis size

plt.tight_layout() works by changing the axis size, so all elements in your figure fit within that figure frame.
See:
y=np.random.normal(size=100)
fig, ax = plt.subplots(); plt.plot(y)
plt.xlabel('time / s.', fontsize=40)
fig.set_size_inches([5, 2])
plt.tight_layout()
The axis got resized so all elements could fit in the figure. I want my axis size to remain constant. Is there an alternative that resizes the figure while leaving the axis untouched?
The sizes of the axes are specified in terms of figure coordinates. I.e. by default a single subplot is 77.5% of the figure width wide and 77% of the figure height high. So you cannot leave the axes untouched when changing the figure size, because the one depends on the other.
Now it depends on the goal of calling tight_layout and the desired output.
The problem
Running the code from the question results in the following figure,
As can be seen, the oversized xlabel is cut.
Saving the figure
If for example you are interested in saving your figure, you may do so without calling tight_layout() and instead specify the bbox_inches="tight" in the call to savefig.
plt.savefig("some.png", bbox_inches="tight")
This will produce a figure which is larger than the original one without changing any of the positions of the objects inside. It's hence different from tight_layout because it does not change the layout and spacings at all. The drawback here is that it will only affect the saved figure, not the one shown on screen.
Showing the figure
Now you can in principle do the same to your figure which is on screen. This is very hacky and will have consequences on how your axes are stored. The following is a function tight_figure, which treats the figure as if it was saved, but lets it stay within the original canvas on screen.
import numpy as np
import matplotlib.pyplot as plt
import io
from matplotlib.transforms import Bbox, TransformedBbox, Affine2D
from matplotlib import tight_bbox
def tight_figure(fig,**kwargs):
canvas = fig.canvas._get_output_canvas("png")
print_method = getattr(canvas, 'print_png')
print_method(io.BytesIO(), dpi=fig.dpi,
facecolor=fig.get_facecolor(), dryrun=True)
renderer = fig._cachedRenderer
bbox_inches = fig.get_tightbbox(renderer)
bbox_artists = fig.get_default_bbox_extra_artists()
bbox_filtered = []
for a in bbox_artists:
bbox = a.get_window_extent(renderer)
if a.get_clip_on():
clip_box = a.get_clip_box()
if clip_box is not None:
bbox = Bbox.intersection(bbox, clip_box)
clip_path = a.get_clip_path()
if clip_path is not None and bbox is not None:
clip_path = \
clip_path.get_fully_transformed_path()
bbox = Bbox.intersection(
bbox, clip_path.get_extents())
if bbox is not None and (
bbox.width != 0 or bbox.height != 0):
bbox_filtered.append(bbox)
if bbox_filtered:
_bbox = Bbox.union(bbox_filtered)
trans = Affine2D().scale(1.0 / fig.dpi)
bbox_extra = TransformedBbox(_bbox, trans)
bbox_inches = Bbox.union([bbox_inches, bbox_extra])
pad = kwargs.pop("pad_inches", None)
if pad is None:
pad = plt.rcParams['savefig.pad_inches']
bbox_inches = bbox_inches.padded(pad)
tight_bbox.adjust_bbox(fig, bbox_inches, canvas.fixed_dpi)
w = bbox_inches.x1 - bbox_inches.x0
h = bbox_inches.y1 - bbox_inches.y0
fig.set_size_inches(w,h)
y=np.random.normal(size=100)
fig, ax = plt.subplots(); plt.plot(y)
plt.xlabel('time / s.', fontsize=40)
fig.set_size_inches([5, 2])
tight_figure(fig)
plt.show()
The output with tight_figure would look like

MatPlotLib share y across entire figure

Here is an example of a plot I am generating using Pandas and MatPlotLib.
Please note that even though I stated sharey = True in the code, the y-Axis is only shared across each row.This isn't much help to me, as I need to compare all plots against each other.
How can I use just one axis for the entire plot? I'd also ideally want that axis repeated for each plot.
Thank you!
for field in chosenFields:
for dataID in dataIDs:
fig = plt.figure()
subplots = [fig.add_subplot(rows, cols, subplot) for
subplot in range(1, len(fileNames) + 1)]
for subplot, plot, fileName in zip(subplots, plots, fileNames):
graphData = Build_Graphs.prepareOutputGraph(plot[0],
field,
dataID,
batchName,
segmentName)
haveLegend = True if len(graphData.columns) < 12 else False
subplt = graphData.plot(ax = subplot,
legend = haveLegend,
title = fileName,
sharey = True)
Build_Graphs.labelGraph(subplt, field, dataID, batchName, segmentName)
plt.get_current_fig_manager().window.showMaximized()
writeOutput(outputDirectory, field, dataID, graphData)
plt.show()
In order to get the same axis range repeated for each plot, you can get_ylim from all existing and use global min/max to set all the axes,
import numpy as np
import matplotlib.pyplot as plt
#Setup dummy data
fig, subplots = plt.subplots(2,3)
x = np.linspace(0,2.*np.pi,1000)
[sp.plot(x,np.sin(x)*(10*np.random.randn(1))) for sp in subplots.reshape(-1)]
#Get global minimum and maximum y values accross all axis
ygmin = 0.; ygmax = 0.
for sp in subplots.reshape(-1):
ymin, ymax = sp.get_ylim()
ygmin = min(ygmin,ymin)
ygmax = max(ygmax,ymax)
#Set same axis for all subplots
[sp.set_ylim((ygmin,ygmax)) for sp in subplots.reshape(-1)]
plt.show()
As suggested by paulH, this can also be done with sharey=True as part of plt.subplots. However, the y axis is hidden for anything but the first axis by default, so you need to tell matplotlib to show these again,
import numpy as np
import matplotlib.pyplot as plt
#Setup dummy data
fig, subplots = plt.subplots(2,3,sharey=True)
x = np.linspace(0,2.*np.pi,1000)
[sp.plot(x,np.sin(x)*(10*np.random.randn(1))) for sp in subplots.reshape(-1)]
#Show axis on all subplots
[plt.setp(sp.get_yticklabels(), visible=True) for sp in subplots.reshape(-1)]
plt.show()
You can also specify sharey="col" or sharey="row" to share axes alone the column or row respectively.

Reduce left and right margins in matplotlib plot

I'm struggling to deal with my plot margins in matplotlib. I've used the code below to produce my chart:
plt.imshow(g)
c = plt.colorbar()
c.set_label("Number of Slabs")
plt.savefig("OutputToUse.png")
However, I get an output figure with lots of white space on either side of the plot. I've searched google and read the matplotlib documentation, but I can't seem to find how to reduce this.
One way to automatically do this is the bbox_inches='tight' kwarg to plt.savefig.
E.g.
import matplotlib.pyplot as plt
import numpy as np
data = np.arange(3000).reshape((100,30))
plt.imshow(data)
plt.savefig('test.png', bbox_inches='tight')
Another way is to use fig.tight_layout()
import matplotlib.pyplot as plt
import numpy as np
xs = np.linspace(0, 1, 20); ys = np.sin(xs)
fig = plt.figure()
axes = fig.add_subplot(1,1,1)
axes.plot(xs, ys)
# This should be called after all axes have been added
fig.tight_layout()
fig.savefig('test.png')
You can adjust the spacing around matplotlib figures using the subplots_adjust() function:
import matplotlib.pyplot as plt
plt.plot(whatever)
plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
This will work for both the figure on screen and saved to a file, and it is the right function to call even if you don't have multiple plots on the one figure.
The numbers are fractions of the figure dimensions, and will need to be adjusted to allow for the figure labels.
All you need is
plt.tight_layout()
before your output.
In addition to cutting down the margins, this also tightly groups the space between any subplots:
x = [1,2,3]
y = [1,4,9]
import matplotlib.pyplot as plt
fig = plt.figure()
subplot1 = fig.add_subplot(121)
subplot1.plot(x,y)
subplot2 = fig.add_subplot(122)
subplot2.plot(y,x)
fig.tight_layout()
plt.show()
Sometimes, the plt.tight_layout() doesn't give me the best view or the view I want. Then why don't plot with arbitrary margin first and do fixing the margin after plot?
Since we got nice WYSIWYG from there.
import matplotlib.pyplot as plt
fig,ax = plt.subplots(figsize=(8,8))
plt.plot([2,5,7,8,5,3,5,7,])
plt.show()
Then paste settings into margin function to make it permanent:
fig,ax = plt.subplots(figsize=(8,8))
plt.plot([2,5,7,8,5,3,5,7,])
fig.subplots_adjust(
top=0.981,
bottom=0.049,
left=0.042,
right=0.981,
hspace=0.2,
wspace=0.2
)
plt.show()
In case anybody wonders how how to get rid of the rest of the white margin after applying plt.tight_layout() or fig.tight_layout(): With the parameter pad (which is 1.08 by default), you're able to make it even tighter:
"Padding between the figure edge and the edges of subplots, as a fraction of the font size."
So for example
plt.tight_layout(pad=0.05)
will reduce it to a very small margin. Putting 0 doesn't work for me, as it makes the box of the subplot be cut off a little, too.
Just use ax = fig.add_axes([left, bottom, width, height])
if you want exact control of the figure layout. eg.
left = 0.05
bottom = 0.05
width = 0.9
height = 0.9
ax = fig.add_axes([left, bottom, width, height])
plt.savefig("circle.png", bbox_inches='tight',pad_inches=-1)
inspired by Sammys answer above:
margins = { # vvv margin in inches
"left" : 1.5 / figsize[0],
"bottom" : 0.8 / figsize[1],
"right" : 1 - 0.3 / figsize[0],
"top" : 1 - 1 / figsize[1]
}
fig.subplots_adjust(**margins)
Where figsize is the tuple that you used in fig = pyplot.figure(figsize=...)
With recent matplotlib versions you might want to try Constrained Layout:
constrained_layout automatically adjusts subplots and decorations like
legends and colorbars so that they fit in the figure window while
still preserving, as best they can, the logical layout requested by
the user.
constrained_layout is similar to tight_layout, but uses a constraint
solver to determine the size of axes that allows them to fit.
constrained_layout needs to be activated before any axes are added to
a figure.
Too bad pandas does not handle it well...
The problem with matplotlibs subplots_adjust is that the values you enter are relative to the x and y figsize of the figure. This example is for correct figuresizing for printing of a pdf:
For that, I recalculate the relative spacing to absolute values like this:
pyplot.subplots_adjust(left = (5/25.4)/figure.xsize, bottom = (4/25.4)/figure.ysize, right = 1 - (1/25.4)/figure.xsize, top = 1 - (3/25.4)/figure.ysize)
for a figure of 'figure.xsize' inches in x-dimension and 'figure.ysize' inches in y-dimension. So the whole figure has a left margin of 5 mm, bottom margin of 4 mm, right of 1 mm and top of 3 mm within the labels are placed. The conversion of (x/25.4) is done because I needed to convert mm to inches.
Note that the pure chart size of x will be "figure.xsize - left margin - right margin" and the pure chart size of y will be "figure.ysize - bottom margin - top margin" in inches
Other sniplets (not sure about these ones, I just wanted to provide the other parameters)
pyplot.figure(figsize = figureSize, dpi = None)
and
pyplot.savefig("outputname.eps", dpi = 100)
For me, the answers above did not work with matplotlib.__version__ = 1.4.3 on Win7. So, if we are only interested in the image itself (i.e., if we don't need annotations, axis, ticks, title, ylabel etc), then it's better to simply save the numpy array as image instead of savefig.
from pylab import *
ax = subplot(111)
ax.imshow(some_image_numpyarray)
imsave('test.tif', some_image_numpyarray)
# or, if the image came from tiff or png etc
RGBbuffer = ax.get_images()[0].get_array()
imsave('test.tif', RGBbuffer)
Also, using opencv drawing functions (cv2.line, cv2.polylines), we can do some drawings directly on the numpy array. http://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html
# import pyplot
import matplotlib.pyplot as plt
# your code to plot the figure
# set tight margins
plt.margins(0.015, tight=True)

Categories

Resources