[Tutor] plotting several datasets and calling data from afar

Elaina Ann Hyde elainahyde at gmail.com
Tue Mar 27 02:58:38 CEST 2012


On Mon, Mar 26, 2012 at 11:14 PM, Evert Rol <evert.rol at gmail.com> wrote:

>  Hi Elaina,
>
>
> > Hi everyone,
> >    I am trying to set up a code to do some plotting and before I get too
> far I wanted to ask some structure questions.  Basically I want to tell
> python to read 2 datasets, plot them on the same scale on the same x-y axis
> , read a third dataset and match the name from the first dataset, then
> label certain values from the third... complicating matters is that all
> these data are part of much, much larger sets in seperate files, the paths
> look like:
> > pathway1/namered.dat
> > pathway2/nameblue.dat
> > matchingfile.txt
> >
> > so I do fopen on the matchingfile, read it with asciitable, and then I
> have a column in that file called 'name' and a column called 'M', I sort
> the file, return a subset that is interesting, and get name1, name2, etc
> for every subset.  I want to make a plot that looks like:
> >
> > plot pathway1/namered.dat and pathway2/nameblue.dat with label 'M' for
> every value in the subset name1, each row[i] I need to assign to a seperate
> window so that I get a multiplot with a shared x-axis, and stacking my
> plots up the y-axis.  I do have multiplot working and I know how to plot
> 'M' for each subset.
> >
> > The conceptual trouble has come in, how do I match 'name' variable of my
> subset 'name1' with the plot I want to do for pathway1/namered.dat and
> pathway2/nameblue.dat... the key feature that is the same is the 'name'
> variable, but in one instance I have to match the 'name'+'red.dat' and in
> the other the 'name'+'blue.dat'
>
> It's not 100% clear to me what you precisely want to do, but here are a
> few possibilites:
>
> - use a dictionary. Assign each dataset to a dictionary with the name as
> the key (or the name + color). Eg, dataset['name1red'],
> dataset['name1blue'], dataset['name2red'] etc. Each value in this
> dictionary is a dataset read by asciitable.
>  the (big) disadvantage is that you would read every single *.dat file
> beforehand
> - simply fopen the files with the filename deduced from the name. So, in a
> loop, that would be something like
>   for name in subset_names:
>      with fopen(name + 'red.dat') as datafile:
>          # read data and plot
>      with fopen(name + 'blue.dat') as datafile:
>          # read data and plot
>
> But perhaps I misunderstand your problem. I can't really tell if the
> problem is in opening the selected data files, or plotting them, or
> creating the labels for the plot.
> This may actually be where some code comes in handy. Provided the plotting
> isn't the problem, you could make a very simple Python script that shows
> the concept and how you attempt to solve it. The simpler the script, the
> better probably the implementation, so even make such a simple script is
> incredibly useful. (Any example data sets of, say, just 2 lines each can
> also help with that.)
> From that, the problem may become clearer and we can help you improving
> the script.
>
> Of course, someone else may actually understand the problem properly and
> has a better suggestion.
>
> Cheers,
>
>  Evert
>
> (yes, small world ;-)
>
>
> > Any ideas would be appreciated, thanks!
> > ~Elaina Hyde
> >
> > --
> > PhD Candidate
> > Department of Physics and Astronomy
> > Faculty of Science
> > Macquarie University
> > North Ryde, NSW 2109, Australia
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > To unsubscribe or change subscription options:
> > http://mail.python.org/mailman/listinfo/tutor
>
> ---------------------------------



Thanks Evert and Tino,
   The dictionaries look a bit like the right idea, but in the end I was
able to manipulate my input table so they aren't quite necessary.  The code
I have now, which partially works, is as follows:
----------------------------
#!/usr/bin/python

# import modules used here
import sys
import asciitable
import matplotlib
import matplotlib.path as mpath
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure, show, axis
from matplotlib.patches import Ellipse
import scipy
import numpy as np
from numpy import *
import math
import pylab
import random
from pylab import *
import astropysics
import astropysics.obstools
import astropysics.coords
import string
from astropysics.coords import ICRSCoordinates,GalacticCoordinates

#File from Read_All
x=open('LowZ_joinAll')

dat=asciitable.read(x,Reader=asciitable.NoHeader,
fill_values=['--','-999.99'])
#gives dat file where filenames are first two columns

###############################
bluefilename1=dat['col1']
filename1=dat['col2']

#other stuff I need

#Ra/Dec in decimal radians
Radeg1=dat[ 'col6']*180./math.pi   #ra-drad
Decdeg1=dat['col7']*180./math.pi    #dec-drad
Vmag=dat['col8']
Mag=dat['col15']
EW1=dat['col16']
EW2=dat['col17']
EW3=dat['col18']
#Horizontal Branch Estimate
VHB=18.0
EWn = (0.5*abs(EW1)) + abs(EW2) + (0.6*abs(EW3))
# NEED ABS VALUE FOR FORMULA
FEHn = -2.66 + 0.42*(EWn + 0.64*(Vmag - VHB))
EW1_G=dat['col23']
EW2_G=dat['col24']
EW3_G=dat['col25']
EWg = (0.5*abs(EW1_G)) + abs(EW2_G) + (0.6*abs(EW3_G))
FEHg = -2.66 + 0.42*(EWg + 0.64*(Vmag - VHB))
#use 0.15-0.2 dex as standard error -- R. Ibata
FEHerror=0.2
#corrected velocity
Vhel=dat['col37']
V_err=dat['col38']
m_H=dat['col74']
alpha_Fe=dat['col75']
microturb=dat['col76']
Vrot=dat['col77']
Chisq=dat['col78']
RVorig=dat['col79']
RVcorr=dat['col80']
Heliocentric_RV=RVorig+RVcorr

#now if I want to make plots I have to access paths

#example, trying with one element
path1r='../Core2dfdr-red-sorted/'+filename1[1]
path1b='../Core2dfdr-blue/'+bluefilename1[1]

#and use a title in the plot
title1='Data Fe/Hn='+str(FEHn[1])+' Fe/Hg='+str(FEHg[1])

for i in xrange(len(Radeg1)):
    if i<=5:
#subset1
        print 'filename= ',filename1[i],'  ',bluefilename1[i],'  FEHn=
',FEHn[i],'  FEHg= ',FEHg[i]
#multiplot1
        fig1, (ax1, ax2, ax3, ax4, ax5) = plt.subplots(5, sharex=True)
        ax1.plot(path1r,'--ro')
        ax1.plot(path1b,'--bo')
#        ax1.set_title('Data Fe/Hn='+str(FEHn[1])+' Fe/Hg='+str(FEHg[1]),
fontsize='11')

#        ax2.plot('../Core2dfdr-red-sorted/'filename1[2],'--ro')
#        ax2.plot('../Core2dfdr-blue/'filenameblueb[2],'--bo')
#        ax2.set_title('Data Fe/Hn='FEHn[2]' Fe/Hg='FEHg[2], fontsize='11')

#        ax3.plot('../Core2dfdr-red-sorted/'filename1[3],'--ro')
#        ax3.plot('../Core2dfdr-blue/'filenameblueb[3],'--bo')
#        ax3.set_title('Data Fe/Hn='FEHn[3]' Fe/Hg='FEHg[3], fontsize='11')

#       ax4.plot('../Core2dfdr-red-sorted/'filename1[4],'--ro')
#        ax4.plot('../Core2dfdr-blue/'filenameblueb[4],'--bo')
#        ax4.set_title('Data Fe/Hn='FEHn[4]' Fe/Hg='FEHg[4], fontsize='11')

#        ax5.plot('../Core2dfdr-red-sorted/'filename1[5],'--ro')
#        ax5.plot('../Core2dfdr-blue/'filenameblueb[5],'--bo')
#        ax5.set_title('Data Fe/Hn='FEHn[5]' Fe/Hg='FEHg[5], fontsize='11')

        plt.show()
#subset2
#    if i>5 and i<=10:
#        print 'filename= ',filename1[i],'  FEHn= ',FEHn[i],'  FEHg=
',FEHg[i]

.....
this is where it breaks down.
   The path name it cannot follow, and the title is not fully a string.
Since I will want to do this for many subset's I would prefer to loop but I
don't see a good way to do that.  If I try with just one element I get the
error:

filename=  100604F2_1red.0063.fits    100604F2_1blueb.0063.fits   FEHn=
-3.16295   FEHg=  -3.216626
Traceback (most recent call last):
  File "plot_LowZ_targets.py", line 232, in <module>
    ax1.plot(path1r,'--ro')
  File
"/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/matplotlib/axes.py",
line 3849, in plot
    self.add_line(line)
  File
"/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/matplotlib/axes.py",
line 1443, in add_line
    self._update_line_limits(line)
  File
"/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/matplotlib/axes.py",
line 1451, in _update_line_limits
    p = line.get_path()
  File
"/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/matplotlib/lines.py",
line 644, in get_path
    self.recache()
  File
"/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/matplotlib/lines.py",
line 401, in recache
    y = np.asarray(yconv, np.float_)
  File
"/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/numpy/core/numeric.py",
line 235, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float:
../Core2dfdr-red-sorted/100604F2_2red.0063.fits

..........
Note that the print statement I put in works, so the correct values are
called.  I just haven't assigned in a way that python can read, and look at
the path.  Trying to make my path variables strings doesn't seem to work
either, so I'm a little lost.  Thanks everyone, and yes, small world Evert!
~Elaina


-- 
PhD Candidate
Department of Physics and Astronomy
Faculty of Science
Macquarie University
North Ryde, NSW 2109, Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120327/2b453546/attachment.html>


More information about the Tutor mailing list