1

I'm making three scatter plots, and I'd like them to all show up in the same window (but not in the same plot). Right now, three separate windows pop up, one for each plot. If I move matplotlib.pyplot.show() outside the loop, then they are all plotted on the same set of axes.

import matplotlib.pyplot

time = [1,2,3]
value = {}
value['x'] = [1,2,3]
value['y'] = [1,4,9]
value['z'] = [1,8,27]
for dimension in ['x', 'y', 'z']:
    matplotlib.pyplot.scatter(time, value[dimension])
    matplotlib.pyplot.show()
BenH
  • 2,100
  • 2
  • 22
  • 33
  • Possible duplicate of http://stackoverflow.com/questions/1358977/how-to-make-several-plots-on-a-single-page-using-matplotlib – cyborg Dec 15 '11 at 16:42
  • @cyborg I saw that, but I still couldn't figure out my problem. – BenH Dec 15 '11 at 16:43

2 Answers2

4

use subplot to create subplots:

import matplotlib.pyplot as plt

time = [1,2,3]
value = {}
value['x'] = [1,2,3]
value['y'] = [1,4,9]
value['z'] = [1,8,27]
for k, dimension in enumerate(['x', 'y', 'z']):
    plt.subplot(3, 1, k)
    plt.scatter(time, value[dimension])

plt.show()
David Zwicker
  • 23,581
  • 6
  • 62
  • 77
1

I think there has been a python version change that breaks the above code, it did not run for me because enumerate now starts at 0, for example enumerate(iterable, start=0) is the base case, so I made the following modification and it ran:

import matplotlib.pyplot as plt

time = [1,2,3]
value = {}
value['x'] = [1,2,3]
value['y'] = [1,4,9]
value['z'] = [1,8,27]
for k, dimension in enumerate(['x', 'y', 'z'], 1):
#     print(k, dimension)
    plt.subplot(3, 1, k)
    plt.scatter(time, value[dimension])

plt.show()