Typically, you simply pass an instance of the axes of the function.
For example:
import matplotlib.pyplot as plt import numpy as np def main(): x = np.linspace(0, 6 * np.pi, 100) fig1, (ax1, ax2) = plt.subplots(nrows=2) plot(x, np.sin(x), ax1) plot(x, np.random.random(100), ax2) fig2 = plt.figure() plot(x, np.cos(x)) plt.show() def plot(x, y, ax=None): if ax is None: ax = plt.gca() line, = ax.plot(x, y, 'go') ax.set_ylabel('Yabba dabba do!') return line if __name__ == '__main__': main()
To answer your question, you can always do something like this:
def subplot(data, fig=None, index=111): if fig is None: fig = plt.figure() ax = fig.add_subplot(index) ax.plot(data)
Alternatively, you can simply add an instance of the axes to another drawing:
import matplotlib.pyplot as plt fig1, ax = plt.subplots() ax.plot(range(10)) fig2 = plt.figure() fig2.axes.append(ax) plt.show()
Resizing it to fit the other βshapesβ of the subtitle is also possible, but it will quickly become more problematic than worth it. The approach of simply passing an instance of a figure or axes (or a list of instances) is much simpler for complex cases, in my experience ...
Joe Kington Jun 10 2018-11-11T00: 00Z
source share