MatPlotLib 在颜色条后添加颜色条 [英] MatPlotLib adding colorbar after colorbar

查看:55
本文介绍了MatPlotLib 在颜色条后添加颜色条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 matplotlib 中的图形中删除颜色条

我不太明白如何实际使用这个答案.

I am not quite getting how to use this answer practically.

在我的程序中,我需要生成 5 个图.它们基本上都是相同的格式.这是我的功能:

In my program, I need to produce 5 plots. They are all basically the same format. Here is my function:

def plot_that(x_vals, y_vals, z_vals, figname, units, efficiency_or_not):
    ui = uniformity_calc(z_vals)
    if efficiency_or_not:
        plt.scatter(x_vals, y_vals, s = 3*max(x_vals), c = z_vals, cmap = 'rainbow', vmin = 0, vmax = 1)
    else:
        plt.scatter(x_vals, y_vals, s = 3*max(x_vals), c = z_vals, cmap = 'rainbow')
    c = plt.colorbar()
    c.set_label(units)

    plt.xlabel('Uniformity: ' + str(round(ui,2)))
    plt.savefig('./'+ figname + '.jpg', dpi = 100)

我很高兴它可以工作,最终我可以将所有 5 个图都放入我的 pdf...

I am very excited that it works and ultimately I can get all 5 plots into my pdf...

我不太高兴每个图都有 1 个额外的颜色条.第一个有它自己的...第二个有它自己的加上上图中的一个....第三个有自己的加上前两个......

I am less excited that each plot has 1 additional colorbar. The first has its own...the second has its own plus the one from the previous figure....the third has its own plus the previous two...

我查看了这个解决方案,但我并没有真正看到实现它的好方法.我知道我需要清除以前的颜色条,如果有的话……我真的不明白怎么做.

I looked at this solution, but I'm not really seeing a good way to implement it. I understand I need to clear the previous colorbar if there is one...I just really really don't understand how.

推荐答案

我添加了一行并解决了我的问题:plt.close()

I added one line and solved my problem: plt.close()

这篇关于MatPlotLib 在颜色条后添加颜色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆