Skip to content

Commit

Permalink
Add box and whisker plot capability (#136)
Browse files Browse the repository at this point in the history
* add box and whisker plot capability

* forgot import and addition to call function
  • Loading branch information
kevindougherty-noaa authored Jul 14, 2023
1 parent 3bab147 commit d136690
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/emcpy/plots/create_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ def create_figure(self):
'bar_plot': self._barplot,
'horizontal_bar': self._hbar,
'skewt': self._skewt,
'boxandwhisker': self._boxandwhisker,
'map_scatter': self._map_scatter,
'map_gridded': self._map_gridded,
'map_contour': self._map_contour,
Expand Down Expand Up @@ -676,6 +677,15 @@ def _hbar(self, plotobj, ax):

ax.barh(plotobj.y, plotobj.width, **inputs)

def _boxandwhisker(self, plotobj, ax):
"""
Uses BoxandWhiskerPlot object to plot on axis.
"""
skipvars = ['plottype', 'data']
inputs = self._get_inputs_dict(skipvars, plotobj)

ax.boxplot(plotobj.data, **inputs)

def _get_inputs_dict(self, skipvars, plotobj):
"""
Creates dictionary for plot inputs. Skips variables
Expand Down
31 changes: 31 additions & 0 deletions src/emcpy/plots/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,34 @@ def __init__(self, x, y):
self.markersize = None
self.alpha = None
self.label = None


class BoxandWhiskerPlot:

def __init__(self, data):
"""
Constructor to create a Box and Whisker
plot.
Args:
data : (array type)
"""
super().__init__()
self.plottype = 'boxandwhisker'

self.data = data

self.notch = False
self.sym = None
self.vert = True
self.whis = 1.5
self.bootstrap = None
self.usermedians = None
self.conf_intervals = None
self.positions = None
self.widths = None
self.patch_artist = False
self.labels = None
self.manage_ticks = True
self.autorange = False
self.meanline = False
self.zorder = None
33 changes: 32 additions & 1 deletion src/tests/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from emcpy.plots.plots import LinePlot, VerticalLine,\
Histogram, Density, Scatter, HorizontalLine, BarPlot, \
GriddedPlot, ContourPlot, FilledContourPlot, HorizontalBar, \
HorizontalSpan, SkewT
BoxandWhiskerPlot, HorizontalSpan, SkewT
from emcpy.plots.create_plots import CreatePlot, CreateFigure


Expand Down Expand Up @@ -294,6 +294,25 @@ def test_contours_plot():
fig.save_figure('test_contour_and_contourf_plot.png')


def test_box_and_whisker_plot():
# Create box and whisker plot

data = _getBoxPlotData()

bwp = BoxandWhiskerPlot(data)

plot1 = CreatePlot()
plot1.plot_layers = [bwp]
plot1.add_xlabel(xlabel='X Axis Label')
plot1.add_ylabel(ylabel='Y Axis Label')
plot1.add_title('Test Box and Whisker Plot')

fig = CreateFigure()
fig.plot_list = [plot1]
fig.create_figure()
fig.save_figure('test_box_and_whisker_plot.png')


def test_horizontal_bar_plot():
# Create horizontal bar plot

Expand Down Expand Up @@ -591,6 +610,17 @@ def _getContourfData():
return x, y, z


def _getBoxPlotData():
# generate test data for box and whisker plot

# Fixing random state for reproducibility
np.random.seed(19680801)

data = [np.random.normal(0, std, 100) for std in range(6, 10)]

return data


def _getSkewTData():
# use data for skew-t log-p plot
from io import StringIO
Expand Down Expand Up @@ -687,6 +717,7 @@ def main():
test_bar_plot()
test_gridded_plot()
test_contours_plot()
test_box_and_whisker_plot()
test_horizontal_bar_plot()
test_multi_subplot()
test_HorizontalSpan()
Expand Down

0 comments on commit d136690

Please sign in to comment.