Skip to content

Commit

Permalink
Changes for matplotlib3.8 (handley-lab#339)
Browse files Browse the repository at this point in the history
* first pass at fixing next color (could be wrong color)

* kde_2d seems to now be made up of 4 collections, instead of 5. They are also different (remember to attach screenshot to PR

* use set_cmap. Side-effect is now only two collections

* work around QuadContourSet.tcolors deprecation:

* matplotlib now plots >=mincnt, not >mincnt

* version bump

* just test for more than 0 collections

* version bump

* unlimit matplotlib version from merge

* deprecated solution to contour test :(

* finally clicked what's going on - different tests for mpl>3.8

* clean up unnecessary `pass`

---------

Co-authored-by: Lukas Hergt <[email protected]>
  • Loading branch information
AdamOrmondroyd and lukashergt authored Oct 1, 2023
1 parent 8563cae commit 434d97e
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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/

Expand Down
2 changes: 1 addition & 1 deletion anesthetic/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.4.0'
__version__ = '2.4.1'
20 changes: 9 additions & 11 deletions anesthetic/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)))

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions anesthetic/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 24 additions & 10 deletions tests/test_plot.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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():
Expand Down
8 changes: 4 additions & 4 deletions tests/test_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -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
Expand Down

0 comments on commit 434d97e

Please sign in to comment.