Issue
I want to create a plot consisting of several subplots with shared x/y axes. It should look something like this from the documentation (though my subplots will be scatterblots): (code here)
But I want to create the subplots dynamically!
So the number of subplots depends on the output of a previous function. (It will probably be around 3 to 15 subplots per diagram, each from a distinct dataset, depending on the input of my script.)
Can anyone tell me how to accomplish that?
Solution
import matplotlib.pyplot as plt
from pylab import *
import numpy as np
x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)
subplots_adjust(hspace=0.000)
number_of_subplots=3
for i,v in enumerate(xrange(number_of_subplots)):
v = v+1
ax1 = subplot(number_of_subplots,1,v)
ax1.plot(x,y)
plt.show()
This code works but you will need to correct the axes. I used to subplot
to plot 3 graphs all in the same column. All you need to do is assign an integer to number_of_plots
variable. If the X and Y values are different for each plot you will need to assign them for each plot.
subplot
works as follows, if for example I had a subplot values of 3,1,1
. This creates a 3x1 grid and places the plot in the 1st position. In the next interation if my subplot
values were 3,1,2
it again creates a 3x1 grid but places the plot in the 2nd position and so forth.
Answered By - Harpal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.