Skip to content

Commit

Permalink
Update HeatMap and QuadMesh test to pass on Matplotlib 3.8 (#5924)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxbro authored Oct 16, 2023
1 parent 3da3d75 commit eccd230
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 49 deletions.
17 changes: 2 additions & 15 deletions examples/gallery/demos/bokeh/bachelors_degrees_by_gender.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,15 @@
"## Define data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This example uses the matplotlib sample data that conda users can fetch using:\n",
"\n",
"```\n",
"conda install -c conda-forge mpl_sample_data\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from matplotlib.cbook import get_sample_data\n",
"\n",
"fname = get_sample_data('percent_bachelors_degrees_women_usa.csv')\n",
"gender_degree_data = pd.read_csv(fname)\n",
"gender_degree_data = pd.read_csv(\"https://datasets.holoviz.org/bachelor_women/v1/percent_bachelors_degrees_women_usa.csv\")\n",
"\n",
"title = ('Percentage of Bachelor\\'s degrees conferred to women in '\n",
" 'the U.S.A. by major (1970-2011)\\n')\n",
Expand Down Expand Up @@ -130,5 +117,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
2 changes: 1 addition & 1 deletion examples/gallery/demos/bokeh/bars_economic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
4 changes: 2 additions & 2 deletions examples/gallery/demos/bokeh/topographic_hillshading.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"from matplotlib.cbook import get_sample_data\n",
"from matplotlib.colors import LightSource\n",
"\n",
"dem = np.load(get_sample_data('jacksboro_fault_dem.npz'))\n",
"dem = get_sample_data('jacksboro_fault_dem.npz')\n",
"z = dem['elevation']\n",
"\n",
"dx, dy = dem['dx'], dem['dy']\n",
Expand Down Expand Up @@ -89,5 +89,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@
"outputs": [],
"source": [
"import pandas as pd\n",
"from matplotlib.cbook import get_sample_data\n",
"\n",
"fname = get_sample_data('percent_bachelors_degrees_women_usa.csv')\n",
"gender_degree_data = pd.read_csv(fname)\n",
"gender_degree_data = pd.read_csv(\"https://datasets.holoviz.org/bachelor_women/v1/percent_bachelors_degrees_women_usa.csv\")\n",
"\n",
"title = ('Percentage of Bachelor\\'s degrees conferred to women in '\n",
" 'the U.S.A. by major (1970-2011)\\n')\n",
Expand Down Expand Up @@ -123,5 +121,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"from matplotlib.cbook import get_sample_data\n",
"from matplotlib.colors import LightSource\n",
"\n",
"dem = np.load(get_sample_data('jacksboro_fault_dem.npz'))\n",
"dem = get_sample_data('jacksboro_fault_dem.npz')\n",
"z = dem['elevation']\n",
"\n",
"dx, dy = dem['dx'], dem['dy']\n",
Expand Down Expand Up @@ -92,5 +92,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
22 changes: 16 additions & 6 deletions holoviews/tests/plotting/matplotlib/test_heatmapplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from holoviews.element import HeatMap, Image

from .test_plot import TestMPLPlot, mpl_renderer
from .test_plot import TestMPLPlot, mpl38, mpl_renderer


class TestHeatMapPlot(TestMPLPlot):
Expand All @@ -12,25 +12,35 @@ def test_heatmap_invert_axes(self):
hm = HeatMap(Image(arr)).opts(invert_axes=True)
plot = mpl_renderer.get_plot(hm)
artist = plot.handles['artist']
self.assertEqual(artist.get_array().data, arr.T[::-1].flatten())
if mpl38:
np.testing.assert_equal(artist.get_array().data, arr.T[::-1])
else:
np.testing.assert_equal(artist.get_array().data, arr.T[::-1].flatten())

def test_heatmap_extents(self):
hmap = HeatMap([('A', 50, 1), ('B', 2, 2), ('C', 50, 1)])
plot = mpl_renderer.get_plot(hmap)
self.assertEqual(plot.get_extents(hmap, {}), (-.5, -22, 2.5, 74))
assert plot.get_extents(hmap, {}) == (-.5, -22, 2.5, 74)

def test_heatmap_invert_xaxis(self):
hmap = HeatMap([('A',1, 1), ('B', 2, 2)]).opts(invert_xaxis=True)
plot = mpl_renderer.get_plot(hmap)
array = plot.handles['artist'].get_array()
expected = np.array([1, np.inf, np.inf, 2])
if mpl38:
expected = np.array([[1, np.inf], [np.inf, 2]])
else:
expected = np.array([1, np.inf, np.inf, 2])
masked = np.ma.array(expected, mask=np.logical_not(np.isfinite(expected)))
self.assertEqual(array, masked)
np.testing.assert_equal(array, masked)

def test_heatmap_invert_yaxis(self):
hmap = HeatMap([('A',1, 1), ('B', 2, 2)]).opts(invert_yaxis=True)
plot = mpl_renderer.get_plot(hmap)
array = plot.handles['artist'].get_array()
expected = np.array([1, np.inf, np.inf, 2])
if mpl38:
expected = np.array([[1, np.inf], [np.inf, 2]])
else:
expected = np.array([1, np.inf, np.inf, 2])
masked = np.ma.array(expected, mask=np.logical_not(np.isfinite(expected)))
self.assertEqual(array, masked)
np.testing.assert_equal(array, masked)
3 changes: 3 additions & 0 deletions holoviews/tests/plotting/matplotlib/test_plot.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import matplotlib.pyplot as plt
import pyviz_comms as comms
from packaging.version import Version
from param import concrete_descendents

from holoviews.core.options import Store
from holoviews.element.comparison import ComparisonTestCase
from holoviews.plotting.mpl import mpl_version
from holoviews.plotting.mpl.element import ElementPlot

from .. import option_intersections

mpl_renderer = Store.renderers['matplotlib']
mpl38 = mpl_version >= Version("3.8")


class TestPlotDefinitions(ComparisonTestCase):
Expand Down
40 changes: 22 additions & 18 deletions holoviews/tests/plotting/matplotlib/test_quadmeshplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,53 @@

from holoviews.element import Dataset, Image, QuadMesh

from .test_plot import TestMPLPlot, mpl_renderer
from .test_plot import TestMPLPlot, mpl38, mpl_renderer


class TestQuadMeshPlot(TestMPLPlot):

def test_quadmesh_invert_axes(self):
arr = np.array([[0, 1, 2], [3, 4, 5]])
arr = np.array([[0, 1, 2], [3, 4, 5]])
qmesh = QuadMesh(Image(arr)).opts(invert_axes=True)
plot = mpl_renderer.get_plot(qmesh)
artist = plot.handles['artist']
self.assertEqual(artist.get_array().data, arr.T[:, ::-1].flatten())
if mpl38:
np.testing.assert_equal(artist.get_array().data, arr[::-1].T)
else:
np.testing.assert_equal(artist.get_array().data, arr.T[::-1].flatten())

def test_quadmesh_nodata(self):
arr = np.array([[0, 1, 2], [3, 4, 5]])
arr = np.array([[0, 1, 2], [3, 4, 5]])
qmesh = QuadMesh(Image(arr)).opts(nodata=0)
plot = mpl_renderer.get_plot(qmesh)
artist = plot.handles['artist']
self.assertEqual(artist.get_array().data,
np.array([3, 4, 5, np.nan, 1, 2]))
if mpl38:
expected = np.array([[3, 4, 5], [np.nan, 1, 2]])
else:
expected = np.array([3, 4, 5, np.nan, 1, 2])

np.testing.assert_equal(artist.get_array().data, expected)

def test_quadmesh_nodata_uint(self):
arr = np.array([[0, 1, 2], [3, 4, 5]], dtype='uint32')
arr = np.array([[0, 1, 2], [3, 4, 5]], dtype='uint32')
qmesh = QuadMesh(Image(arr)).opts(nodata=0)
plot = mpl_renderer.get_plot(qmesh)
artist = plot.handles['artist']
self.assertEqual(artist.get_array().data,
np.array([3, 4, 5, np.nan, 1, 2]))
if mpl38:
expected = np.array([[3, 4, 5], [np.nan, 1, 2]])
else:
expected = np.array([3, 4, 5, np.nan, 1, 2])
np.testing.assert_equal(artist.get_array().data, expected)

def test_quadmesh_update_cbar(self):
xs = ys = np.linspace(0, 6, 10)
zs = np.linspace(1, 2, 5)
XS, YS, ZS = np.meshgrid(xs, ys, zs)
XS, _YS, ZS = np.meshgrid(xs, ys, zs)
values = np.sin(XS) * ZS
ds = Dataset((xs, ys, zs, values.T), ['x', 'y', 'z'], 'values')
hmap = ds.to(QuadMesh).opts(colorbar=True, framewise=True)
plot = mpl_renderer.get_plot(hmap)
cbar = plot.handles['cbar']
self.assertEqual(
np.allclose([cbar.vmin, cbar.vmax], [-0.9989549170979283, 0.9719379013633128]),
True
)
np.testing.assert_allclose([cbar.vmin, cbar.vmax], [-0.9989549170979283, 0.9719379013633128])
plot.update(3)
self.assertEqual(
np.allclose([cbar.vmin, cbar.vmax], [-1.7481711049213744, 1.7008913273857975]),
True
)
np.testing.assert_allclose([cbar.vmin, cbar.vmax], [-1.7481711049213744, 1.7008913273857975])
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
'pytest-cov',
'pytest-xdist',
'flaky',
'matplotlib >=3, <3.8', # 3.8 breaks tests
'matplotlib >=3',
'nbconvert',
'bokeh >=3.1',
'pillow',
Expand Down

0 comments on commit eccd230

Please sign in to comment.