Reputation: 4725
So when I try to graph multiple subplots using pyplot.subplots
I get something like:
How can I have:
((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()
Upvotes: 5
Views: 11181
Reputation: 6639
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')
Upvotes: 7