Hi,
I want to have two plots above each other with shared y-axis. For this I
am using gridspec to adjust the vertical distance between the graphs.
Attached is a .pdf with the current results. Two problems:
1) I would like to reduce the top margin. For that I though I can do
something like this:
gs = gridspec.GridSpec(2, 1, width_ratios=[2,1])
- gs.update(bottom=0.2, left=0.2, hspace=0.05)
+ gs.update(bottom=0.2, left=0.2, hspace=0.05, top=0.1)
But I get an error ""ValueError: bottom cannot be >= top"". This
limitation is not clear to me, why is this so?
2) I want to make a shared y-axis label. I found this page:
http://www.scipy.org/Cookbook/Matplotlib/Multiple_Subplots_with_One_Axis_Label
But any additional axis I put before the gridspec axis is not shown in
the end. Is there a special procedure that can be used together with
gridspec?
Below the code I use for the figure:
line_profiles.pdf (184 KB)
···
################################
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
# Import data into numpy ndarray
tung = np.loadtxt('tungsten_profiles_leveled.txt')
insu = np.loadtxt('insulator_profiles_leveled.txt')
fig_width_pt = 270
fig_height_pt = 220
inches_per_pt = 1.0/72.27 # Convert pt to inches
fig_width = fig_width_pt*inches_per_pt # width in inches
fig_height =fig_height_pt*inches_per_pt # height in inches
fig_size = [fig_width,fig_height]
# Settings for fitting LaTeX style
plt.rc('font',**{'family':'serif','serif':['Computer Modern Roman']})
params = {'backend': 'ps',
'text.latex.preamble': [r"\usepackage{upgreek}"],
'axes.labelsize': 10,
'text.fontsize': 10,
'legend.fontsize': 8,
'xtick.labelsize': 8,
'ytick.labelsize': 8,
'text.usetex': True,
'figure.figsize': fig_size,
'axes.unicode_minus': True}
plt.rcParams.update(params)
#######
plt.figure(1)
plt.clf()
gs = gridspec.GridSpec(2, 1, width_ratios=[2,1])
gs.update(bottom=0.2, left=0.2, hspace=0.05, top=0.1)
ax1 = plt.subplot(gs[0])
plt.ylabel(r'Height (nm)', labelpad = 12)
for i in range(len(tung[:][0])/2):
ax1.plot(tung[:,i*2]*1e6-0.3, tung[:,1+i*2]*1e9,linewidth=1)
plt.ylim(-1,11)
plt.yticks([0, 2, 4, 6, 8, 10])
leg = ax1.legend(('Profile 1', 'Profile 2', 'Profile 3', 'Profile 4',
'Profile 5', 'Profile 6', 'Profile 7'),
shadow=True, loc = (1.1,-0.5))
plt.setp(ax1.get_xticklabels(), visible=False)
ax1.annotate(r'\bf Tungsten', xy=(0,10),
xycoords='data', # <-- check webpage for different
coordinate systems
horizontalalignment='left', verticalalignment='top',
fontsize=10)
ax2 = plt.subplot(gs[1], sharex = ax1)
for i in range(len(insu[:][0])/2):
ax2.plot(insu[:,i*2]*1e6-0.3, insu[:,1+i*2]*1e9,linewidth=1)
plt.xlim(-0.1,2.1)
plt.ylim(-1,11)
plt.yticks([0, 2, 4, 6, 8, 10])
plt.xlabel(r'Position ($\upmu$m)', labelpad = 12)
plt.ylabel(r'Height (nm)', labelpad = 12) # r for raw
ax2.annotate(r'\bf Insulator', xy=(0,10),
xycoords='data', # <-- check webpage for different
coordinate systems
horizontalalignment='left', verticalalignment='top',
fontsize=10)
#plt.show()
plt.savefig('line_profiles.pdf')