Skip to content

Commit

Permalink
Add contour and contourf capabilities for non map plots (#135)
Browse files Browse the repository at this point in the history
* add contour and contourf plotting capabilities

* missed executing test

* imports would help

* typo

* typo

* change to FilledContourPlot()
  • Loading branch information
kevindougherty-noaa committed Jul 14, 2023
1 parent cc94d33 commit 3bab147
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/emcpy/plots/create_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ def create_figure(self):
'density': self._density,
'line_plot': self._lineplot,
'gridded_plot': self._gridded,
'contour': self._contour,
'contourf': self._contourf,
'vertical_line': self._verticalline,
'horizontal_line': self._horizontalline,
'horizontal_span': self._horizontalspan,
Expand Down Expand Up @@ -603,6 +605,32 @@ def _lineplot(self, plotobj, ax):

ax.plot(plotobj.x, plotobj.y, **inputs)

def _contour(self, plotobj, ax):
"""
Uses ContourPlot object to plot on axis.
"""
skipvars = ['plottype', 'x', 'y', 'z', 'colorbar']
inputs = self._get_inputs_dict(skipvars, plotobj)

cs = ax.contour(plotobj.x, plotobj.y,
plotobj.z, **inputs)

if plotobj.colorbar:
self.cs = cs

def _contourf(self, plotobj, ax):
"""
Use FilledContourPlot object to plot on axis.
"""
skipvars = ['plottype', 'x', 'y', 'z', 'colorbar']
inputs = self._get_inputs_dict(skipvars, plotobj)

cs = ax.contourf(plotobj.x, plotobj.y,
plotobj.z, **inputs)

if plotobj.colorbar:
self.cs = cs

def _verticalline(self, plotobj, ax):
"""
Uses VerticalLine object to plot on axis.
Expand Down
62 changes: 62 additions & 0 deletions src/emcpy/plots/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,68 @@ def __init__(self, x, y, z):
self.colorbar = True


class ContourPlot:

def __init__(self, x, y, z):
"""
Constructor for ContourPlot.
Args:
x : (array type)
y : (array type)
z : (array type)
"""
super().__init__()
self.plottype = 'contour'

self.x = x
self.y = y
self.z = z

self.corner_mask = False
self.colors = 'black'
self.alpha = None
self.cmap = None
self.norm = None
self.vmin = None
self.vmax = None
self.origin = None
self.extent = None
self.locator = None
self.extend = None
self.colorbar = False


class FilledContourPlot:

def __init__(self, x, y, z):
"""
Constructor for FilledContourPlot.
Args:
x : (array type)
y : (array type)
z : (array type)
"""
super().__init__()
self.plottype = 'contourf'

self.x = x
self.y = y
self.z = z

self.corner_mask = False
self.colors = None
self.alpha = None
self.cmap = 'viridis'
self.norm = None
self.vmin = None
self.vmax = None
self.origin = None
self.extent = None
self.locator = None
self.extend = None
self.colorbar = True


class VerticalLine:

def __init__(self, x):
Expand Down
40 changes: 39 additions & 1 deletion src/tests/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

from emcpy.plots.plots import LinePlot, VerticalLine,\
Histogram, Density, Scatter, HorizontalLine, BarPlot, \
GriddedPlot, HorizontalBar, HorizontalSpan, SkewT
GriddedPlot, ContourPlot, FilledContourPlot, HorizontalBar, \
HorizontalSpan, SkewT
from emcpy.plots.create_plots import CreatePlot, CreateFigure


Expand Down Expand Up @@ -269,6 +270,30 @@ def test_gridded_plot():
fig.save_figure('test_gridded_plot.png')


def test_contours_plot():
# Create contourf plot

x, y, z = _getContourfData()

cfp = FilledContourPlot(x, y, z)
cfp.cmap = 'Greens'

cp = ContourPlot(x, y, z)
cp.linestyles = '--'

plot1 = CreatePlot()
plot1.plot_layers = [cfp, cp]
plot1.add_xlabel(xlabel='X Axis Label')
plot1.add_ylabel(ylabel='Y Axis Label')
plot1.add_title('Test Contour and Contourf Plot')
plot1.add_colorbar(orientation='vertical')

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


def test_horizontal_bar_plot():
# Create horizontal bar plot

Expand Down Expand Up @@ -554,6 +579,18 @@ def _getGriddedData():
return x, y, z


def _getContourfData():
# generate test data for contourf plots

x = np.linspace(-3, 15, 50).reshape(1, -1)
y = np.linspace(-3, 15, 20).reshape(-1, 1)
z = np.cos(x)*2 - np.sin(y)*2

x, y = x.flatten(), y.flatten()

return x, y, z


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

0 comments on commit 3bab147

Please sign in to comment.