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

2

5

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()
Outstation answered 17/5, 2013 at 14:2 Comment(2)
What version of matplotlib are you using? What do you mean by "multiple independent axes for every subplot"? Also, with "Axes along every subplot" do you mean that you want to have independent axes (i.e. not sharing x- or y-axis with other subplots) for every subplot, where each subplot has ticklabels? What do you mean by "overlaying plots in every subplot axes using subplots"? Please be specific, and make sure it's easily understandable what you want to achieve. If you have any images etc. of what you want it to look like, please refer to them.Storyteller
did you get this sorted out?Contingence
I
7

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

Indigestion answered 8/1, 2014 at 7:5 Comment(1)
False is the default. The OP is setting them explicitly.Ay
C
0

Don't tell it to share axes:

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

ax1.plot(range(10),2*np.arange(10))
ax2.plot(range(10),range(10))
ax3.plot(range(5),np.arange(5)*1000)

doc

Contingence answered 17/5, 2013 at 14:17 Comment(5)
Is that something WX-backend dependent? With the axes only partially shared with sharex='col',sharey='row' it does work for me. See: nbviewer.ipython.org/5599482 Or perhaps the code and the screenshot in the OP dont match.Tag
:/ How can I prove the code and screenshot go together? It looks like that for me: i.imgur.com/p4Kftwa.pngAlow
Also how would I overlay plots in shared axes?Alow
what do you mean by overlay? Show more than one plot in a given axes?Contingence
ex 4 plots that all share x,y axes, and THEN overlay a bar graph on top of one of them that uses the same/ different axes to the 'right' of the graph?Alow

© 2022 - 2024 — McMap. All rights reserved.