0

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.

Community
  • 1
  • 1
mauve
  • 2,707
  • 1
  • 20
  • 34
  • Duplicated in the newer question which I answered: http://stackoverflow.com/questions/25086206/matplotlib-plotting-in-loop-removing-colorbar-but-whitespace-remains – mdurant Aug 01 '14 at 23:01

1 Answers1

0

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

mauve
  • 2,707
  • 1
  • 20
  • 34