0

I am plotting time series data in matplotlib utilising the plot_date command.

In the following code the dt_str_etc and BTOTetc refer to arrays of datetime formatted data. Part of my Code is as follows:

import matplotlib.pylab as plt
import matplotlib.dates as dates
from datetime import datetime

f,axarr=plt.subplots(nrows=9,sharex=False)

axarr[0].plot_date(dt_str_16h25,BTOT16h25,fmt='-k')
axarr[1].plot_date(dt_str_16h26,BTOT16h26,fmt='-k')
axarr[2].plot_date(dt_str_16h27,BTOT16h27,fmt='-k')
axarr[3].plot_date(dt_str_16h28,BTOT16h28,fmt='-k')
axarr[4].plot_date(dt_str_16h29,BTOT16h29,fmt='-k')
axarr[5].plot_date(dt_str_16h30,BTOT16h30,fmt='-k')
axarr[6].plot_date(dt_str_16h31,BTOT16h31,fmt='-k')
axarr[7].plot_date(dt_str_16h32,BTOT16h32,fmt='-k')
axarr[8].plot_date(dt_str_16h33,BTOT16h33,fmt='-k')

Now, the above code works. plot_date functions as you would expect and I get a nice plot with 9 subplots together with a nice looking time series...

What I want to do is to plot the above 9 plots not on one column but two columns [Essentially 2 by 5 (with one unoccupied space)]]. A multiple columned subplot. I attempted to do so using the following code:

import matplotlib.pylab as plt
import matplotlib.dates as dates
from datetime import datetime

f,axarr=plt.subplots(nrows=5,ncols=2,sharex=False)

axarr[0].plot_date(dt_str_16h25,BTOT16h25,fmt='-k')
axarr[1].plot_date(dt_str_16h26,BTOT16h26,fmt='-k')
axarr[2].plot_date(dt_str_16h27,BTOT16h27,fmt='-k')
axarr[3].plot_date(dt_str_16h28,BTOT16h28,fmt='-k')
axarr[4].plot_date(dt_str_16h29,BTOT16h29,fmt='-k')
axarr[5].plot_date(dt_str_16h30,BTOT16h30,fmt='-k')
axarr[6].plot_date(dt_str_16h31,BTOT16h31,fmt='-k')
axarr[7].plot_date(dt_str_16h32,BTOT16h32,fmt='-k')
axarr[8].plot_date(dt_str_16h33,BTOT16h33,fmt='-k')

This returns me the error:

Traceback (most recent call last):
      File "C:/Users/Charles/Google Drive/Year 4 Real/MPhys Project/Mag     Data/2007/Mag_data_2007_plot_T 25 26 27 28 29 30 31 32 33 adjusted.py", line 118, in <module>
    axarr[1].plot_date(dt_str_16h25,BTOT16h25,fmt='-k')
AttributeError: 'numpy.ndarray' object has no attribute 'plot_date'

All I have done is write "ncols=2". That is the only difference. Why can I no longer plot in this way using the plot_date command?

I have read some info on the subplot matplotlib documentation about potentiall using "add_subplot". If needs be I can re write the code using this, but I would really like to keep it as it is. Any ideas?

Many thanks for any help or thoughts provided.

1 Answer 1

1

When the plot layout has multiple rows and columns, the returned array is two-dimensional. You'll need to access the individual subplots using things like axarr[0, 0] and axarr[0, 1].

Sign up to request clarification or add additional context in comments.

2 Comments

Hi BrenBarn. Thanks so much for your help, feel a bit stupid ha. I still don't have enough reputation to upvote this, but hopefully, until I can, my thanks will be enough! Thank you so much!
There is also a kwarg to subplots ('flatten' iirc of the top of my head) that will return the axes back to you as a 1D list

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.