2

I have two subplots that share the x-axes. The first one has data and a fit function, in the second one is the difference between the data and the fit function. In the figure both subplots have the same y axis size (in pixels). Now i want the y axis of the data and the fit to be bigger than the axis of the errors. my code is the following:

import matplotlib.pyplot as plt
f, axarr = plt.subplots(2, sharex=True,figsize=(15, 12))
axarr[0].scatter(x, data , facecolors='none', edgecolors='crimson')
axarr[0].plot(x, fit, color='g',linewidth=1.5)
axarr[0].set_ylim([18,10])
axarr[1].plot(x,data-fit,color='k',linewidth=width)
axarr[1].set_ylim([-0.4,0.4])
yticks[-1].label1.set_visible(False)
plt.subplots_adjust(hspace=0.)

is there any code that sets the size of the second plot?

M.S.
  • 23
  • 1
  • 3

1 Answers1

1

Take a look at this example, using gridspec. I believe it is exactly what you want. Below is the example adopted for your case. Edited to also share the x-axis

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec

# generate some data
x = np.arange(0, 10, 0.2)
y = np.sin(x)

# plot it
fig = plt.figure(figsize=(8, 6))
gs = gridspec.GridSpec(2, 1, height_ratios=[3, 1])
ax0 = plt.subplot(gs[0])
ax1 = plt.subplot(gs[1], sharex=ax0)  # <---- sharex=ax0 will share ax1 with ax2
ax0.plot(x, y)
ax1.plot(y, x)

plt.show()

Or even simpler by following Hagnes answer in the first link:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 10, 0.2)
y = np.sin(x)

f, (a0, a1) = plt.subplots(2,1, gridspec_kw = {'height_ratios':[1, 3]}, sharex=True)  # <---- sharex=True will share the xaxis between the two axes
a0.plot(x, y)
a1.plot(y, x)
plt.show()

enter image description here

Community
  • 1
  • 1
pathoren
  • 1,634
  • 2
  • 14
  • 22
  • First of all thank you for your answer! Yes, i do know this, but the two plots do not share the x-axis anymore. I wonder if there is any way to obtain the 'sharex' option and set the size of the plots. – M.S. Aug 11 '16 at 15:42
  • You are welcome! See my updated answer, where I also share the x-axis. I also added a simpler code snippet which does not need `GridSpec`. – pathoren Aug 11 '16 at 16:35
  • perfect! this is exactly what i was looking for. – M.S. Aug 11 '16 at 16:42
  • I would, but i just get the error TypeError: __init__() got an unexpected keyword argument 'gridspec_kw' – M.S. Aug 11 '16 at 17:03
  • What matplotlib version do you have? – pathoren Aug 11 '16 at 17:11
  • To be honest, i dont know, and i dont know how to check it either. Could you please tell me, how to update my version? Just a short terminal command would be great :) – M.S. Aug 11 '16 at 17:15
  • Use `import matplotlib` and then `print matplotlib.__version__`. – pathoren Aug 11 '16 at 17:30
  • it is version 1.3.1 – M.S. Aug 11 '16 at 17:35
  • Try to update matplotlib then. How did you install it python and matplotlib? With anaconda? pip? – pathoren Aug 11 '16 at 17:38
  • it is a bit complicated, it is not my computer, tomorrow i will ask my supervisor to update it for me. thank you though. – M.S. Aug 11 '16 at 17:44
  • Ok, do that. I think the latest version is 1.5, but I have 1.4 and it works with that. However if the GridSpec-version of my code is working i would go for that. – pathoren Aug 11 '16 at 17:48