0

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

Community
  • 1
  • 1
aitatanit
  • 60
  • 2
  • not sure to understand the problem ... you only have one plot here (three different lines but all on the same plot) ... also, do you want the same x range (all plots range from 0 to 10 for example) or do you want the same scale? – Julien Spronck Mar 13 '16 at 18:40
  • The same scale. some x values go from 3 - 10 and some from 2 - 5. The latter will look squeezed when plotted with the former inside the same figure. Is there an efficient mapping/transformation that makes the values of each plot's x-values say in a 1-inch wide. That way all the images will have the same x scale and they'll look better – aitatanit Mar 13 '16 at 18:45
  • and yes it's one plot with several "plots" inside it, shifting the length of the x-axis as I move along the horizontal. It's much faster than using subplot for each plot especially when you have 100s of them. – aitatanit Mar 13 '16 at 18:47

1 Answers1

1

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()
Julien Spronck
  • 15,069
  • 4
  • 47
  • 55