aitatanit
aitatanit

Reputation: 60

make subplots have the same x range

I'm trying to plot several hundred subplots into one figure (i can split it into a grid and make it span several PDF pages) but the process is slow if I use the standard matplotlib subplots and add one subplot at a time. A solution that may apply to my problem here. The issue is that my data has different x range for each subplot. How can I make all the x values have the same width, say 1 inch? Here is an example that itustrates the problem.

import numpy as np
import matplotlib.pyplot as plt

ax = plt.subplot(111)
plt.setp(ax, 'frame_on', False)
ax.set_ylim([0, 5])
ax.set_xlim([0, 30])
ax.set_xticks([])
ax.set_yticks([])
ax.grid('off')

x1 = np.arange(2,8)
y1 = np.sin(x1)*np.sin(x1)

xoffset1 = max(x1) + 1

print x1

x2 = np.arange(5,10)
y2 = np.sin(x2)*np.sin(x2)
print x2
print x2+ xoffset1

xoffset2 = max(x2) + xoffset1



x3 = np.arange(3,15)
y3 = np.sin(x3)*np.sin(x3)
print x3
print x3+ xoffset2


ax.plot(x1,y1)
ax.plot(x2+xoffset1, y2)
ax.plot(x3+xoffset2, y3)

plt.show()

plot

Thank you

Upvotes: 0

Views: 1072

Answers (1)

Julien Spronck
Julien Spronck

Reputation: 15423

I am not sure if that is what you want but here is an example where I remapped all x-ranges to have all ranges taking the same space:

import numpy as np
import matplotlib.pyplot as plt

nplots = 3
xmin = 0
xmax = 30
subrange = float(xmax-xmin)/float(nplots)

ax = plt.subplot(111)
plt.setp(ax, 'frame_on', False)
ax.set_ylim([0, 5])
ax.set_xlim([xmin, xmax])
ax.set_xticks([])
ax.set_yticks([])
ax.grid('off')

xranges = [(2,8), (5,10), (3,15)]

for j in xrange(nplots):
    x = np.arange(*xranges[j])
    y = np.sin(x)*np.sin(x)

    new_x = (x-x[0])*subrange/float(x[-1]-x[0])+j*subrange ## remapping the x-range

    ax.plot(new_x,y)

plt.show()

Upvotes: 1

Related Questions