Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Colorbar.add_lines() is misused or something... #4998

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

Closed
WeatherGod opened this issue Aug 27, 2015 · 2 comments
Closed

Colorbar.add_lines() is misused or something... #4998

WeatherGod opened this issue Aug 27, 2015 · 2 comments

Comments

@WeatherGod
Copy link
Member

testing with numpy 1.10rc1 revealed some bugs through their deprecation warnings. One in particular is in the interaction of contour/contourf with colorbar. It seems that add_lines() requires that inputs have the same lengths, but it does not check if this is the case. If I add in a check for this, one of our tests fails:

======================================================================
ERROR: test suite for <class 'matplotlib.tests.test_contour.test_given_colors_levels_and_extends'>
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/nas/home/broot/centos6/lib/python2.7/site-packages/nose-1.1.2-py2.7.egg/nose/suite.py", line 208, in run
    self.setUp()
  File "/nas/home/broot/centos6/lib/python2.7/site-packages/nose-1.1.2-py2.7.egg/nose/suite.py", line 291, in setUp
    self.setupContext(ancestor)
  File "/nas/home/broot/centos6/lib/python2.7/site-packages/nose-1.1.2-py2.7.egg/nose/suite.py", line 314, in setupContext
    try_run(context, names)
  File "/nas/home/broot/centos6/lib/python2.7/site-packages/nose-1.1.2-py2.7.egg/nose/util.py", line 478, in try_run
    return func()
  File "/nas/home/broot/centos6/lib/python2.7/site-packages/matplotlib-1.5.dev1-py2.7-linux-x86_64.egg/matplotlib/testing/decorators.py", line 154, in setup_class
    cls._func()
  File "/nas/home/broot/centos6/lib/python2.7/site-packages/matplotlib-1.5.dev1-py2.7-linux-x86_64.egg/matplotlib/tests/test_contour.py", line 192, in test_given_colors_levels_and_extends
    plt.colorbar()
  File "/nas/home/broot/centos6/lib/python2.7/site-packages/matplotlib-1.5.dev1-py2.7-linux-x86_64.egg/matplotlib/pyplot.py", line 2234, in colorbar
    ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw)
  File "/nas/home/broot/centos6/lib/python2.7/site-packages/matplotlib-1.5.dev1-py2.7-linux-x86_64.egg/matplotlib/figure.py", line 1570, in colorbar
    cb = cbar.colorbar_factory(cax, mappable, **kw)
  File "/nas/home/broot/centos6/lib/python2.7/site-packages/matplotlib-1.5.dev1-py2.7-linux-x86_64.egg/matplotlib/colorbar.py", line 1338, in colorbar_factory
    cb = Colorbar(cax, mappable, **kwargs)
  File "/nas/home/broot/centos6/lib/python2.7/site-packages/matplotlib-1.5.dev1-py2.7-linux-x86_64.egg/matplotlib/colorbar.py", line 905, in __init__
    self.add_lines(CS)
  File "/nas/home/broot/centos6/lib/python2.7/site-packages/matplotlib-1.5.dev1-py2.7-linux-x86_64.egg/matplotlib/colorbar.py", line 948, in add_lines
    erase=erase)
  File "/nas/home/broot/centos6/lib/python2.7/site-packages/matplotlib-1.5.dev1-py2.7-linux-x86_64.egg/matplotlib/colorbar.py", line 542, in add_lines
    " %d vs. %d" % (len(colors), len(levels)))
ValueError: colors is not the same length as levels: 5 vs. 4

The test in question is:

@image_comparison(baseline_images=['contour_manual_colors_and_levels'],
                  extensions=['png'], remove_text=True)
def test_given_colors_levels_and_extends():
    _, axes = plt.subplots(2, 4)

    data = np.arange(12).reshape(3, 4)

    colors = ['red', 'yellow', 'pink', 'blue', 'black']
    levels = [2, 4, 8, 10]

    for i, ax in enumerate(axes.flatten()):
        plt.sca(ax)

        filled = i % 2 == 0.
        extend = ['neither', 'min', 'max', 'both'][i // 2]

        if filled:
            last_color = -1 if extend in ['min', 'max'] else None
            plt.contourf(data, colors=colors[:last_color], levels=levels,
                         extend=extend)
        else:
            last_level = -1 if extend == 'both' else None
            plt.contour(data, colors=colors, levels=levels[:last_level],
                        extend=extend)

        plt.colorbar()
@anntzer
Copy link
Contributor

anntzer commented Jun 26, 2017

The error does not occur anymore on that test, at least as of recent mpl and numpy.

@anntzer anntzer closed this as completed Jun 26, 2017
@QuLogic
Copy link
Member

QuLogic commented Jun 26, 2017

Fixed by #8737; dupe of #5363 and #7334.

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

No branches or pull requests

3 participants