9
9
import numpy as np
10
10
from numpy .linalg import solve
11
11
try :
12
- import matplotlib
13
- # TODO: Sometimes this interferes with notebooks, other times not.
14
- # Maybe don't set this at import time.
15
- # matplotlib.use('Agg')
16
- matplotlib .rcParams ['figure.figsize' ] = (16.0 , 12.0 )
17
12
import matplotlib .pyplot as plt
18
13
from matplotlib .font_manager import FontProperties
19
14
except ImportError :
23
18
from astropy .io import ascii , fits
24
19
from . import Pydlspec2dException , Pydlspec2dUserWarning
25
20
21
+ #
22
+ # Used for automated matplotlib plots.
23
+ #
24
+ _default_figsize = (16.0 , 12.0 )
25
+
26
26
#
27
27
# Used by findspec
28
28
#
@@ -782,24 +782,22 @@ def plot_eig(filename, title='Unknown'):
782
782
title = 'CV Stars: Eigenspectra'
783
783
else :
784
784
raise ValueError ('Unknown template type!' )
785
- base , ext = filename .split ('.' )
786
- spectrum = fits .open (filename )
787
- newloglam0 = spectrum [0 ].header ['COEFF0' ]
788
- objdloglam = spectrum [0 ].header ['COEFF1' ]
789
- spectro_data = spectrum [0 ].data
790
- spectrum .close ()
785
+ base , ext = os .path .splitext (filename )
786
+ with fits .open (filename , mode = 'readonly' ) as hdulist :
787
+ newloglam0 = hdulist [0 ].header ['COEFF0' ]
788
+ objdloglam = hdulist [0 ].header ['COEFF1' ]
789
+ spectro_data = hdulist [0 ].data
791
790
(neig , ndata ) = spectro_data .shape
792
791
newloglam = np .arange (ndata ) * objdloglam + newloglam0
793
792
lam = 10.0 ** newloglam
794
- fig = plt .figure (dpi = 100 )
795
- ax = fig .add_subplot (111 )
793
+ fig , ax = plt .subplots (1 , 1 , figsize = _default_figsize , dpi = 100 )
796
794
colorvec = ['k' , 'r' , 'g' , 'b' , 'm' , 'c' ]
797
795
for l in range (neig ):
798
796
_ = ax .plot (lam , spectro_data [l , :],
799
797
colorvec [l % len (colorvec )]+ '-' , linewidth = 1 )
800
- ax .set_xlabel (r'Wavelength [$\AA$]' )
801
- ax .set_ylabel ('Flux [Arbitrary Units]' )
802
- ax .set_title (title )
798
+ _ = ax .set_xlabel (r'Wavelength [$\AA$]' )
799
+ _ = ax .set_ylabel ('Flux [Arbitrary Units]' )
800
+ _ = ax .set_title (title )
803
801
# ax.set_xlim([3500.0,10000.0])
804
802
# ax.set_ylim([-400.0,500.0])
805
803
# fig.savefig(base+'.zoom.png')
@@ -1515,47 +1513,44 @@ def template_input(inputfile, dumpfile, flux=False, verbose=False):
1515
1513
for k in range (nplots ):
1516
1514
istart = k * nfluxes
1517
1515
iend = min (istart + nfluxes , nspectra ) - 1
1518
- fig = plt .figure (dpi = 100 )
1519
- ax = fig .add_subplot (111 )
1516
+ fig , ax = plt .subplots (1 , 1 , figsize = _default_figsize , dpi = 100 )
1520
1517
for l in range (istart , iend + 1 ):
1521
1518
_ = ax .plot (10.0 ** pcaflux ['newloglam' ],
1522
1519
pcaflux ['newflux' ][l , :] + separation * (l % nfluxes ),
1523
1520
colorvec [l % len (colorvec )]+ '-' ,
1524
1521
linewidth = 1 )
1525
- ax .set_xlabel (r'Wavelength [$\AA$]' )
1526
- ax .set_ylabel (r'Flux [$\mathsf{10^{-17} erg\, cm^{-2} s^{-1} \AA^{-1}}$] + Constant' )
1527
- ax .set_title ('Input Spectra {0:04d}-{1:04d}' .format (istart + 1 , iend + 1 ))
1528
- ax .set_ylim (pcaflux ['newflux' ][istart , :].min (), pcaflux ['newflux' ][iend - 1 , :].max ()+ separation * (nfluxes - 1 ))
1522
+ _ = ax .set_xlabel (r'Wavelength [$\AA$]' )
1523
+ _ = ax .set_ylabel (r'Flux [$\mathsf{10^{-17} erg\, cm^{-2} s^{-1} \AA^{-1}}$] + Constant' )
1524
+ _ = ax .set_title ('Input Spectra {0:04d}-{1:04d}' .format (istart + 1 , iend + 1 ))
1525
+ _ = ax .set_ylim (pcaflux ['newflux' ][istart , :].min (), pcaflux ['newflux' ][iend - 1 , :].max ()+ separation * (nfluxes - 1 ))
1529
1526
fig .savefig ('{0}.flux.{1:04d}-{2:04d}.png' .format (outfile , istart + 1 , iend + 1 ))
1530
1527
plt .close (fig )
1531
1528
#
1532
1529
# Missing data diagnostic.
1533
1530
#
1534
- fig = plt .figure (dpi = 100 )
1535
- ax = fig .add_subplot (111 )
1531
+ fig , ax = plt .subplots (1 , 1 , figsize = _default_figsize , dpi = 100 )
1536
1532
_ = ax .plot (10.0 ** pcaflux ['newloglam' ], (pcaflux ['newivar' ] == 0 ).sum (0 )/ float (nspectra ), 'k-' )
1537
- ax .set_xlabel (r'Wavelength [$\AA$]' )
1538
- ax .set_ylabel ('Fraction of spectra with missing data' )
1539
- ax .set_title ('Missing Data' )
1540
- ax .grid (True )
1533
+ _ = ax .set_xlabel (r'Wavelength [$\AA$]' )
1534
+ _ = ax .set_ylabel ('Fraction of spectra with missing data' )
1535
+ _ = ax .set_title ('Missing Data' )
1536
+ _ = ax .grid (True )
1541
1537
fig .savefig (outfile + '.missing.png' )
1542
1538
plt .close (fig )
1543
1539
#
1544
1540
# usemask diagnostic
1545
1541
#
1546
1542
if 'usemask' in pcaflux :
1547
- fig = plt .figure (dpi = 100 )
1548
- ax = fig .add_subplot (111 )
1543
+ fig , ax = plt .subplots (1 , 1 , figsize = _default_figsize , dpi = 100 )
1549
1544
_ = ax .semilogy (10.0 ** pcaflux ['newloglam' ][pcaflux ['usemask' ] > 0 ],
1550
1545
pcaflux ['usemask' ][pcaflux ['usemask' ] > 0 ], 'k-' ,
1551
1546
10.0 ** pcaflux ['newloglam' ],
1552
1547
np .zeros (pcaflux ['newloglam' ].shape ,
1553
1548
dtype = pcaflux ['newloglam' ].dtype ) + metadata ['minuse' ],
1554
1549
'k--' )
1555
- ax .set_xlabel (r'Wavelength [$\AA$]' )
1556
- ax .set_ylabel ('Usemask' )
1557
- ax .set_title ('UseMask' )
1558
- ax .grid (True )
1550
+ _ = ax .set_xlabel (r'Wavelength [$\AA$]' )
1551
+ _ = ax .set_ylabel ('Usemask' )
1552
+ _ = ax .set_title ('UseMask' )
1553
+ _ = ax .grid (True )
1559
1554
fig .savefig (outfile + '.usemask.png' )
1560
1555
plt .close (fig )
1561
1556
#
@@ -1565,36 +1560,34 @@ def template_input(inputfile, dumpfile, flux=False, verbose=False):
1565
1560
aratio10 = pcaflux ['acoeff' ][:, 1 ]/ pcaflux ['acoeff' ][:, 0 ]
1566
1561
aratio20 = pcaflux ['acoeff' ][:, 2 ]/ pcaflux ['acoeff' ][:, 0 ]
1567
1562
aratio30 = pcaflux ['acoeff' ][:, 3 ]/ pcaflux ['acoeff' ][:, 0 ]
1568
- fig = plt .figure (dpi = 100 )
1569
- ax = fig .add_subplot (111 )
1563
+ fig , ax = plt .subplots (1 , 1 , figsize = _default_figsize , dpi = 100 )
1570
1564
_ = ax .plot (aratio10 , aratio20 , marker = 'None' , linestyle = 'None' )
1571
1565
for k in range (len (aratio10 )):
1572
1566
_ = ax .text (aratio10 [k ], aratio20 [k ],
1573
1567
'{0:04d}-{1:04d}' .format (slist .plate [k ], slist .fiberid [k ]),
1574
1568
horizontalalignment = 'center' , verticalalignment = 'center' ,
1575
1569
color = colorvec [k % len (colorvec )],
1576
1570
fontproperties = smallfont )
1577
- # ax.set_xlim([aratio10.min(), aratio10.max])
1578
- # ax.set_xlim([aratio20.min(), aratio20.max])
1579
- ax .set_xlabel ('Eigenvalue Ratio, $a_1/a_0$' )
1580
- ax .set_ylabel ('Eigenvalue Ratio, $a_2/a_0$' )
1581
- ax .set_title ('Eigenvalue Ratios' )
1571
+ # _ = ax.set_xlim([aratio10.min(), aratio10.max])
1572
+ # _ = ax.set_xlim([aratio20.min(), aratio20.max])
1573
+ _ = ax .set_xlabel ('Eigenvalue Ratio, $a_1/a_0$' )
1574
+ _ = ax .set_ylabel ('Eigenvalue Ratio, $a_2/a_0$' )
1575
+ _ = ax .set_title ('Eigenvalue Ratios' )
1582
1576
fig .savefig (outfile + '.a2_v_a1.png' )
1583
1577
plt .close (fig )
1584
- fig = plt .figure (dpi = 100 )
1585
- ax = fig .add_subplot (111 )
1578
+ fig , ax = plt .subplots (1 , 1 , figsize = _default_figsize , dpi = 100 )
1586
1579
_ = ax .plot (aratio20 , aratio30 , marker = 'None' , linestyle = 'None' )
1587
1580
for k in range (len (aratio10 )):
1588
1581
_ = ax .text (aratio20 [k ], aratio30 [k ],
1589
1582
'{0:04d}-{1:04d}' .format (slist .plate [k ], slist .fiberid [k ]),
1590
1583
horizontalalignment = 'center' , verticalalignment = 'center' ,
1591
1584
color = colorvec [k % len (colorvec )],
1592
1585
fontproperties = smallfont )
1593
- # ax.set_xlim([aratio10.min(), aratio10.max])
1594
- # ax.set_xlim([aratio20.min(), aratio20.max])
1595
- ax .set_xlabel ('Eigenvalue Ratio, $a_2/a_0$' )
1596
- ax .set_ylabel ('Eigenvalue Ratio, $a_3/a_0$' )
1597
- ax .set_title ('Eigenvalue Ratios' )
1586
+ # _ = ax.set_xlim([aratio10.min(), aratio10.max])
1587
+ # _ = ax.set_xlim([aratio20.min(), aratio20.max])
1588
+ _ = ax .set_xlabel ('Eigenvalue Ratio, $a_2/a_0$' )
1589
+ _ = ax .set_ylabel ('Eigenvalue Ratio, $a_3/a_0$' )
1590
+ _ = ax .set_title ('Eigenvalue Ratios' )
1598
1591
fig .savefig (outfile + '.a3_v_a2.png' )
1599
1592
plt .close (fig )
1600
1593
#
@@ -1862,8 +1855,7 @@ def template_star(metadata, newloglam, newflux, newivar, slist, outfile,
1862
1855
thesesubclassnum = np .zeros (thesesubclass .size , dtype = 'i4' )
1863
1856
colorvec = ['k' , 'r' , 'g' , 'b' , 'm' , 'c' ]
1864
1857
smallfont = FontProperties (size = 'xx-small' )
1865
- fig = plt .figure (dpi = 100 )
1866
- ax = fig .add_subplot (111 )
1858
+ fig , ax = plt .subplots (1 , 1 , figsize = _default_figsize , dpi = 100 )
1867
1859
for isub in range (nsubclass ):
1868
1860
ii = (thesesubclass == subclasslist [isub ]).nonzero ()[0 ]
1869
1861
thesesubclassnum [ii ] = isub
@@ -1881,13 +1873,13 @@ def template_star(metadata, newloglam, newflux, newivar, slist, outfile,
1881
1873
# Plot spectra
1882
1874
#
1883
1875
plotflux = thisflux / thisflux .max ()
1884
- ax .plot (10.0 ** newloglam , plotflux ,
1885
- "{0}-" .format (colorvec [isub % len (colorvec )]),
1886
- linewidth = 1 )
1876
+ _ = ax .plot (10.0 ** newloglam , plotflux ,
1877
+ "{0}-" .format (colorvec [isub % len (colorvec )]),
1878
+ linewidth = 1 )
1887
1879
if isub == 0 :
1888
- ax .set_xlabel (r'Wavelength [$\AA$]' )
1889
- ax .set_ylabel ('Flux [arbitrary units]' )
1890
- ax .set_title ('STAR {0}: Eigenspectra Reconstructions' .format (c ))
1880
+ _ = ax .set_xlabel (r'Wavelength [$\AA$]' )
1881
+ _ = ax .set_ylabel ('Flux [arbitrary units]' )
1882
+ _ = ax .set_title ('STAR {0}: Eigenspectra Reconstructions' .format (c ))
1891
1883
_ = ax .text (10.0 ** newloglam [- 1 ], plotflux [- 1 ],
1892
1884
subclasslist [isub ],
1893
1885
horizontalalignment = 'right' , verticalalignment = 'center' ,
@@ -1919,7 +1911,8 @@ def template_input_main(): # pragma: no cover
1919
1911
#
1920
1912
import sys
1921
1913
from argparse import ArgumentParser
1922
-
1914
+ import matplotlib
1915
+ matplotlib .use ("Agg" )
1923
1916
# Get home directory in platform-independent way
1924
1917
home_dir = os .path .expanduser ('~' )
1925
1918
#
0 commit comments