I have tried and tried to search for a solution, but I can't seem to make it work, so here is my problem:
I am trying to make a script, where I can set a size (i.e. number of subplots in a figure) of the figure. It should always be a N*2 grid structure (with equal dimensions) for the subplots in the figure. In the grids, I want to plot some lines (with a specific color) and add a horizontal colorbar at the bottom of the figure. I have tried following the solution to the problem, mentioned in Matplotlib 2 Subplots, 1 Colorbar. Here is my solution:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import matplotlib.colors as colors
import matplotlib.cm as cmx
import random
lines = np.random.rand(100, 10)
fig, ax = plt.subplots(nrows=2,ncols=2, sharex=True)
ranks = np.linspace(0,len(lines[0,:]), len(lines[0,:]))
norm = matplotlib.colors.Normalize(
vmin=np.min(ranks),
vmax=np.max(ranks))
c_m = matplotlib.cm.cool
s_m = matplotlib.cm.ScalarMappable(cmap=c_m, norm=norm)
s_m.set_array([])
for line, rank in zip(xrange(0,len(lines[0,:])), ranks):
try:
no = random.randint(0,1)
no2 =random.randint(0,1)
im =ax[no,no2].plot(lines[:, line], c=s_m.to_rgba(rank))
except IOError as error:
print error
fig.subplots_adjust(bottom=0.2)
fig.colorbar(s_m, orientation='horizontal')
plt.show()
So my question is: How do I make a gridbased subplot (for line- and scatterplots), where I can add one colorbar horizontally at the bottom to stretch across both subplots in the bottom?