2

I am using matplotlib.pyplot to create a bargraph and saving a image in a same file but in different functions. But the issue is coming that while plotting/saving my image it is also adding the bar graph too.

Sample Code:
THIS FUNCTION CREATES BAR GRAPH

def plotbargraph(file1_class,file2_class,label_list,color_list, flag , title,classname,filename):    
# SOME  CODE
for i in range(2):
    print '********'
    plt.bar(index+bar_width[i],c[i], bar_width[i+1],alpha=opacity,color=color_list[i],label=label_list[i])
plt.xlabel('Classes')
plt.ylabel('Pixels Count')
plt.title(title)
plt.xticks(index + bar_width[1],classname)
plt.legend()
plt.tight_layout()
plt.savefig(path+filename+'.png')

#THIS FUNCTION CREATES IMAGE

def plotimage(image,labels,classname):#input - array of labels and image of 1..n classes

    cmap = mpl.colors.ListedColormap(['r', 'g', 'b','k'])
    img = plt.imshow(image, cmap=cmap , vmin = 1, vmax =5)
    plt.imsave(path+classname+'.png',image,cmap=cmap)
    plt.savefig(path+classname+'_legend'+'.png')
    #plt.show()

#BAR GARAPH CALLED  

plotbargraph()

def   createdifferencemap(file2_read,unique_val2,true_x_axis,true_y_axis,x_axis,y_axis,class_name):
for i in range(unique_val2.shape[0]):
    3 PLOT IMAGE CALLED
    plotimage(c,labels,class_name[i])
# CREATE DIFFERENCE MAP CALLED
createdifferencemap()

Now the image i am gettingbar graph

Image overlapped with bar graph and multiple legends

Piyush
  • 388
  • 1
  • 6
  • 21
  • 2
    You have to clear your `plt` object before you use a new one, try using `plt.close(img)` prior to re-initializing. See this question for more information http://stackoverflow.com/questions/8213522/matplotlib-clearing-a-plot-when-to-use-cla-clf-or-close – Bas Jansen Jul 30 '15 at 11:09
  • Thanks a Lot . I have used plt.close() and it works. – Piyush Jul 30 '15 at 11:25

0 Answers0