Questions tagged [subplot]

subplot is a Matlab / Matplotlib function which divides the current figure into a grid. Each block in the grid can contain an axes object which can be manipulated and used for plotting.

Plotly

For information about plotly::subplot look at the Subplot in R/JavaScript/Python.

Matlab

subplot() in Matlab is a function which allows displaying several axes elements in the same figure, side by side. It divides the current figure into a grid that is numbered row-wise. Each block in the grid can contain an axes object which can be manipulated using Axes Properties. Subsequent plots are output to the current axes.

For more information, see Matlab's official doc.

Matplotlib

In Matplotlib the most straightforward way to use the subplot functionality is probably with the pyplot.subplots() function, which already returns a Figure object and a numpy.ndarray object representing the subplot grid with nrows and ncols.

Another way to create subplots is to use gridspec. Gridspec specifies the geometry of the subplots' grid with the number of rows and columns. Gripspec is more flexible instrument to work with subplots than the standard subplots() function.

1972 questions
632
votes
6 answers

How do I change the figure size with subplots?

How do I increase the figure size for this figure? This does nothing: f.figsize(15, 15) Example code from the link: import matplotlib.pyplot as plt import numpy as np # Simple data to display in various forms x = np.linspace(0, 2 * np.pi, 400) y =…
Brian
  • 13,996
  • 19
  • 70
  • 94
584
votes
7 answers

What does the argument mean in fig.add_subplot(111)?

Sometimes I come across code such as this: import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [1, 4, 9, 16, 25] fig = plt.figure() fig.add_subplot(111) plt.scatter(x, y) plt.show() Which produces: I've been reading the documentation like…
pleasedontbelong
  • 19,542
  • 12
  • 53
  • 77
536
votes
9 answers

Improve subplot size/spacing with many subplots

I need to generate a whole bunch of vertically-stacked plots in matplotlib. The result will be saved using savefig and viewed on a webpage, so I don't care how tall the final image is, as long as the subplots are spaced so they don't overlap. No…
mcstrother
  • 6,867
  • 5
  • 22
  • 18
446
votes
10 answers

How to add a title to each subplot

I have one figure which contains many subplots. fig = plt.figure(num=None, figsize=(26, 12), dpi=80, facecolor='w', edgecolor='k') fig.canvas.set_window_title('Window Title') # Returns the Axes instance ax = fig.add_subplot(311) ax2 =…
Shailen
  • 7,909
  • 3
  • 29
  • 37
394
votes
6 answers

Matplotlib different size subplots

I need to add two subplots to a figure. One subplot needs to be about three times as wide as the second (same height). I accomplished this using GridSpec and the colspan argument but I would like to do this using figure so I can save to PDF. I can…
Jason Strimpel
  • 14,670
  • 21
  • 76
  • 106
349
votes
3 answers

How to set a single, main title above all the subplots

I am using pyplot. I have 4 subplots. How to set a single, main title above all the subplots? title() sets it above the last subplot.
Jakub M.
  • 32,471
  • 48
  • 110
  • 179
343
votes
13 answers

How to have one colorbar for all subplots

I've spent entirely too long researching how to get two subplots to share the same y-axis with a single colorbar shared between the two in Matplotlib. What was happening was that when I called the colorbar() function in either subplot1 or subplot2,…
astromax
  • 6,001
  • 10
  • 36
  • 47
292
votes
6 answers

How to set the subplot axis range

How can I set the y axis range of the second subplot to e.g. [0,1000] ? The FFT plot of my data (a column in a text file) results in a (inf.?) spike so that the actual data is not visible. pylab.ylim([0,1000]) has no effect, unfortunately. This is…
someone
  • 5,979
  • 3
  • 17
  • 6
282
votes
9 answers

How to set common axes labels for subplots

I have the following plot: import matplotlib.pyplot as plt fig2 = plt.figure() ax3 = fig2.add_subplot(2,1,1) ax4 = fig2.add_subplot(2,1,2) ax4.loglog(x1, y1) ax3.loglog(x2, y2) ax3.set_ylabel('hello') I want to be able to create axes labels and…
farqwag25
  • 2,821
  • 3
  • 15
  • 3
253
votes
1 answer

How to set xlim and ylim for a subplot

I would like to limit the X and Y axis in matplotlib for a specific subplot. The subplot figure itself doesn't have any axis property. I want for example to change only the limits for the second plot: import matplotlib.pyplot as…
Cupitor
  • 11,007
  • 19
  • 65
  • 91
246
votes
13 answers

How to plot in multiple subplots

I am a little confused about how this code works: fig, axes = plt.subplots(nrows=2, ncols=2) plt.show() How does the fig, axes work in this case? What does it do? Also why wouldn't this work to do the same thing: fig = plt.figure() axes =…
bugsyb
  • 5,662
  • 7
  • 31
  • 47
228
votes
10 answers

How to plot multiple dataframes in subplots

I have a few Pandas DataFrames sharing the same value scale, but having different columns and indices. When invoking df.plot(), I get separate plot images. what I really want is to have them all in the same plot as subplots, but I'm unfortunately…
Jimmy C
  • 9,270
  • 11
  • 44
  • 64
167
votes
4 answers

Matplotlib - global legend and title aside subplots

I've started with matplot and managed some basic plots, but now I find it hard to discover how to do some stuff I need now :( My actual question is how to place a global title and global legend on a figure with subplots. I'm doing 2x3 subplots where…
Gere
  • 12,075
  • 18
  • 62
  • 94
165
votes
5 answers

How to make two plots side-by-side

I found the following example on matplotlib: import numpy as np import matplotlib.pyplot as plt x1 = np.linspace(0.0, 5.0) x2 = np.linspace(0.0, 2.0) y1 = np.cos(2 * np.pi * x1) * np.exp(-x1) y2 = np.cos(2 * np.pi * x2) plt.subplot(2, 1,…
Heißenberg93
  • 1,829
  • 3
  • 11
  • 10
131
votes
4 answers

Row and column headers in matplotlib's subplots

What's the best practise to add a row and a column header to a grid of subplots generated in a loop in matplotlib? I can think of a couple, but not particularly neat: For columns, with a counter to your loop you can use set_title() for the first…
gozzilli
  • 8,089
  • 11
  • 56
  • 87
1
2 3
99 100