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

Colorbars for contour, pcolor, etc. not working #12

Closed
bradyrx opened this issue Feb 28, 2019 · 2 comments
Closed

Colorbars for contour, pcolor, etc. not working #12

bradyrx opened this issue Feb 28, 2019 · 2 comments
Labels

Comments

@bradyrx
Copy link
Collaborator

bradyrx commented Feb 28, 2019

I'm having issues with setting up colorbars for pcolormesh,pcolorpoly,contourf, etc. See the following (note the meshgrid call is so that it will work with cartopy):

f, ax = plot.subplots(bottomcolorbar=True)

offset = 20
x = plot.arange(-180+offset,180+offset-1,30)
y = plot.arange(-60,60+1,10)
X, Y = np.meshgrid(x, y)
data = np.random.rand(len(y), len(x))
m = ax.pcolormesh(X, Y, data)
f.bottompanel.colorbar(m)

screen shot 2019-02-28 at 4 51 16 pm

The error message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-106-ff323e608faf> in <module>
      8 m = ax.pcolormesh(x, y, data)
      9 
---> 10 f.bottompanel.colorbar(m)

~/miniconda3/envs/python3/lib/python3.6/site-packages/proplot/subplots.py in iterator(*args, **kwargs)
    586                 ret = []
    587                 for ax in self:
--> 588                     res = getattr(ax, attr)(*args, **kwargs)
    589                     if res is not None:
    590                         ret += [res]

~/miniconda3/envs/python3/lib/python3.6/site-packages/proplot/axes.py in colorbar(self, i, n, length, space, hspace, wspace, *args, **kwargs)
   2198             orientation  = 'vertical'
   2199         kwargs.update({'orientation':orientation, 'ticklocation':ticklocation})
-> 2200         return colorbar_factory(ax, *args, **kwargs)
   2201 
   2202 class MapAxes(BaseAxes):

~/miniconda3/envs/python3/lib/python3.6/site-packages/proplot/axes.py in colorbar_factory(ax, mappable, values, orientation, extend, extendlength, clabel, label, ctickminor, tickminor, cgrid, grid, ticklocation, cticklocation, ctickdir, tickdir, clocator, locator, cminorlocator, minorlocator, clocator_kw, locator_kw, cminorlocator_kw, minorlocator_kw, cformatter, formatter, cticklabels, ticklabels, norm, norm_kw, **kwargs)
   3241             ticks=locators[0],
   3242             format=cformatter,
-> 3243             **csettings)
   3244 
   3245     # Make edges/dividers consistent with axis edges

~/miniconda3/envs/python3/lib/python3.6/site-packages/matplotlib/figure.py in colorbar(self, mappable, cax, ax, use_gridspec, **kw)
   2127                              'panchor']
   2128         cb_kw = {k: v for k, v in kw.items() if k not in NON_COLORBAR_KEYS}
-> 2129         cb = cbar.colorbar_factory(cax, mappable, **cb_kw)
   2130 
   2131         self.sca(current_ax)

~/miniconda3/envs/python3/lib/python3.6/site-packages/matplotlib/colorbar.py in colorbar_factory(cax, mappable, **kwargs)
   1565         cb = ColorbarPatch(cax, mappable, **kwargs)
   1566     else:
-> 1567         cb = Colorbar(cax, mappable, **kwargs)
   1568 
   1569     cid = mappable.callbacksSM.connect('changed', cb.on_mappable_changed)

~/miniconda3/envs/python3/lib/python3.6/site-packages/matplotlib/colorbar.py in __init__(self, ax, mappable, **kw)
   1096                 kw['alpha'] = mappable.get_alpha()
   1097 
-> 1098             ColorbarBase.__init__(self, ax, **kw)
   1099 
   1100     def on_mappable_changed(self, mappable):

~/miniconda3/envs/python3/lib/python3.6/site-packages/matplotlib/colorbar.py in __init__(self, ax, cmap, norm, alpha, values, boundaries, orientation, ticklocation, extend, spacing, ticks, format, drawedges, filled, extendfrac, extendrect, label)
    412             self.formatter = format  # Assume it is a Formatter
    413         # The rest is in a method so we can recalculate when clim changes.
--> 414         self.draw_all()
    415 
    416     def _extend_lower(self):

~/miniconda3/envs/python3/lib/python3.6/site-packages/matplotlib/colorbar.py in draw_all(self)
    446         C = self._values[:, np.newaxis]
    447         self.config_axis()
--> 448         self._config_axes(X, Y)
    449         if self.filled:
    450             self._add_solids(X, Y, C)

~/miniconda3/envs/python3/lib/python3.6/site-packages/matplotlib/colorbar.py in _config_axes(self, X, Y)
    634         ax.add_artist(self.patch)
    635 
--> 636         self.update_ticks()
    637 
    638     def _set_label(self):

~/miniconda3/envs/python3/lib/python3.6/site-packages/matplotlib/colorbar.py in update_ticks(self)
    546         else:
    547             _log.debug('Using fixed locator on colorbar')
--> 548             ticks, ticklabels, offset_string = self._ticker(locator, formatter)
    549             long_axis.set_ticks(ticks)
    550             long_axis.set_ticklabels(ticklabels)

~/miniconda3/envs/python3/lib/python3.6/site-packages/matplotlib/colorbar.py in _ticker(self, locator, formatter)
    771         ticks = self._locate(b)
    772         formatter.set_locs(b)
--> 773         ticklabels = [formatter(t, i) for i, t in enumerate(b)]
    774         offset_string = formatter.get_offset()
    775         return ticks, ticklabels, offset_string

~/miniconda3/envs/python3/lib/python3.6/site-packages/matplotlib/colorbar.py in <listcomp>(.0)
    771         ticks = self._locate(b)
    772         formatter.set_locs(b)
--> 773         ticklabels = [formatter(t, i) for i, t in enumerate(b)]
    774         offset_string = formatter.get_offset()
    775         return ticks, ticklabels, offset_string

~/miniconda3/envs/python3/lib/python3.6/site-packages/proplot/axistools.py in __call__(self, x, pos)
    888         prefix = ''
    889         tickrange = self._tickrange
--> 890         if (x + eps) < tickrange[0] or (x - eps) > tickrange[1]:
    891             return '' # avoid some ticks
    892         string = super().__call__(x, pos)

TypeError: 'NoneType' object is not subscriptable
@bradyrx
Copy link
Collaborator Author

bradyrx commented Feb 28, 2019

@lukelbd , it also might be worth re-running your demo notebook after major updates. A lot of the code snippets break with the current version of proplot. For example, the snippet I was trying to make a colorbar off of:

import proplot as plot
import numpy as np
plot.nbsetup()
# First make figure
f, axs = plot.subplots(ncols=2, nrows=2, width=7, hspace=0.2, wspace=0.3, top=0.5,
                       bottomcolorbars=True, bwidth=0.2, bottom=0.2,
                       proj='hammer', proj_kw={'lon_0':0},
                       # basemap=False,
                       basemap={(1,3):False, (2,4):True},
                       )
offset = 20
x = plot.arange(-180+offset,180+offset-1,60)
y = plot.arange(-60,60+1,30)
data = np.random.rand(len(x), len(y))
for ax,p,pcolor,basemap in zip(axs,range(4),[1,1,0,0],[0,1,0,1]):
    # adfdas
    m = None
    cmap = ['sunset', 'sunrise'][basemap]
    levels = [0, .3, .5, .7, .9, 1]
    levels = np.linspace(0,1,11)
    if pcolor:
        m = ax.pcolorpoly(x, y, data, levels=levels, cmap=cmap, extend='both', extremes=True)
        ax.scatter(np.random.rand(5,5)*180, 180*np.random.rand(5,5))
    if not pcolor:
        m = ax.contourf(x, y, data, levels=levels, cmap=cmap, extend='both', extremes=False)
        ax.scatter(np.random.rand(5,5)*180, 180*np.random.rand(5,5))
    ax.format(facecolor='gray2', suptitle='Hammer projection in different mapping frameworks', collabels=['Cartopy', 'Basemap'])
    if p<2:
        ax, c = f.bottompanel[p].colorbar(m, clabel='values', ctickminor=False)
    # print(p, ax._sharex, ax._sharey, list(ax._shared_x_axes))
    # if p==2:
        # raise Exception

Breaks with a similar error to above.

@lukelbd
Copy link
Collaborator

lukelbd commented Mar 1, 2019

Ok, this bug + some other bugs should be fixed now. All the demo examples work.

@lukelbd lukelbd closed this as completed Mar 1, 2019
@lukelbd lukelbd added the bug label Sep 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants