File size: 7,160 Bytes
b2e2343 | 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | # plottools.py
"""
Collection of classes to help create plots.
"""
import matplotlib.pyplot as plt
class Plot(object):
"""
Base class for a plot.
:param title: Plot title. Optional.
:type title: str
"""
def __init__(self, title=None):
self.fig = plt.figure()
self.axplot = self.fig.add_subplot(1, 1, 1)
for item in [self.fig, self.axplot]:
item.patch.set_visible(False)
if title is not None:
self.set_title(title)
def set_title(self, title):
"""
Add a title to the plot.
:param title: Title to assign to the plot.
:type title: str
"""
self.axplot.set_title(title)
def set_axis_label(self, label, axis):
"""
Add an axis label to the plot.
:param label: Label to assign to axis.
:type label: str
:param axis: Name of the axis, x or y
:type axis: str
"""
if axis == 'x':
self.axplot.set_xlabel(label)
elif axis == 'y':
self.axplot.set_ylabel(label)
else:
errmsg = 'Valid axis names are x and y.'
raise ValueError(errmsg)
class SpPlot(Plot):
"""
Class to create and customize a spectrum plot. Subclasses Plot.
:param title: Title to assign to the plot.
:type title: str
"""
def __init__(self, title=None):
Plot.__init__(self, title)
def plot_spectrum(self, sp1d, title=None, color='k'):
"""
Plot the spectrum (counts vs wavelength) with title and axis labels.
:param sp1d: A Spectrum instance.
:type sp1d: Spectrum object.
:param title: Title for the plot. Optional.
:type title: str
:param color: Colour of the line
:type color: str
"""
if title is not None:
self.set_title(title)
self.set_axis_label(''.join(['Wavelength [', sp1d.wunit.name, ']']),
'x')
self.set_axis_label('Counts', 'y')
self.axplot.plot(sp1d.wlen, sp1d.counts, color)
self.axplot.axis('tight')
self.fig.canvas.draw()
return
def adjust_ylimits(self, ylim1, ylim2):
"""
Adjust the lower and upper bounds to the y-axis.
:param ylim1: Lower limit for the y-axis.
:type ylim1: float
:param ylim2: Upper limit for the y-axis.
:type ylim2: float
"""
self.axplot.set_ylim(ylim1, ylim2)
self.fig.canvas.draw()
return
def erase_plot(self, line_position=0):
"""
Erase the spectrum but not the box and axes.
:param line_position: Position on the stack of the spectrum to
erase.
:type line_position: int
"""
self.axplot.lines.pop(line_position).remove
self.fig.canvas.draw()
return
def annotate_lines(self, lines):
"""
Annotate the plot with spectra line identifications.
:param lines: The line list to add to the plot. The lines are stored
in a list of tuples with (obswlen, name), where obswlen is a float
and name is a string.
:type lines: list of tuples
"""
# lines is list of tuple (obswlen, name)
(xlow, xhigh) = self.axplot.get_xlim()
(_, yhigh) = self.axplot.get_ylim()
ypos = yhigh * 0.8
ydelta = yhigh * 0.05
i = 0
for line in lines:
if xlow < line[0] < xhigh:
self.axplot.text(line[0], ypos-(i*ydelta)-ydelta*1.25, '|',
horizontalalignment='center',
verticalalignment='center', fontsize=10)
self.axplot.text(line[0], ypos-(i*ydelta), line[1],
horizontalalignment='center',
verticalalignment='center', fontsize=10)
i += 1
self.fig.canvas.draw()
return
# def draw_band_limits(self):
# return
def write_png(self, output_name):
"""
Write the figure to a PNG file.
:param output_name: Name of the output file.
??Is the extension .png required or is it added automatically??
:type output_name: str
"""
self.fig.savefig(output_name)
return
class MultiPlot(object):
def __init__(self, nrows=1, ncols=1, title=None):
self.nrows = nrows
self.ncols = ncols
if title is not None:
self.title = title
self.plots_titles = None
self.plots_labels = None
self.plots_data = None
self.pdf = None
def add_data(self, plots_data, plots_labels=None):
"""
Parameters
----------
plot_data : list of tuple
tuples of x and y data, one tuple per plot
"""
self.plots_data = plots_data
if plots_labels is not None:
self.add_labels(plots_labels)
def add_labels(self, plots_labels):
self.plots_labels = plots_labels
def add_titles(self, plots_titles):
self.plots_titles = plots_titles
def set_size(self, width, height):
self.width = width
self.height = height
def plot(self, png=False, filename='multiplot.png', pdf=None):
self.fig, self.axs = plt.subplots(self.nrows, self.ncols)
self.fig.suptitle(self.title)
self.fig.set_size_inches(self.width, self.height)
nplots = len(self.plots_data)
plotid = 0
for axrow in self.axs:
for ax in axrow:
xlabel = None
ylabel = None
title = None
if plotid == nplots:
break # fewer plots then slots
x = self.plots_data[plotid][0]
y = self.plots_data[plotid][1]
if len(self.plots_labels) == 1:
xlabel = self.plots_labels[0][0]
ylabel = self.plots_labels[0][1]
elif self.plots_labels is not None:
xlabel = self.plots_labels[plotid][0]
ylabel = self.plots_labels[plotid][1]
if self.plots_titles is not None:
title = self.plots_titles[plotid]
ax.plot(x, y)
ax.set_title(title)
ax.set(xlabel=xlabel, ylabel=ylabel)
plotid += 1
self.fig.tight_layout(rect=[0, 0.03, 1, 0.95])
if pdf:
pdf.savefig()
if png:
plt.savefig(filename)
else:
plt.show()
def save_plot(self, filename='multiplot.png'):
self.plot(save=True, filename=filename)
def close(self):
plt.close()
"""
import plottools
from importlib import reload
import numpy as np
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
m = plottools.MultiPlot(2,2,'test')
plot_data = [(x,y), (x,-y), (-x, y), (-x, -y)]
plot_labels = [('x axis', 'y axis'),('x axis', '-y axis'),('-x axis', 'y axis'),('-x axis', '-y axis')]
m.add_data(plot_data, plot_labels)
m.plot()
""" |