Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drawing only one contour #866

Closed
JoyMonteiro opened this issue Jun 2, 2016 · 8 comments · Fixed by #868
Closed

Drawing only one contour #866

JoyMonteiro opened this issue Jun 2, 2016 · 8 comments · Fixed by #868

Comments

@JoyMonteiro
Copy link

Hello,

I was trying to draw only a single contour by passing levels=[0], and nothing gets
plotted.

I checked utils.py, and the logic used to calculate n_colors in _build_discrete_cmap
gives n_colors=0, since it will first set extend to 'neither', and so ext_n = 0, and
n_colors = len(levels) + ext_n - 1

I'm not sure, but this might be the issue.

Another issue, which might be unrelated, is when I'm trying to draw two contours. It plots only
one contour, and it will plot two contours only if I set norm=None.

Any suggestions?

TIA,
Joy

@fmaussion
Copy link
Member

Here's a mwe:

import numpy as np
import matplotlib.pyplot as plt
import xarray as xr

x, y = np.meshgrid(np.arange(12), np.arange(12)) 
z = xr.DataArray(x**2 + y**2)
z.plot.contour(levels=[10], add_colorbar=False)

which indeed doesn't plot anything.

Note that without add_colorbar=False, it crashes with:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mowglie/.pyvirtualenvs/oggm_pycharm/lib/python3.4/site-packages/xarray/plot/plot.py", line 452, in plotmethod
    return newplotfunc(**allargs)
  File "/home/mowglie/.pyvirtualenvs/oggm_pycharm/lib/python3.4/site-packages/xarray/plot/plot.py", line 426, in newplotfunc
    cbar = plt.colorbar(primitive, ax=ax, extend=cmap_params['extend'])
  File "/home/mowglie/.pyvirtualenvs/oggm_pycharm/lib/python3.4/site-packages/matplotlib/pyplot.py", line 2237, in colorbar
    ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw)
  File "/home/mowglie/.pyvirtualenvs/oggm_pycharm/lib/python3.4/site-packages/matplotlib/figure.py", line 1595, in colorbar
    cb = cbar.colorbar_factory(cax, mappable, **kw)
  File "/home/mowglie/.pyvirtualenvs/oggm_pycharm/lib/python3.4/site-packages/matplotlib/colorbar.py", line 1330, in colorbar_factory
    cb = Colorbar(cax, mappable, **kwargs)
  File "/home/mowglie/.pyvirtualenvs/oggm_pycharm/lib/python3.4/site-packages/matplotlib/colorbar.py", line 895, in __init__
    ColorbarBase.__init__(self, ax, **kw)
  File "/home/mowglie/.pyvirtualenvs/oggm_pycharm/lib/python3.4/site-packages/matplotlib/colorbar.py", line 323, in __init__
    self.draw_all()
  File "/home/mowglie/.pyvirtualenvs/oggm_pycharm/lib/python3.4/site-packages/matplotlib/colorbar.py", line 346, in draw_all
    X, Y = self._mesh()
  File "/home/mowglie/.pyvirtualenvs/oggm_pycharm/lib/python3.4/site-packages/matplotlib/colorbar.py", line 812, in _mesh
    y = self._uniform_y(self._central_N())
  File "/home/mowglie/.pyvirtualenvs/oggm_pycharm/lib/python3.4/site-packages/matplotlib/colorbar.py", line 749, in _uniform_y
    automin = automax = 1. / (N - 1.)
ZeroDivisionError: float division by zero

@JoyMonteiro
Copy link
Author

Yes, I had encountered that as well, but did not realise it was due to the colorbar... I normally don't use the colorbar. Thanks for the example!

@fmaussion
Copy link
Member

fmaussion commented Jun 3, 2016

Actually there are a couple of other things which are not OK with xarray's contour. For example:

import numpy as np
import matplotlib.pyplot as plt
import xarray as xr

x, y = np.meshgrid(np.arange(12), np.arange(12))
z = xr.DataArray(np.sqrt(x**2 + y**2))

kw = {'levels':[2, 4, 6, 8], 'colors':['k']}
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 3))
z.plot.contour(ax=ax1, **kw)
ax2.contour(z.values, **kw)
plt.show()

figure_1

I'll try to have a look at what's going on. @xarraydevs, do you agree that add_colorbar should default to false when contour is used?

@JoyMonteiro
Copy link
Author

Just repeating your example, but with

z.plot.contour(ax=ax1, norm=None, **kw)

gives the expected

figure_1-1

for some reason, the norm is causing an issue.

@fmaussion
Copy link
Member

This PR should fix the issue. I now realize you were maybe willing to submit your own fix @JoyMonteiro ? Sorry if that was the case I can retract my PR.

@JoyMonteiro
Copy link
Author

Oh, not really! thanks for the fix :)

@shoyer
Copy link
Member

shoyer commented Jun 3, 2016

@fmaussion I would agree that if you pick a fixed color, then add_colorbar=False is a more sensible default choice. Why do you think that's a better choice in general?

@fmaussion
Copy link
Member

fmaussion commented Jun 3, 2016

@shoyer I thought that the colorbar was not very meaningful for contours, but I changed my mind.

[EDIT] I just updated the PR again. See the following example generated with the PR branch:

import numpy as np
import matplotlib.pyplot as plt
import xarray as xr

x, y = np.meshgrid(np.arange(12), np.arange(12))
z = xr.DataArray(np.sqrt(x**2 + y**2))
ds = z.to_dataset(name='z')

f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(6, 6))
ds.z.plot.contour(ax=ax1, levels=[4])
ds.z.plot.contour(ax=ax2, levels=[-1, 2, 4, 6, 8])
ds.z.plot.contour(ax=ax3, levels=[4], colors=['k'])
ds.z.plot.contour(ax=ax4, levels=[2, 4, 6, 8], colors=['k', 'r', 'g', 'b'])
plt.tight_layout()
plt.show()

figure_1-3

I think it's ok now, and the 1-level examples now run just fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants