So I just started coding, and I'm so glad I chose Python to start me off! I really enjoy the clean layout, easy syntax, and power of the language. I'm doing astronomy research and I just started teaching myself matplotlib along with my general python work. I feel like I'm catching on quick, but I also feel that this particular plot script is a little rough. The plot looks correct, but the code seems really long for what I'm trying to do. So any tricks to make this more efficient would be greatly appreciated!
A little bit about what's going on before I post the script. I'm using np.genfromtext to upload a CSV file of a bunch of astronomy data. I need to break this data down into multiple subsets. 4 mass separated subsets (row['logM']) and each of those is divided into bulge and disk dominated. And for each of those I need to separate quiescent and star forming. So I have a total of 8 star forming subsets and 8 quiescent subsets. And all sixteen different styles of object need a distinct marker on the plot. I hope that with the comments you can see where I divided those up in the code. I did it all with a ton of for loops and if statements filling in a bunch of empty arrays. Hopefully this is enough info to make sense of what I'm going for. My main question is can I shorten this code, especially the for loops, to make it more efficient and clean? Here is the script: # -*- coding: utf-8 -*- #----- Zach Rizer #----- UVJ plot #----- data = n>2_qui_flag.csv import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as patch from matplotlib.path import Path fig = plt.figure('UVJ') ax = fig.add_subplot(111) #subplot for shaded region ########################################################################## # ----- Data uploaded from preliminary TopCat CSV Files data = np.genfromtxt('/Users/ProtonLenny/Documents/Research/Catalog_Data/Catalog_3/n>2_qui_flag.csv',\ dtype=None,names=True,delimiter =",") # ----- Define Quiesent Subset qui_m1d_xval = [] qui_m1d_yval = [] qui_m2d_xval = [] qui_m2d_yval = [] qui_m3d_xval = [] qui_m3d_yval = [] qui_m4d_xval = [] qui_m4d_yval = [] qui_m1b_xval = [] qui_m1b_yval = [] qui_m2b_xval = [] qui_m2b_yval = [] qui_m3b_xval = [] qui_m3b_yval = [] qui_m4b_xval = [] qui_m4b_yval = [] for row in data: if row['Dv'] > 0.65 and row['DSw'] > 0.65: * # filling disk-dom arrays* if row['UminV'] > 1.3 and row['VminJ'] < 1.6 and row['UminV'] > 0.88*(row['VminJ']+0.59):#quiescent criteria at particular z if row['Z'] >= 0.6 and row['Z'] < 0.9:#redshift bin criteria if row['logM'] >= 9.7 and row['logM'] < 10:#mass subsets for shape in plots qui_m1d_xval.append(row['VminJ']) * # (x) fill the empty list with the data from the appropriate column.* qui_m1d_yval.append(row['UminV']) elif row['logM'] >= 10 and row['logM'] < 10.5: qui_m2d_xval.append(row['VminJ']) qui_m2d_yval.append(row['UminV']) elif row['logM'] >= 10.5 and row['logM'] < 10.8: qui_m3d_xval.append(row['VminJ']) qui_m3d_yval.append(row['UminV']) elif row['logM'] >= 10.8: qui_m4d_xval.append(row['VminJ']) qui_m4d_yval.append(row['UminV']) if row['Dv'] < 0.35 and row['DSw'] > 0.65: * # filling bulge-dom arrays* if row['UminV'] > 1.3 and row['VminJ'] < 1.6 and row['UminV'] > 0.88*(row['VminJ']+0.59):#quiescent criteria at particular z if row['Z'] >= 0.6 and row['Z'] < 0.9:#redshift bin criteria if row['logM'] >= 9.7 and row['logM'] < 10:#mass subsets for shape in plots qui_m1b_xval.append(row['VminJ']) qui_m1b_yval.append(row['UminV']) elif row['logM'] >= 10 and row['logM'] < 10.5: qui_m2b_xval.append(row['VminJ']) qui_m2b_yval.append(row['UminV']) elif row['logM'] >= 10.5 and row['logM'] < 10.8: qui_m3b_xval.append(row['VminJ']) qui_m3b_yval.append(row['UminV']) elif row['logM'] >= 10.8: qui_m4b_xval.append(row['VminJ']) qui_m4b_yval.append(row['UminV']) # ----- Define Star-Forming Subset sf_m1d_xval = [] sf_m1d_yval = [] sf_m2d_xval = [] sf_m2d_yval = [] sf_m3d_xval = [] sf_m3d_yval = [] sf_m4d_xval = [] sf_m4d_yval = [] sf_m1b_xval = [] sf_m1b_yval = [] sf_m2b_xval = [] sf_m2b_yval = [] sf_m3b_xval = [] sf_m3b_yval = [] sf_m4b_xval = [] sf_m4b_yval = [] for row in data: if row['Dv'] > 0.65 and row['DSw'] > 0.65: # filling disk-dom arrays if row['UminV'] < 1.3 or row['VminJ'] > 1.6 or row['UminV'] < 0.88*(row['VminJ']+0.59):#star forming creteria at particular z if row['Z'] >= 0.6 and row['Z'] < 0.9:#reshift bin criteria if row['logM'] >= 9.7 and row['logM'] < 10: sf_m1d_xval.append(row['VminJ']) sf_m1d_yval.append(row['UminV']) elif row['logM'] >= 10 and row['logM'] < 10.5: sf_m2d_xval.append(row['VminJ']) sf_m2d_yval.append(row['UminV']) elif row['logM'] >= 10.5 and row['logM'] < 10.8: sf_m3d_xval.append(row['VminJ']) sf_m3d_yval.append(row['UminV']) elif row['logM'] >= 10.8: sf_m4d_xval.append(row['VminJ']) sf_m4d_yval.append(row['UminV']) if row['Dv'] < 0.35 and row['DSw'] > 0.65: # filling disk-dom arrays if row['UminV'] < 1.3 or row['VminJ'] > 1.6 or row['UminV'] < 0.88*(row['VminJ']+0.59):#star forming creteria at particular z if row['Z'] >= 0.6 and row['Z'] < 0.9:#reshift bin criteria if row['logM'] >= 9.7 and row['logM'] < 10: sf_m1b_xval.append(row['VminJ']) sf_m1b_yval.append(row['UminV']) elif row['logM'] >= 10 and row['logM'] < 10.5: sf_m2b_xval.append(row['VminJ']) sf_m2b_yval.append(row['UminV']) elif row['logM'] >= 10.5 and row['logM'] < 10.8: sf_m3b_xval.append(row['VminJ']) sf_m3b_yval.append(row['UminV']) elif row['logM'] >= 10.8: sf_m4b_xval.append(row['VminJ']) sf_m4b_yval.append(row['UminV']) # ----- plot the first column as x, and second column as y plot_qui_m4d, = plt.plot(qui_m4d_xval, qui_m4d_yval, 'r^', ms=12) plot_qui_m4b, = plt.plot(qui_m4b_xval, qui_m4b_yval, 'ro', ms=12) plot_qui_m3d, = plt.plot(qui_m3d_xval, qui_m3d_yval, 'r^', ms=9) plot_qui_m3b, = plt.plot(qui_m3b_xval, qui_m3b_yval, 'ro', ms=9) plot_qui_m2d, = plt.plot(qui_m2d_xval, qui_m2d_yval, 'r^', ms=6) plot_qui_m2b, = plt.plot(qui_m2b_xval, qui_m2b_yval, 'ro', ms=6) plot_qui_m1d, = plt.plot(qui_m1d_xval, qui_m1d_yval, 'r^', ms=3) plot_qui_m1b, = plt.plot(qui_m1b_xval, qui_m1b_yval, 'ro', ms=3) plot_sf_m4d, = plt.plot(sf_m4d_xval, sf_m4d_yval, 'b^', ms=12) plot_sf_m4b, = plt.plot(sf_m4b_xval, sf_m4b_yval, 'bo', ms=12) plot_sf_m3d, = plt.plot(sf_m3d_xval, sf_m3d_yval, 'b^', ms=9) plot_sf_m3b, = plt.plot(sf_m3b_xval, sf_m3b_yval, 'bo', ms=9) plot_sf_m2d, = plt.plot(sf_m2d_xval, sf_m2d_yval, 'b^', ms=6) plot_sf_m2b, = plt.plot(sf_m2b_xval, sf_m2b_yval, 'bo', ms=6) plot_sf_m1d, = plt.plot(sf_m1d_xval, sf_m1d_yval, 'b^', ms=3) plot_sf_m1b, = plt.plot(sf_m1b_xval, sf_m1b_yval, 'bo', ms=3) ########################################################################## ####### Shading, coloring, patches, etc... #------ quiescent divided region verts = [(-1.,1.3), (0.8,1.3), (1.6,2.0), (1.6,2.5), (-1.,2.5), (-1.,1.3)] codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY] path = Path(verts,codes) patch = patch.PathPatch(path, facecolor='none', lw=1.5, alpha=0.5, hatch='//') ax.add_patch(patch) ########################################################################## #----- titles and axis labels and axis limits plt.title('UVJ Plot of n>2 population at 0.6<z<0.9') plt.xlabel('V-J') plt.ylabel('U-V') plt.xlim(0.0, 2.5) plt.ylim(0.0, 2.5) plt.legend([plot_qui_m4d,plot_qui_m4b,plot_sf_m4d,plot_sf_m4b],["Qui-Disk (size goes w/ Mass)",\ "Qui-Bulge (size goes w/ Mass)","SF-Disk (size goes w/ Mass)",\ "SF-Bulge (size goes w/ Mass)"], 'best', numpoints=1) plt.show()
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor