Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have each plot in matplotlib's `subplots` use a different axes?

So when I try to graph multiple subplots using pyplot.subplots I get something like:

Four subplots

How can I have:

  1. Multiple independent axes for every subplot
  2. Axes for every subplot
  3. Overlay plots in every subplot axes using subplots. I tried to do ((ax1,ax2),(ax3,ax4)) = subplots and then do ax1.plot twice, but as a result, neither of the two showed.

Code for the picture:

import string
import matplotlib
matplotlib.use('WX')

import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import numpy as np
from itertools import izip,chain


f,((ax1,ax2),(ax3,ax4)) = plt.subplots(2,2,sharex='col',sharey='row')

ax1.plot(range(10),2*np.arange(10))
ax2.plot(range(10),range(10))
ax3.plot(range(5),np.arange(5)*1000)
#pyplot.yscale('log')
#ax2.set_autoscaley_on(False)
#ax2.set_ylim([0,10])


plt.show()
like image 642
Eiyrioü von Kauyf Avatar asked May 17 '13 14:05

Eiyrioü von Kauyf


People also ask

How to create subplots/multiplots in Matplotlib?

In matplotlib, we can do this using Subplots. The subplots () function is in the pyplot module of the matplotlib library and is used for creating subplots/multiplots in an effective manner. This function will create a figure and a set of subplots.

How do I plot subplots with different axes?

You should create your subplots first, then twin the axes for each subplot. It is easier to use the methods contained in the axis object to do the plotting, rather than the high level plot function calls. The axes returned by subplots is an array of axes.

How to use the subplots () function to create a single graph/plot?

So the subplots () function can be normally used to create a single graph/plot as well. Let us cover an example for stacked plots. We will try to plot a sequence of plots (i.e 2 here) just by stacking one over the other. As there is the stacking of plots, so the number of rows (i.e nrows) will change, which means ncols stay the same as 1.

How to create a single figure and axes in Pyplot?

This is actually the simplest and recommended way of creating a single Figure and Axes. The first two optional arguments of pyplot.subplots define the number of rows and columns of the subplot grid. When stacking in one direction only, the returned axs is a 1D numpy array containing the list of created Axes.


1 Answers

Questions 1 & 2:

To accomplish this, explicitly set the subplots options sharex and sharey=False.

replace this line in the code for the desired results.

f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=False, sharey=False)

Alternatively, those two options can be omitted altogether, as False is the default. (as noted by rubenvb below)

Question 3:

Here are two examples of adding secondary plots to two of the subplots:

(add this snippet before plt.show())

# add an additional line to the lower left subplot
ax3.plot(range(5), -1*np.arange(5)*1000)

# add a bar chart to the upper right subplot
width = 0.75       # the width of the bars
x = np.arange(2, 10, 2)
y = [3, 7, 2, 9]

rects1 = ax2.bar(x, y, width, color='r')

Subplots with independent axes, and "multiple" plots

like image 97
Drew Avatar answered Sep 21 '22 12:09

Drew