-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspecPlot.py
More file actions
143 lines (120 loc) · 4.19 KB
/
specPlot.py
File metadata and controls
143 lines (120 loc) · 4.19 KB
1
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'''Spectroscopy Package Plotting Module
Author: Alex M. Pronschinske
List of classes: -none-
List of functions:
format_default
plot_density
tight_scale
'''
# third-party modules
import matplotlib
import matplotlib.pyplot as plt
#===============================================================================
def pltformat_basic(ax):
'''Format plot: basic style
Formatting adjustments include: axes thickness, axes z-position,
major & minor tick dimensions, grid appearance, and application of
matplotlib.tight_layout() function.
Args:
ax (Axes): matplotlib.Axis object for the plot to be formated
'''
ax.set_axisbelow(True)
for child in ax.get_children():
if isinstance(child, matplotlib.spines.Spine):
child.set_linewidth(2)
# END for
ax.tick_params(which='major', labelsize=18, length=7, width=3)
ax.tick_params(which='minor', length=5, width=3)
#ax.set_xlabel(size=22)
#ax.set_ylabel(size=22)
ax.grid(color=(0.8,0.8,0.8), linewidth=1.5)
ax.minorticks_on()
ax.figure.tight_layout()
# END pltformat_basic
#===============================================================================
def format_default(ax):
'''DEPRECIATED, use pltformat_basic instead'''
print (
'Warning "format_default" will be depreciated, '
+ 'use "pltformat_basic" instead'
)
pltformat_basic(ax)
# END format_default
#===============================================================================
def plot_density(bundle, ax=None, mkrcolor='blue', alpha=None, **extra_kwargs):
'''Create curve density plot
This function will plot all of the curves in the input bundle on the same
axis where each is semi-transparent so that where curves overlap the color
appears darker
Args:
bundle (SpecBundle): Group of data to be plotted
ax = None (Axis): Axis on which to add the density plot, if none is
given then one will be made and returned
mkrcolor = 'blue' (str): curve color
alpha = None (float): The Opacity value of each curve, if none is given
if will be set to 0.1 for bundle of 100 or fewer
curves and set up 8/bundle.N if their are more
than 100 curves
**extra_kwargs: Any extra keywords will be passed onto the ax.plot()
function call
Returns:
(Axis) The newly created plot
Example:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
plot_density(bun, ax)
ax2 = plot_density(bun)
'''
if ax is None:
fig = plt.figure()
ax = fig.add_subplot(111)
# END if
if mkrcolor == 'blue':
mkrcolor = (0.5,0.6,1.0)
elif mkrcolor == 'red':
mkrcolor = (1.0,0.5,0.5)
# END if
if alpha is None:
alpha = 0.1
if bundle.N >= 100: alpha = 8.0/bundle.N
# END if
X = bundle.X
for Y in bundle:
ax.plot(
X, Y, '-', color=mkrcolor, alpha=alpha, linewidth=2, markersize=5,
markeredgewidth=0, **extra_kwargs
)
return ax
# END plot_density
#===============================================================================
def tight_scale(ax, fit_amount=0.99, growth_factor=0.20):
'''Tighten the y-axis scaling
This function will adjust the y-axis limits of a plot so that the specified
fit_amount fraction will be within the limits then grow the size of the
vertical frame by the growth_factor fractional amount. For example the
default values of fit_amount=0.99 and growth_factor=0.20 will first set the
y-limits to accommodate 99% of the data, then make the frame +20% bigger
(i.e. 120% of it's adjusted size).
Args:
ax (Axis): Axis which will be re-scaled
fit_amount = 0.99 (float):
growth_factor = 0.20 (float):
Returns:
(tuple) (low_bnd, upp_bnd) New upper and lower bounds of the y-axis.
'''
# Put all of the y-points of all of the curves into one array and sort them
all_curves = ax.get_lines()
ypoints = []
for curve in all_curves: ypoints.extend( curve.get_ydata() )
ypoints.sort()
# Scale the y-limits
oneside_lim = int((1-fit_amount)*len(ypoints))/2
y_window = abs(ypoints[oneside_lim] - ypoints[-oneside_lim])
low_bnd = ypoints[oneside_lim] - growth_factor*y_window/2
upp_bnd = ypoints[-oneside_lim] + growth_factor*y_window/2
# Set the new y-limits
ax.set_ylim(low_bnd, upp_bnd)
# Return the new y-limits in case the user wants them
return low_bnd, upp_bnd
# END tight_scale