My goal is to create three subplots in same window. Those subplots should be aligned in one column, each one under another. The first subplot should not use the slider widget, other two should, so if I will move slider then two bottom subplots should scroll. I've ended up with following two codes. I've modified a bit the code from answer to my another question. The tricky part is plotting part, which was mainly created as trial and error:
#!/usr/bin/python import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider fig, ax = plt.subplots(nrows=2, ncols=1, sharex=True, sharey=False) # 1. On which (sub)plots this option is apply to # (already existed or also for subplots created in future)? plt.subplots_adjust(bottom=0.25) x_time = np.arange(0.0, 100.0, 0.1) sin_s = np.sin(x_time) cos_s = np.cos(x_time) oscil = np.cos(2 * np.pi * x_time) * np.exp(-x_time) sin_l, = plt.plot(x_time, sin_s, "bo-", label="sin") cos_l, = plt.plot(x_time, cos_s, "r.-", label="cos") # 2. On which plots this option is apply to # (already existed or also for subplots created in future)? plt.axis([0, 10, -1, 1]) #################################### # HERE COMES THE TRICKY PART WITH # # TWO ALTERNATIVES - THE RESULT OF # # TRIAL AND ERROR. PLEASE SEE CODE # # SNIPPETS BELLOW # #################################### axcolor = 'lightgoldenrodyellow' axpos = plt.axes([0.2, 0.1, 0.65, 0.03], axisbg=axcolor) spos = Slider(axpos, 'Pos', 0.1, 90.0) def update(val): pos = spos.val # 3. No matter which index is chosen (0 or 1), # it behaves exactly the same in both alternatives. # How is this possible? ax[0].axis([pos,pos+10,-1,1]) # ax[1].axis([pos,pos+10,-1,1]) fig.canvas.draw_idle() spos.on_changed(update) plt.show() Here are two different parts of code that I use for plotting, this is the trial and error part of code that I do not understand:
# 1st alternative - this plot is scrollable: ax[0].plot(x_time, oscil, "k-", label="oscilation") ax[0].legend() # 2nd alternative - this plot is not scrollable: plt.subplot(211) plt.plot(x_time, oscil, "k-", label="oscilation") plt.title('A tale of 2 subplots') plt.ylabel('Damped oscillation') I've tried to combine those examples into my final solution (having three plots from which the two are scrollable). So I've changed nrows=2 to nrows=3 in following line:
fig, ax = plt.subplots(nrows=2, ncols=1, sharex=True, sharey=False)
But if I use 1st alternative then all three subplots are scrollable, instead of two. And if I use 2nd alternative then I get just two subplots instead of three, where one is scrollable and another is not.
Can you please answer following questions:
- comment regarding:
plt.subplots_adjust(bottom=0.25) - comment regarding:
plt.axis([0, 10, -1, 1]) - comment regarding:
ax[0].axis([pos,pos+10,-1,1]) vs ax[1].axis([pos,pos+10,-1,1]) - The most important: how to make it work for three subplots?
- And finally the difference between 1st and 2nd alternative?
Edit, working version:
#!/usr/bin/python import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider plt.close('all') fig, ax = plt.subplots(nrows=3, ncols=1) plt.subplots_adjust(bottom=0.25) ######################################## x_time = np.arange(0.0, 100.0, 0.1) oscil = np.cos(2 * np.pi * x_time) * np.exp(-x_time) sin_s = 2*np.sin(x_time) cos_s = np.cos(x_time) ######################################## ax[0].plot(x_time, oscil, "k-", label="oscilation") ax[1].plot(x_time, sin_s, "bo-", label="sin") ax[2].plot(x_time, cos_s, "r.-", label="cos") ######################################## ax[0].set_xlim([0, 10]) ax[1].set_ylim([-2, 2]) ax[2].set_ylim([-1, 1]) ######################################## axcolor = 'lightgoldenrodyellow' axpos = plt.axes([0.2, 0.1, 0.65, 0.03], axisbg=axcolor) spos = Slider(axpos, 'Pos', 0.1, 90.0) def update(val): pos = spos.val ax[1].axis([pos,pos+10,-2,2]) ax[2].axis([pos,pos+10,-1,1]) fig.canvas.draw_idle() spos.on_changed(update) plt.show()