1

As we know that we can remove the collections of contour/contourf. But how can I remove the contour's clabel?

fig = plt.figure()
ax = fig.add_subplots(111)
for ivalue in range(10):
    values = alldata [ivalue,:,:]
    cs = plt.contour(x,y,vakues)
    cb = plt.clabel(cs, cs.levels)
     # now remove cs
    for c in cs.collections:
        c.remove()
    # but how can I remove cb?
    plt.savefig('%s.png'%ivalue)

The clabel of first png still exists in second png. So i want to remove clabel meanwhile.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Li Ziming
  • 385
  • 2
  • 5
  • 17
  • 1
    Don't call `clabel()`? – Bart Nov 01 '17 at 08:08
  • Sorry, I should give some scripts. – Li Ziming Nov 01 '17 at 15:19
  • Sorry, but I really don't get your question: *"but how can I remove cb?"*. Why try to remove it, when your problem is solved when you don't call `clabel`? – Bart Nov 01 '17 at 21:13
  • Might be good to post the *actual* problem, i.e. how do you end up with a contour plot with labels, which you can't remove by not callling `clabel()`, or clarify why you first want to add labels, only to remove them later. – Bart Nov 02 '17 at 07:33
  • @Bart I just want to plot figure quickly. Create one figure and a it's axis --- plot contour and clabel --- savefig . So after plot contour i need to remove it and it's clabel. That's why i want to remove clabel. – Li Ziming Nov 02 '17 at 07:38

1 Answers1

3

You can do the exact same as you are already doing for the contour lines. Minimal example:

import numpy as np
import matplotlib.pylab as pl

pl.figure()
for i in range(2):
    c  = pl.contour(np.random.random(100).reshape(10,10))
    cl = pl.clabel(c)

    if i == 1:
        pl.savefig('fig.png'.format(i))

Results in double contours, labels:

enter image description here

By changing it to:

    # Same code as above left out

    if i == 1:
        pl.savefig('fig.png'.format(i))

    for contour in c.collections:
        contour.remove()

    for label in cl:
        label.remove()

enter image description here

Bart
  • 9,825
  • 5
  • 47
  • 73