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()
2
  • Possible duplicate of stackoverflow.com/questions/1358977/… Commented Dec 15, 2011 at 16:42
  • @cyborg I saw that, but I still couldn't figure out my problem. Commented Dec 15, 2011 at 16:43

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

Comments

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.