swincas / cookies-n-code

A repo for code review sessions at CAS
http://astronomy.swin.edu.au/
MIT License
31 stars 34 forks source link

Annotating points on the plot #16

Closed rdzudzar closed 6 years ago

rdzudzar commented 6 years ago

Hi, what solution is the best to annotate points on the plot (more than a few)?

np.loadtxt will not load/recognize floats (and I use it to load data); Manually typing:

ax.annotate('cool galaxy',Mass[0], SFR[0] )
...
ax.annotate('boring galaxy',Mass[45], SFR[45] )

take ages and ages (and makes my jupyter notebook ugly).

Suggestions?

(@manodeep edited for syntax highlighting)

jacobseiler commented 6 years ago

If you're reading a string (what you meant instead of float), you can use the more general np.genfromtxt from numpy (Doc Page)

So I would use np.loadtxt to load all of your numbers, then use names = np.genfromtxt(filename, dtype='str', usecols(COL# WITH NAME). As an aside you could also do this natively with pandas, I'd check out the code-review tutorial.

For your loop since Mass and SFR is incrementing by 1 for each iteration you can use enumerate (Doc Page).

for count, name in enumerate(names):
    ax.annotate(name, Mass[count], SFR[count])

By wrapping your loop with enumerate the count variable will be incremented by 1 each iteration. This way you don't need to define count outside the loop and have to deal with doing count += 1.

Let me know if this works for you and be sure to post your solution before you close the issue ;)

rdzudzar commented 6 years ago

Perfect! Solved problem. Thank you.

rdzudzar commented 6 years ago

I did end up splitting the reading into: read the floats (using np.loadtxt) followed by reading the strings (using np.genfromtxt).

Class, Mabs_R_0_T,  LOGF_HA_0_T,    RA, DEC,    INC,    PA, FRAC_HA0_T, TGAS_T, TGAS0_T,    MABS_R_T,   MABS_R0_T,  MAPP_R_T,   \
                ERR_MAG_R_T,    RE_R_T, ERR_RE_R_T, RE_HA_T,    ERR_RE_HA_T0,   LOGMDYN,    LOGMDYN_90, ERR_LOGF_HA_0_T,    LOGL_R0_T,  \
                LOGL_R_T,   ERR_LOGL_R_T,   LOGL_HA0_T, LOGSE_HA0_T,    ERR_LOGSE_HA_T, Stell_L, Stell_M, Mag_class, EW, MUER, Morphology, Group, Central= \
                np.loadtxt('AllChoirGalaxies.txt', usecols = (2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35, 36), unpack=True)

Names = np.genfromtxt('AllChoirGalaxies.txt',delimiter='',usecols=1, dtype=str)

for count, name in enumerate(Names):
    ax.annotate(name, (Stell_M[count],np.log10(ChoirSFR[count]/ChoirStellM[count])), fontsize = 10)

I wish I could be as cool as you @jacobseiler