简体   繁体   中英

MatPlotLib adding colorbar after colorbar

remove colorbar from figure in matplotlib

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

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)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM