diff --git a/README.rst b/README.rst index 3e58c60f..844b6e7a 100644 --- a/README.rst +++ b/README.rst @@ -2,7 +2,7 @@ anesthetic: nested sampling post-processing =========================================== :Authors: Will Handley and Lukas Hergt -:Version: 2.4.0 +:Version: 2.4.1 :Homepage: https://github.com/handley-lab/anesthetic :Documentation: http://anesthetic.readthedocs.io/ diff --git a/anesthetic/_version.py b/anesthetic/_version.py index ba9b9133..5cd7abf8 100644 --- a/anesthetic/_version.py +++ b/anesthetic/_version.py @@ -1 +1 @@ -__version__ = '2.4.0' +__version__ = '2.4.1' diff --git a/anesthetic/plot.py b/anesthetic/plot.py index c4ca0872..b25f60d9 100644 --- a/anesthetic/plot.py +++ b/anesthetic/plot.py @@ -745,7 +745,7 @@ def fastkde_plot_1d(ax, data, *args, **kwargs): density = kwargs.pop('density', False) cmap = kwargs.pop('cmap', None) - color = kwargs.pop('color', (next(ax._get_lines.prop_cycler)['color'] + color = kwargs.pop('color', (ax._get_lines.get_next_color() if cmap is None else plt.get_cmap(cmap)(0.68))) facecolor = kwargs.pop('facecolor', False) @@ -861,7 +861,7 @@ def kde_plot_1d(ax, data, *args, **kwargs): density = kwargs.pop('density', False) cmap = kwargs.pop('cmap', None) - color = kwargs.pop('color', (next(ax._get_lines.prop_cycler)['color'] + color = kwargs.pop('color', (ax._get_lines.get_next_color() if cmap is None else plt.get_cmap(cmap)(0.68))) facecolor = kwargs.pop('facecolor', False) @@ -952,7 +952,7 @@ def hist_plot_1d(ax, data, *args, **kwargs): density = kwargs.get('density', False) cmap = kwargs.pop('cmap', None) - color = kwargs.pop('color', (next(ax._get_lines.prop_cycler)['color'] + color = kwargs.pop('color', (ax._get_lines.get_next_color() if cmap is None else plt.get_cmap(cmap)(0.68))) @@ -1038,7 +1038,7 @@ def fastkde_contour_plot_2d(ax, data_x, data_y, *args, **kwargs): zorder = kwargs.pop('zorder', 1) levels = kwargs.pop('levels', [0.95, 0.68]) - color = kwargs.pop('color', next(ax._get_lines.prop_cycler)['color']) + color = kwargs.pop('color', ax._get_lines.get_next_color()) facecolor = kwargs.pop('facecolor', True) edgecolor = kwargs.pop('edgecolor', None) cmap = kwargs.pop('cmap', None) @@ -1065,8 +1065,7 @@ def fastkde_contour_plot_2d(ax, data_x, data_y, *args, **kwargs): contf = ax.contourf(x[i], y[j], pdf[np.ix_(j, i)], levels, cmap=cmap, zorder=zorder, vmin=0, vmax=pdf.max(), *args, **kwargs) - for c in contf.collections: - c.set_cmap(cmap) + contf.set_cmap(cmap) ax.add_patch(plt.Rectangle((0, 0), 0, 0, lw=2, label=label, fc=cmap(0.999), ec=cmap(0.32))) cmap = None @@ -1158,7 +1157,7 @@ def kde_contour_plot_2d(ax, data_x, data_y, *args, **kwargs): zorder = kwargs.pop('zorder', 1) levels = kwargs.pop('levels', [0.95, 0.68]) - color = kwargs.pop('color', next(ax._get_lines.prop_cycler)['color']) + color = kwargs.pop('color', ax._get_lines.get_next_color()) facecolor = kwargs.pop('facecolor', True) edgecolor = kwargs.pop('edgecolor', None) cmap = kwargs.pop('cmap', None) @@ -1195,8 +1194,7 @@ def kde_contour_plot_2d(ax, data_x, data_y, *args, **kwargs): linewidths = kwargs.pop('linewidths', 0.5) contf = ax.contourf(X, Y, P, levels=levels, cmap=cmap, zorder=zorder, vmin=0, vmax=P.max(), *args, **kwargs) - for c in contf.collections: - c.set_cmap(cmap) + contf.set_cmap(cmap) ax.add_patch(plt.Rectangle((0, 0), 0, 0, lw=2, label=label, fc=cmap(0.999), ec=cmap(0.32))) cmap = None @@ -1258,7 +1256,7 @@ def hist_plot_2d(ax, data_x, data_y, *args, **kwargs): label = kwargs.pop('label', None) levels = kwargs.pop('levels', None) - color = kwargs.pop('color', next(ax._get_lines.prop_cycler)['color']) + color = kwargs.pop('color', ax._get_lines.get_next_color()) cmap = kwargs.pop('cmap', basic_cmap(color)) q = kwargs.pop('q', 5) @@ -1345,7 +1343,7 @@ def scatter_plot_2d(ax, data_x, data_y, *args, **kwargs): markersize = kwargs.pop('markersize', 1) cmap = kwargs.pop('cmap', None) - color = kwargs.pop('color', (next(ax._get_lines.prop_cycler)['color'] + color = kwargs.pop('color', (ax._get_lines.get_next_color() if cmap is None else cmap(0.68))) kwargs.pop('q', None) diff --git a/anesthetic/plotting/_matplotlib/core.py b/anesthetic/plotting/_matplotlib/core.py index f0ca1e8f..0b156dac 100644 --- a/anesthetic/plotting/_matplotlib/core.py +++ b/anesthetic/plotting/_matplotlib/core.py @@ -131,6 +131,8 @@ def __init__(self, data, x, y, C=None, **kwargs) -> None: C = '__weights' data[C] = data.get_weights() kwargs['reduce_C_function'] = np.sum + if 'mincnt' not in kwargs: + kwargs['mincnt'] = np.finfo(np.float64).tiny super().__init__(data, x, y, C=C, **kwargs) diff --git a/pyproject.toml b/pyproject.toml index 4eb958ae..d8810c5c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,7 +34,7 @@ dependencies = [ "scipy", "numpy", "pandas>=2.0.0", - "matplotlib>=3.6.1,<3.8.0", + "matplotlib>=3.6.1", ] classifiers = [ "Programming Language :: Python :: 3", diff --git a/tests/test_plot.py b/tests/test_plot.py index 8ab6bd0a..47db51cd 100644 --- a/tests/test_plot.py +++ b/tests/test_plot.py @@ -1,6 +1,8 @@ import anesthetic.examples._matplotlib_agg # noqa: F401 +from packaging import version import pytest import numpy as np +import matplotlib import matplotlib.pyplot as plt import matplotlib.gridspec as gs from anesthetic.plot import (make_1d_axes, make_2d_axes, kde_plot_1d, @@ -612,8 +614,12 @@ def test_contour_plot_2d(contour_plot_2d): cf1, ct1 = contour_plot_2d(ax, data_x, data_y, facecolor='C2') cf2, ct2 = contour_plot_2d(ax, data_x, data_y, fc='None', cmap=cmap) # filled `contourf` and unfilled `contour` colors are the same: - assert cf1.tcolors[0] == ct2.tcolors[0] - assert cf1.tcolors[1] == ct2.tcolors[1] + # tcolors deprecated in matplotlib 3.8 + cf1_tcolors = [tuple(rgba) for rgba in cf1.to_rgba(cf1.cvalues, cf1.alpha)] + ct2_tcolors = [tuple(rgba) for rgba in ct2.to_rgba(ct2.cvalues, ct2.alpha)] + + assert cf1_tcolors[0] == ct2_tcolors[0] + assert cf1_tcolors[1] == ct2_tcolors[1] cf, ct = contour_plot_2d(ax, data_x, data_y, edgecolor='C0') assert ct.colors == 'C0' cf, ct = contour_plot_2d(ax, data_x, data_y, ec='C0', cmap=plt.cm.Reds) @@ -698,14 +704,22 @@ def test_contour_plot_2d_levels(contour_plot_2d, levels): contour_plot_2d(ax2, x, y, levels=levels, cmap=cmap, fc=None) # assert that color between filled and unfilled contours matches - # first level - color1 = ax1.collections[0].get_facecolor() # filled face color - color2 = ax2.collections[0].get_edgecolor() # unfilled line color - assert_array_equal(color1, color2) - # last level - color1 = ax1.collections[len(levels)-1].get_facecolor() - color2 = ax2.collections[len(levels)-1].get_edgecolor() - assert_array_equal(color1, color2) + if version.parse(matplotlib.__version__) >= version.parse('3.8.0'): + color1 = ax1.collections[0].get_facecolor() # filled face color + color2 = ax2.collections[0].get_edgecolor() # unfilled line color + # first level + assert_array_equal(color1[0], color2[0]) + # last level + assert_array_equal(color1[len(levels)-1], color2[len(levels)-1]) + else: + # first level + color1 = ax1.collections[0].get_facecolor() # filled face color + color2 = ax2.collections[0].get_edgecolor() # unfilled line color + assert_array_equal(color1, color2) + # last level + color1 = ax1.collections[len(levels)-1].get_facecolor() + color2 = ax2.collections[len(levels)-1].get_edgecolor() + assert_array_equal(color1, color2) def test_scatter_plot_2d(): diff --git a/tests/test_samples.py b/tests/test_samples.py index 281833a2..42263538 100644 --- a/tests/test_samples.py +++ b/tests/test_samples.py @@ -1270,7 +1270,7 @@ def test_samples_dot_plot(): assert len(axes) == 2 axes = samples.plot.kde_2d('x0', 'x1') - assert len(axes.collections) == 5 + assert len(axes.collections) > 0 assert axes.get_xlabel() == '$x_0$' assert axes.get_ylabel() == '$x_1$' axes = samples.plot.hist_2d('x1', 'x0') @@ -1293,7 +1293,7 @@ def test_samples_dot_plot(): assert axes.get_xlim()[1] < 0.3 axes = samples.drop_labels().plot.kde_2d('x0', 'x1') - assert len(axes.collections) == 5 + assert len(axes.collections) > 0 assert axes.get_xlabel() == 'x0' assert axes.get_ylabel() == 'x1' axes = samples.drop_labels().plot.hist_2d('x1', 'x0') @@ -1308,12 +1308,12 @@ def test_samples_dot_plot(): axes = samples.plot.fastkde_2d('x0', 'x1') assert axes.get_xlabel() == '$x_0$' assert axes.get_ylabel() == '$x_1$' - assert len(axes.collections) == 5 + assert len(axes.collections) > 0 plt.close("all") axes = samples.drop_labels().plot.fastkde_2d('x0', 'x1') assert axes.get_xlabel() == 'x0' assert axes.get_ylabel() == 'x1' - assert len(axes.collections) == 5 + assert len(axes.collections) > 0 plt.close("all") axes = samples.x0.plot.fastkde_1d() assert len(axes.lines) == 1