Skip to content

Commit

Permalink
Fixing an issue where line and scatter plots weren't respecting the y…
Browse files Browse the repository at this point in the history
…axis keyword argument. Added a test and reference plot.
  • Loading branch information
duncanwp committed Nov 28, 2016
1 parent 9bcd2a1 commit 33cb0f0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cis/plotting/genericplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@six.add_metaclass(ABCMeta)
class APlot(object):

def __init__(self, packed_data, xaxis, yaxis, color=None,
def __init__(self, packed_data, xaxis, yaxis=None, color=None,
edgecolor=None, itemstyle=None, itemwidth=None, label=None, *mplargs, **mplkwargs):
"""
Abstract base class for all CIS plot types. Includes some common kwargs.
Expand Down
32 changes: 21 additions & 11 deletions cis/plotting/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ def basic_plot(data, how=None, ax=None, xaxis=None, yaxis=None, projection=None,
from cis.data_io.common_data import CommonData
from cis.data_io.gridded_data import GriddedData
from cis.utils import squeeze
from cis.plotting.genericplot import GenericPlot

# Remove any extra gridded data dimensions
if isinstance(data, GriddedData):
Expand All @@ -346,23 +347,32 @@ def basic_plot(data, how=None, ax=None, xaxis=None, yaxis=None, projection=None,
"histogram2d or use a Coord (or None) for the xaxis argument.")
else:
how = 'comparativescatter'
xaxis_coord = xaxis
else:
xaxis = get_axis(data, 'X', xaxis)
xaxis_coord = get_axis(data, 'X', xaxis)

yaxis = get_axis(data, 'Y', yaxis)
yaxis_coord = get_axis(data, 'Y', yaxis)

how = how or data._get_default_plot_type(xaxis.standard_name == 'longitude'
and yaxis.standard_name == 'latitude')

# Set a nice default label
label = get_label(data) if label is None else label
how = how or data._get_default_plot_type(xaxis_coord.standard_name == 'longitude'
and yaxis_coord.standard_name == 'latitude')

try:
plot = plot_types[how](data, xaxis=xaxis, yaxis=yaxis, label=label,
*args, **kwargs)
plt_type = plot_types[how]
except KeyError:
raise ValueError("Invalid plot type, must be one of: {}".format(plot_types.keys()))

if issubclass(plt_type, GenericPlot) and yaxis is not None:
# If we have a line or scatter plot and the user has specified a y-axis, then this should be the data
data = yaxis_coord
else:
# Otherwise, it's just another kwarg
kwargs['yaxis'] = yaxis_coord

# Set a nice default label
label = get_label(data) if label is None else label

plot = plot_types[how](data, xaxis=xaxis_coord, label=label, *args, **kwargs)

subplot_kwargs = {}
if plot.is_map():
if projection is None:
Expand All @@ -381,10 +391,10 @@ def basic_plot(data, how=None, ax=None, xaxis=None, yaxis=None, projection=None,
plot(ax)

# Any post-processing
if xaxis.standard_name == 'time' and how not in ['comparativescatter', 'histogram2d', 'histogram']:
if xaxis_coord.standard_name == 'time' and how not in ['comparativescatter', 'histogram2d', 'histogram']:
set_x_axis_as_time(ax)

if yaxis.standard_name == 'air_pressure':
if yaxis_coord.standard_name == 'air_pressure':
ax.invert_yaxis()

return plot, ax
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions cis/test/plot_tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,3 +982,10 @@ def test_mercator_projection(self):
ax.bluemarble()

self.check_graphic()

def test_can_specify_yaxis_altitude(self):
from cis import read_data
d = read_data(valid_GASSP_aeroplane_filename, valid_GASSP_aeroplane_variable)
d.plot(yaxis='altitude')

self.check_graphic()
1 change: 1 addition & 0 deletions doc/whats_new_1.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ Bugs fixed
CIS 1.5.1 fixes
===============
* Minor fix in interpreting units when reading some NetCDF data in Python 2
* Fixed an issue where line and scatter plots weren't respecting the yaxis keyword

0 comments on commit 33cb0f0

Please sign in to comment.