Note Click here to download the full example code A figure with just one subplot#
This is actually the simplest and recommended way of creating a single Figure and Axes. Text(0.5, 1.0, 'A single plot') Stacking subplots in one direction#The first two optional arguments of
When stacking in one direction only, the returned [<matplotlib.lines.Line2D object at 0x7f2d00efd510>] If you are creating just a few Axes, it's handy to unpack them immediately to dedicated variables for each Axes. That way, we can use [<matplotlib.lines.Line2D object at 0x7f2d00a95b70>] To obtain side-by-side subplots, pass parameters [<matplotlib.lines.Line2D object at 0x7f2cfb43d330>] Stacking subplots in two directions#When stacking in two directions, the returned If you have to set parameters for each subplot it's handy to iterate over all subplots in a 2D grid using fig, axs = plt.subplots(2, 2) axs[0, 0].plot(x, y) axs[0, 0].set_title('Axis [0, 0]') axs[0, 1].plot(x, y, 'tab:orange') axs[0, 1].set_title('Axis [0, 1]') axs[1, 0].plot(x, -y, 'tab:green') axs[1, 0].set_title('Axis [1, 0]') axs[1, 1].plot(x, -y, 'tab:red') axs[1, 1].set_title('Axis [1, 1]') for ax in axs.flat: ax.set(xlabel='x-label', ylabel='y-label') # Hide x labels and tick labels for top plots and y ticks for right plots. for ax in axs.flat: ax.label_outer() You can use tuple-unpacking also in 2D to assign all subplots to dedicated variables: fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2) fig.suptitle('Sharing x per column, y per row') ax1.plot(x, y) ax2.plot(x, y**2, 'tab:orange') ax3.plot(x, -y, 'tab:green') ax4.plot(x, -y**2, 'tab:red') for ax in fig.get_axes(): ax.label_outer() Sharing axes#By default, each Axes is scaled individually. Thus, if the ranges are different the tick values of the subplots do not align. [<matplotlib.lines.Line2D object at 0x7f2cfb007250>] You can use sharex or sharey to align the horizontal or vertical axis. [<matplotlib.lines.Line2D object at 0x7f2cfa993370>] Setting sharex or sharey to [<matplotlib.lines.Line2D object at 0x7f2cfab12980>] For subplots that are sharing axes one set of tick labels is enough. Tick labels of inner Axes are automatically removed by sharex and sharey. Still there remains an unused empty space between the subplots. To precisely control the positioning of the subplots, one can explicitly create a
Apart from fig = plt.figure() gs = fig.add_gridspec(2, 2, hspace=0, wspace=0) (ax1, ax2), (ax3, ax4) = gs.subplots(sharex='col', sharey='row') fig.suptitle('Sharing x per column, y per row') ax1.plot(x, y) ax2.plot(x, y**2, 'tab:orange') ax3.plot(x + 1, -y, 'tab:green') ax4.plot(x + 2, -y**2, 'tab:red') for ax in axs.flat: ax.label_outer() If you want a more complex sharing structure, you can first create the grid of axes with no sharing, and then call
fig, axs = plt.subplots(2, 2) axs[0, 0].plot(x, y) axs[0, 0].set_title("main") axs[1, 0].plot(x, y**2) axs[1, 0].set_title("shares x with main") axs[1, 0].sharex(axs[0, 0]) axs[0, 1].plot(x + 1, y + 1) axs[0, 1].set_title("unrelated") axs[1, 1].plot(x + 2, y + 2) axs[1, 1].set_title("also unrelated") fig.tight_layout() Polar axes#The parameter subplot_kw of Total running time of the script: ( 0 minutes 7.774 seconds) Gallery generated by Sphinx-Gallery How do I show multiple graphs in Matplotlib?To draw multiple plots using the subplot() function from the pyplot module, you need to perform two steps: First, you need to call the subplot() function with three parameters: (1) the number of rows for your grid, (2) the number of columns for your grid, and (3) the location or axis for plotting.
How do you plot two graphs in Python?Create x, y1 and y2 data points using numpy.. Add a subplot to the current figure at index 1.. Plot curve 1 using x and y1.. Add a subplot to the current figure at index 2.. Plot curve 2 using x and y2.. To display the figure, use show() method.. How do you plot two graphs side by side in Python?Creating x, y1, y2 points using numpy.. With nrows = 1, ncols = 2, index = 1, add subplot to the current figure, using the subplot() method.. Plot the line using x and y1 points, using the plot() method.. Set up the title, label for X and Y axes for Figure 1, using plt.. How do you plot multiple graphs on different figures in Python?Create a new figure, or activate an existing figure, with the window title “Welcome to figure 1”.. Draw a line using plot() method, over the current figure.. Create a new figure, or activate an existing figure, with the window title “Welcome to figure 2”.. Draw a line using plot() method, over the current figure.. |