Matplotlib: how to remove contour's clabel

狂风中的少年 提交于 2021-02-08 09:24:12

问题


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.


回答1:


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:

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()



来源:https://stackoverflow.com/questions/47049296/matplotlib-how-to-remove-contours-clabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!