-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Comments
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 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 |
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! |
Actually there are a couple of other things which are not OK with xarray's 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() I'll try to have a look at what's going on. @xarraydevs, do you agree that |
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. |
Oh, not really! thanks for the fix :) |
@fmaussion I would agree that if you pick a fixed color, then |
@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() I think it's ok now, and the 1-level examples now run just fine. |
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
The text was updated successfully, but these errors were encountered: