1

I want to generate only one figure that contains all my scatter plots which are generated in a for loop, and of course changing colors between each iteration within the loop. Here is my code so far. It does plot each scatter plot individually, but I want to display all the curves at the same time. Any help is appreciated!

import sys
import getopt
import os 
import numpy as np
import pandas as pd
import datetime
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.pyplot import cm 



def usage():
  print "\nThis is the usage function\n"
  print 'Usage:\n'
  print 'YEAR: plotData -y [option]'
  print 'HELP: plotData -h'

def main(argv):
  try:
      opts, args = getopt.getopt(sys.argv[1:], ':y:h', ['year=', 'help'])
      if not opts:
        print 'No options supplied'
        usage()
  except getopt.GetoptError,e:
       print e
       usage()
       sys.exit(2)

  for opt, arg in opts:
     if opt in ('-h', '--help'):
         usage()
         sys.exit(2)

     elif opt in ('-y', '--year'):
         year_data = arg
         print 'YEAR: '+ year_data + ' Stations:\n'

         for dirname, dirnames, filenames in os.walk(year_data) :

             for sta in filenames:
                 N = len(filenames)
                 SplitName = sta.split('.', 3)
                 StaName =  SplitName[0]
                 M = filenames.index(sta)
                 comp = SplitName[2]
                 print StaName

                 f = os.path.join(dirname, sta)
                 YYYYJJJ = np.loadtxt(f, dtype='float', unpack=True)
                 xdates = [datetime.datetime.strptime(str(int(date)),'%Y%j') for date in YYYYJJJ]
                 cons = len(YYYYJJJ)
                 uno = np.ones((cons), dtype=np.int)
                 uno = uno*M

                 fig1, ax1 = plt.subplots(figsize=(15,10))       
                 #ax1.set_title("Station: "+StaName+','+' Component: '+comp)
                 ax1.set_xlabel('Time')
                 ax1.set_ylim(-1, N+2)
                 ax1.axes.get_yaxis().set_visible(False)
                 ax1.grid(True)

                 ax1.scatter(xdates,uno, c='b', alpha=1, marker=(5, 0), s=30)
                 plt.hold(True)
                 plt.show()
                 #fig1.savefig('example'+'.pdf', format='pdf', dpi=1000) 

     else:
         print "Input parameters are not recognized, aborting"
         sys.exit(2)

if __name__ =='__main__':
    main(sys.argv[1:]) 

An example of the table(s) that is reading is next (within a directory called "2016"):

file: CAO2.2016.HHZ.dat

2016001
2016002
2016003
2016004
2016005
2016006
2016007
2016008
2016009
2016010
2016011
2016012
2016013
2016014
2016015
2016016
2016017
2016018
2016019
2016020
2016021
2016022
2016023
2016024
2016025
2016026
2016027

1 Answer 1

4

move the figure definitions and plt.show() outside of the loop by unindenting it, as such:

fig1, ax1 = plt.subplots(figsize=(15,10))
#ax1.set_title("Station: "+StaName+','+' Component: '+comp)
ax1.set_xlabel('Time')
ax1.set_ylim(-1, N+2)
ax1.axes.get_yaxis().set_visible(False)
ax1.grid(True) 

for sta in filenames:
    N = len(filenames)
    SplitName = sta.split('.', 3)
    StaName =  SplitName[0]
    M = filenames.index(sta)
    comp = SplitName[2]
    print StaName

    f = os.path.join(dirname, sta)
    YYYYJJJ = np.loadtxt(f, dtype='float', unpack=True)
    xdates = [datetime.datetime.strptime(str(int(date)),'%Y%j')
              for date in YYYYJJJ]
    cons = len(YYYYJJJ)
    uno = np.ones((cons), dtype=np.int)
    uno = uno*M
    ax1.scatter(xdates,uno, c='b', alpha=1, marker=(5, 0), s=30)

plt.show()
Sign up to request clarification or add additional context in comments.

1 Comment

you can also take the N = len(filenames) out of the loop by the way

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.