0

I want to iterate through matplotlib subplots, plotting one trace, tr in each subplot. However, the output that I currently am getting plots all 8 traces in each subplot. How do I plot just one trace into their own subplots? I suspect it has something to do with my for loop indexing, but I can't seem to determine where this issue lies. Thanks.

The simplified code is below:

traces = glob.glob('*.data')
fig,ax = plt.subplots(nrows=4, ncols=2)
index = 0 
for index, ix in enumerate(traces): 
    tr = obspy.read(traces[index])[0]

    *do stuff* 

     for i in range(4):
        for j in range(2):
            ax[i, j].plot(time,tr.data, color='k')
            index+=1

EDIT: THE CODE BELOW HAS SOLVED THIS QUESTION.

traces = glob.glob('*.data')
fig,axes = plt.subplots(nrows=4, ncols=2)
index = 0 
for tr, ax in zip(traces, axes.reshape(-1)):
    ** do stuff **
    tr = obspy.read(traces[index])[0]
    ax.plot(time,tr_trim.data, color='k')
    ax.set_ylabel("Raw Data")
    ax.set_xticks([]);
    index +=1
plt.show()
Thomas Kühn
  • 9,412
  • 3
  • 47
  • 63
geeb.24
  • 527
  • 2
  • 7
  • 25
  • Please see the answer in the question I linked -- you need to reduce your `for` loops into one by `zip`ing the `traces` and `axes` together. – Thomas Kühn Jan 12 '18 at 08:10
  • Will take a look at it now. Thanks Thomas. – geeb.24 Jan 12 '18 at 08:33
  • Hi Thomas, I've edited the above script to reflect the changes outlined in your other response. However, I am receiving the following error: AttributeError: 'numpy.ndarray' object has no attribute 'plot'. I thought that error should have been taken care of by the reshape command, but I guess not. Any thoughts? – geeb.24 Jan 12 '18 at 10:22
  • Yes, you have the `ax` and `axes` mixed up. Correct the following two lines as I indicate and your code will be fine: `fig,axes = plt.subplots(nrows=4, ncols=2)` and `for tr, ax in zip(traces, axes.reshape(-1)):` – Thomas Kühn Jan 12 '18 at 10:30
  • One more thing, it would be better to keep the original code in your question, so that future readers will not become confused. Add the new code below the original code with a statement something like: '**EDIT** for clarification:' or some such. – Thomas Kühn Jan 12 '18 at 10:32
  • Note taken. Thanks again for your help, Thomas. – geeb.24 Jan 13 '18 at 04:13
  • We probably had a little misunderstanding. I edited the question to show what I had in mind -- now it's perfect :) – Thomas Kühn Jan 13 '18 at 09:19
  • Thanks for the edit. Last question: If we take this one step further and want to add another ax in each subplot, what would be the best way to go about this? If I add the line: `ax = fig.add_axes([])` and then plot another set of data, this axis does iterate. So, if I wish to add multiple axes to each subplot, using the above setup, is there a trick in doing so? Thanks. – geeb.24 Jan 13 '18 at 10:01
  • I'm not quite sure if I understand what you mean. As this is more about the layout of a figure, I would almost suggest that you post a new question, best with a rough picture of the layout you are after. – Thomas Kühn Jan 14 '18 at 08:27

0 Answers0