proplot-dev / proplot

🎨 A succinct matplotlib wrapper for making beautiful, publication-quality graphics
https://proplot.readthedocs.io
MIT License
1.07k stars 96 forks source link

Bug: contour does not work for 2 levels #407

Closed syrte closed 1 year ago

syrte commented 1 year ago

Description

contour does not work for 2 levels in proplot.

Steps to reproduce

A "Minimal, Complete and Verifiable Example" will make it much easier for maintainers to help you.

import proplot as pplt
from scipy.stats import multivariate_normal
import numpy as np

fig, ax = pplt.subplot()
x, y = np.mgrid[-2:2:50j, -2:2:50j]
z = multivariate_normal([0, 0], [[2.0, 0.3], [0.3, 0.5]]).pdf(np.dstack([x, y]))
ax.contour(x, y, z, levels=np.linspace(0, z.max(), 4)[1:-1])

I got this with warnings:

/path/lib/python3.8/site-packages/proplot/colors.py:2284: RuntimeWarning: invalid value encountered in true_divide
  return y0 + (y1 - y0) * (x - x0) / (x1 - x0)

and an empty figure.

Actual behavior: [What actually happened] image

If I use 3 levels:

import proplot as pplt
from scipy.stats import multivariate_normal
import numpy as np

fig, ax = pplt.subplot()
x, y = np.mgrid[-2:2:50j, -2:2:50j]
z = multivariate_normal([0, 0], [[2.0, 0.3], [0.3, 0.5]]).pdf(np.dstack([x, y]))
ax.contour(x, y, z, levels=np.linspace(0, z.max(), 5)[1:-1])

It works well. image

Also I confirm that 2 levels work in matplotlib. It confused me for quite a while when I convert my matplotlib code to proplot.

Proplot version

Paste the results of import matplotlib; print(matplotlib.__version__); import proplot; print(proplot.version) here.

3.4.3 0.9.5.post333

syrte commented 1 year ago

A general temporary workaround for functions that do not work as expected: call the matplotlib version of the plotting function, for example, for the contour function in the above example

mpl.axes._axes.Axes.contour(ax, x, y, z, levels=np.linspace(0, z.max(), 4)[1:-1])
lukelbd commented 1 year ago

Good workaround idea -- mpl.axes.Axes should also work. This is a problem with proplot's DiscreteNorm. Fix was pretty quick (d283935).

syrte commented 1 year ago

Thanks for the quick fix!