Python Fix colorbar in plot - python

I am trying to get rid of the overlap of the images and colorbar values on the right side, but nothing seems to work. I have tried the 'tight' command and to shrink the size of the color bar text. The former does not help and the latter does not seem to work at all. (Is this possible?) I need a way to read these numbers, it does not really matter how. (As long as they don't overlap)
gs = gridspec.GridSpec(1, 2)
gs0 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs[0])
gs1 = gridspec.GridSpecFromSubplotSpec(2, 2, subplot_spec=gs[1])
fig = plt.figure()
ax = fig.add_subplot(gs0[0, 0])
plt.imshow(getpoly(seg1),origin="lower")
ax.set_xticks([]); ax.set_yticks([])
ax = fig.add_subplot(gs0[0, 1])
plt.imshow(getpoly(seg2),origin="lower")
ax.set_xticks([]); ax.set_yticks([])
ax = fig.add_subplot(gs0[0, 2])
plt.imshow(getpoly(seg3),origin="lower")
ax.set_xticks([]); ax.set_yticks([])
ax = fig.add_subplot(gs0[1, 0])
plt.imshow(getpoly(seg4),origin="lower")
ax.set_xticks([]); ax.set_yticks([])
ax = fig.add_subplot(gs0[1, 1])
plt.imshow(getpoly(seg5),origin="lower")
ax.set_xticks([]); ax.set_yticks([])
ax = fig.add_subplot(gs0[1, 2])
plt.imshow(getpoly(seg6),origin="lower")
ax.set_xticks([]); ax.set_yticks([])
ax = fig.add_subplot(gs0[2, 1])
plt.imshow(getpoly(seg7),origin="lower")
ax.set_xticks([]); ax.set_yticks([])
ax = fig.add_subplot(gs0[2, 2])
plt.imshow(getpoly(seg8),origin="lower")
ax.set_xticks([]); ax.set_yticks([])
ax = fig.add_subplot(gs0[2, 0])
plt.imshow(getpoly(seg9),origin="lower")
ax.set_xticks([]); ax.set_yticks([])
ax = fig.add_subplot(gs1[0, 0])
plt.imshow(h1,origin="lower")
plt.colorbar(fraction=0.046, pad=0.04)
ax.set_xticks([]); ax.set_yticks([])
ax = fig.add_subplot(gs1[0, 1])
plt.imshow(h2,origin="lower")
plt.colorbar(fraction=0.046, pad=0.04)
ax.set_xticks([]); ax.set_yticks([])
ax = fig.add_subplot(gs1[1, 0])
plt.imshow(getpoly(h2),origin="lower")
plt.colorbar(fraction=0.046, pad=0.04)
ax.set_xticks([]); ax.set_yticks([])
ax = fig.add_subplot(gs1[1, 1])
plt.imshow(h1-getpoly(h2),origin="lower")
plt.colorbar(fraction=0.046, pad=0.04)
ax.set_xticks([]); ax.set_yticks([])
plt.tight_layout()

You may look at the colorbar-whose-height-or-width-in-sync-with-the-master-axes-example.
The idea is to cut a bit of the axes using mpl_toolkits.axes_grid1.make_axes_locatable and create a new axes into which the colorbar can be placed, using fig.colorbar(im, cax=cax).
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from mpl_toolkits.axes_grid1 import make_axes_locatable
gs = gridspec.GridSpec(1, 2)
gs0 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs[0])
gs1 = gridspec.GridSpecFromSubplotSpec(2, 2, subplot_spec=gs[1])
fig = plt.figure()
for i in range(9):
ax = fig.add_subplot(gs0[i//3, i%3])
ax.imshow(np.random.rand(4,4))
ax.set_xticks([]); ax.set_yticks([])
for i in range(4):
ax = fig.add_subplot(gs1[i//2, i%2])
im = ax.imshow(np.random.rand(4,4))
ax.set_xticks([]); ax.set_yticks([])
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
fig.colorbar(im, ax=ax, cax=cax)
plt.show()

I've found it easier to add the colorbar separately
cax = fig.add_axes([0.125, 0.925, 0.775, 0.0725])
#the numbers in fig.add_axes are all percentages
norm = mpl.colors.Normalize(vmin=low_val, vmax=high_val)
mpl.colorbar.ColorbarBase(cax, cmap='rainbow', norm=norm, orientation='horizontal')
fig.add_axes details

Related

Properly displaying pyplot scatter plot with X/Y histograms and a colorbar

I saw this tutorial on how to make a scatter plot with a histogram for the x and y axes and I thought it would be neat to also tack on a colorbar for an extra dimension of information. To do this, I utilized "the make_axes_locatable" function, like so:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
# generating fake data
tx = np.random.randn(1000)
ty = np.random.randn(1000)
tz = np.random.randn(1000)
fig = plt.figure(figsize=(5, 5))
gs = fig.add_gridspec(2, 2, width_ratios=(4, 1), height_ratios=(1, 4),
left=0.1, right=0.9, bottom=0.1, top=0.9,
wspace=0.05, hspace=0.05)
# Create the Axes.
ax = fig.add_subplot(gs[1, 0])
ax_histx = fig.add_subplot(gs[0, 0], sharex=ax)
ax_histy = fig.add_subplot(gs[1, 1], sharey=ax)
def scatter_hist_and_colorbar(x, y, c, ax, ax_histx, ax_histy,label):
# no labels
ax_histx.tick_params(axis="x", labelbottom=False)
ax_histy.tick_params(axis="y", labelleft=False)
# the scatter plot:
sc=ax.scatter(x,y,marker='o',label=label,c=c)
# now determine nice limits by hand:
binwidth = 0.25
xlim = (int(np.max(np.abs(x))/binwidth) + 1) * binwidth
ylim = (int(np.max(np.abs(y))/binwidth) + 1) * binwidth
xbins = np.arange(-xlim, xlim + binwidth, binwidth)
ybins = np.arange(-ylim, ylim + binwidth, binwidth)
ax_histx.hist(x, bins=xbins)
ax_histy.hist(y, bins=ybins, orientation='horizontal')
return sc
sc1= scatter_hist_and_colorbar(tx,ty,tz, ax, ax_histx, ax_histy,label='data')
ax.set_ylabel('x data')
ax.set_xlabel('y data')
ax.legend()
divider = make_axes_locatable(ax)
cax = divider.append_axes('left', size='5%', pad=1)
cbar=fig.colorbar(sc1, cax=cax, orientation='vertical')
cbar.ax.set_ylabel('z data',rotation=90,labelpad=5)
cbar.ax.yaxis.set_ticks_position("left")
plt.savefig('example.png')
plt.show()][2]][2]
This almost works except the "ax_histx" axis is now stretched and doesn't properly line up due to the addition of the colorbar. Is there a way to resize the "ax_histx" axis or is there a better way to add a colorbar to the "ax" subplot so that it wouldn't affect the "ax_histx" or "ax_histy" axes?
After getting a suggestion form #r-beginners , I tried tweaking this code to place a colorbar in the upper right, perpendicular to the histogram axes. This way, it doesn't distort the width/heights of the other shared axes:
# some random data
tx = np.random.randn(1000)
ty = np.random.randn(1000)
tz = np.random.randn(1000)
fig = plt.figure(figsize=(5, 5))
gs = fig.add_gridspec(2, 2, width_ratios=(4, 1), height_ratios=(1, 4),
left=0.1, right=0.9, bottom=0.1, top=0.9,
wspace=0.05, hspace=0.05)
# Create the Axes.
ax0 = fig.add_subplot(gs[0, 1])
ax = fig.add_subplot(gs[1, 0])
ax_histx = fig.add_subplot(gs[0, 0], sharex=ax)
ax_histy = fig.add_subplot(gs[1, 1], sharey=ax)
def scatter_hist_and_colorbar(x, y, c, ax, ax_histx, ax_histy,label):
# no labels
ax_histx.tick_params(axis="x", labelbottom=False)
ax_histy.tick_params(axis="y", labelleft=False)
# the scatter plot:
sc=ax.scatter(x,y,marker='o',label=label,c=c)
# now determine nice limits by hand:
binwidth = 0.25
xymax = max(np.max(np.abs(x)), np.max(np.abs(y)))
lim = (int(xymax/binwidth) + 1) * binwidth
xlim = (int(np.max(np.abs(x))/binwidth) + 1) * binwidth
ylim = (int(np.max(np.abs(y))/binwidth) + 1) * binwidth
xbins = np.arange(-xlim, xlim + binwidth, binwidth)
ybins = np.arange(-ylim, ylim + binwidth, binwidth)
ax_histx.hist(x, bins=xbins)
ax_histy.hist(y, bins=ybins, orientation='horizontal')
return sc
sc1= scatter_hist_and_colorbar(tx,ty,tz, ax, ax_histx, ax_histy,label='data')
ax.set_ylabel('x data')
ax.set_xlabel('y data')
ax.legend()
divider = make_axes_locatable(ax)
divider = make_axes_locatable(ax0)
ca = divider.append_axes('left', size='50%')
ax0.axis('off')
cbar=fig.colorbar(sc1, cax=ca, orientation='vertical')
cbar.ax.set_ylabel('z data',rotation=270,labelpad=5)
cbar.ax.yaxis.set_ticks_position("right")
gs.tight_layout(fig,pad=1)
plt.savefig('example.png')
plt.show()

Adjust space between two axes while keeping it constant on other axes using matplotlib

For some reason I couldn't find information on this (I'm pretty sure it exists somewhere), but in the following generic example, I would like to reduce the hspace between ax1 and ax2 while keeping the same hspace between ax2-ax3 and ax3-ax4.
I'd also appreciate any links to an example like that!
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
def annotate_axes(fig):
for i, ax in enumerate(fig.axes):
ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
ax.tick_params(labelbottom=False, labelleft=False)
fig = plt.figure()
gs1 = GridSpec(6, 1, hspace=0.2)
ax1 = fig.add_subplot(gs1[0])
ax2 = fig.add_subplot(gs1[1])
ax3 = fig.add_subplot(gs1[2:4])
ax4 = fig.add_subplot(gs1[4:6])
annotate_axes(fig)
plt.show()
One way that might suit your need is to create a subgrid (in this example, putting hspace to 0):
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
def annotate_axes(fig):
for i, ax in enumerate(fig.axes):
ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
ax.tick_params(labelbottom=False, labelleft=False)
fig = plt.figure()
gs1 = GridSpec(6, 1, hspace=0.2)
# subgrid for the first two slots
# in this example with no space
subg = gs1[0:2].subgridspec(2, 1, hspace = 0)
# note the ax1 and ax2 being created from the subgrid
ax1 = fig.add_subplot(subg[0])
ax2 = fig.add_subplot(subg[1])
ax3 = fig.add_subplot(gs1[2:4])
ax4 = fig.add_subplot(gs1[4:6])
annotate_axes(fig)
plt.show()

Matplotlib - figure legend doesn't work with multiple subplots with multiple axes

f = plt.figure(figsize=(20, 10))
gs = gridspec.GridSpec(4, 2)
ax0 = plt.subplot(gs[0, 0])
ax1 = plt.subplot(gs[0, 1])
ax2 = plt.subplot(gs[1, 1])
ax3 = plt.subplot(gs[1, 0])
ax4 = plt.subplot(gs[2, 0])
ax5 = plt.subplot(gs[2, 1])
ax6 = plt.subplot(gs[3, 1])
ax7 = plt.subplot(gs[3, 0])
axes = [ax0, ax1, ax2, ax3, ax4, ax5, ax6, ax7]
axes_twin = [ax.twinx() for ax in axes]
for param_set in results.keys():
for i, (ax, ax_twin) in enumerate(zip(axes, axes_twin)):
t_test_plot = np.random.uniform(low=0.0, high=1.0, size=100)
i_test_plot = np.random.uniform(low=0.0, high=1000, size=100)
d_test_plot = np.random.uniform(low=0.0, high=10, size=100)
ax.plot(t_test_plot, i_test_plot, color='lightgrey')
temp = ax_twin.plot(t_test_plot, d_test_plot, label=keys)
The above code will generate a plot that looks like this:
I searched on stackoverflow, and got this code to do figure legend:
handles, labels = ax_twin.get_legend_handles_labels()
fig.legend(handles, labels, loc='upper center')
But this had no effect.
Here's what I want. I took a screenshot of my plot, and generated a legend from a different plot and photoshopped them:
How can I do this?

How to delete extra plots on a AxesSubplot object?

I have a AxesSubplot object ax1 from this:
fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)
I plot multiple times on this ax1 to see how alpha values will set the plots' appearance:
first = ax1.hist(np.random.randn(100), bins=20, color='k', alpha=0.3)
second = ax1.hist(np.random.randn(100), bins=20, color='k', alpha=0.6)
third = ax1.hist(np.random.randn(100), bins=20, color='k', alpha=0.9)
But these three plots overlap each other:
How can I erase the former histogram , then only show one plot each time? And by the way, what does the alpha arg do?
Thanks. :)
If I understand what you want,then try this
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
Output
first = ax1.hist(np.random.randn(100), bins=20, color='k', alpha=0.3)
second = ax2.hist(np.random.randn(100), bins=20, color='k', alpha=0.6)
third = ax3.hist(np.random.randn(100), bins=20, color='k', alpha=0.9)
plt.show()

Set equal aspect in plot with colorbar

I need to generate a plot with equal aspect in both axis and a colorbar to the right. I've tried setting aspect='auto', aspect=1, and aspect='equal' with no good results. See below for examples and the MWE.
Using aspect='auto' the colorbars are of the correct height but the plots are distorted:
Using aspect=1 or aspect='equal' the plots are square (equal aspect in both axis) but the colorbars are distorted:
In both plots the colorbars are positioned too far to the right for some reason. How can I get a square plot with colorbars of matching heights?
MWE
import numpy as np
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
def col_plot(params):
gs, i, data = params
xarr, yarr, zarr = zip(*data)[0], zip(*data)[1], zip(*data)[2]
xmin, xmax = min(xarr), max(xarr)
ymin, ymax = min(yarr), max(yarr)
#plt.subplot(gs[i], aspect='auto')
plt.subplot(gs[i], aspect=1)
#plt.subplot(gs[i], aspect='equal')
plt.xlim(xmin, xmax)
plt.ylim(xmin, xmax)
plt.xlabel('$x axis$', fontsize=20)
plt.ylabel('$y axis$', fontsize=20)
# Scatter plot.
cm = plt.cm.get_cmap('RdYlBu_r')
SC = plt.scatter(xarr, yarr, marker='o', c=zarr, s=60, lw=0.25, cmap=cm,
zorder=3)
# Colorbar.
ax0 = plt.subplot(gs[i + 1])
cbar = plt.colorbar(SC, cax=ax0)
cbar.set_label('$col bar$', fontsize=21, labelpad=-2)
# Generate data.
data0 = np.random.uniform(0., 1., size=(50, 3))
data1 = np.random.uniform(0., 1., size=(50, 3))
# Create the top-level container
fig = plt.figure(figsize=(14, 25))
gs = gridspec.GridSpec(4, 4, width_ratios=[1, 0.05, 1, 0.05])
# Generate plots.
par_lst = [[gs, 0, data0], [gs, 2, data1]]
for pl_params in par_lst:
col_plot(pl_params)
# Output png file.
fig.tight_layout()
plt.savefig('colorbar_aspect.png', dpi=300)
You can use an AxesDivider to do that. I have modified your code a bit to make use of an AxesDivider.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
def col_plot(data):
xarr, yarr, zarr = zip(*data)[0], zip(*data)[1], zip(*data)[2]
xarr = [2*x for x in xarr]
xmin, xmax = min(xarr), max(xarr)
ymin, ymax = min(yarr), max(yarr)
fig = plt.figure()
ax0 = fig.add_subplot(111, aspect='equal')
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
plt.xlabel('$x axis$', fontsize=20)
plt.ylabel('$y axis$', fontsize=20)
# Scatter plot.
cm = plt.cm.get_cmap('RdYlBu_r')
SC = ax0.scatter(xarr, yarr, marker='o', c=zarr, s=60, lw=0.25, cmap=cm,
zorder=3)
the_divider = make_axes_locatable(ax0)
color_axis = the_divider.append_axes("right", size="5%", pad=0.1)
# Colorbar.
cbar = plt.colorbar(SC, cax=color_axis)
cbar.set_label('$col bar$', fontsize=21, labelpad=-2)
# Generate data.
data0 = np.random.uniform(0., 1., size=(20, 3))
col_plot(data0)
And here is the result (I changed your data so it spans a range of [0, 2] in the x-direction for demonstration purposes):
On Joseph Long's blog there is the following nice solution.
1) Define a colorbar function as:
from mpl_toolkits.axes_grid1 import make_axes_locatable
def colorbar(mappable):
ax = mappable.axes
fig = ax.figure
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
return fig.colorbar(mappable, cax=cax)
2) Call colorbar(thing) when you want to make a colorbar. In your case:
SC = ax0.scatter(xarr, yarr, marker='o', c=zarr, s=60, lw=0.25, cmap=cm,
zorder=3)
colorbar(SC)
3) And you get:

Categories

Resources