Save plot using matplotlib

Question:

I try to generate two separate wordcloud for positive and negative. However, when I use savefig it only save the last figure. The last figure is negative sentiment.

TEXT_COLUMN = 'pre_process'
TARGET_COLUMN = 'label'

# visualize the data on a WordCloud
def visualize(label):
    words = ''
    for msg in data[data[TARGET_COLUMN] == label][TEXT_COLUMN]:
        msg = msg.lower()
        words += msg + ' '
    wordcloud = WordCloud(width=600, height=600).generate(words)
    plt.imshow(wordcloud)
    plt.axis('off')
    plt.title('Wordcloud')
    plt.savefig('static/airasia_negcloud.png',transparent=True)
    plt.show()
    plt.clf()

# display wordcloud
visualize(1)
visualize(0)

Here is my wordcloud coding. When I visualize it, it display correctly on PyCharm but in the folder, one is blank image which is suppose to be positive wordcloud and another one is negative worcloud.

Asked By: Ain Nabilah

||

Answers:

The issue you are facing is because you are using the same file name ('static/airasia_negcloud.png') to save both of your wordclouds, so only the last one is being saved. To solve this issue, you can either use a different file name for each of the wordclouds or overwrite the file every time you call plt.savefig(). To use a different file name, you can add the label to the file name like this:

plt.savefig('static/airasia_{}cloud.png'.format('pos' if label == 0 else 'neg'),transparent=True)

Or, to overwrite the file, you can add the line plt.clf() before the call to plt.savefig(), which will clear the current figure:

def visualize(label):
words = ''
for msg in data[data[TARGET_COLUMN] == label][TEXT_COLUMN]:
    msg = msg.lower()
    words += msg + ' '
wordcloud = WordCloud(width=600, height=600).generate(words)
plt.imshow(wordcloud)
plt.axis('off')
plt.title('Wordcloud')
plt.savefig('static/airasia_negcloud.png',transparent=True)
plt.show()
plt.clf()
Answered By: Rajarshi Paul

for solving your problem it would be better if you post a functional example as well. As Rajarshi Paul mentioned, you are overwriting the same file with same name, by calling the function visualize(1), visualize(2). In both cases it has same filename, so you have to change filename for each calling, for example by the used label, here is my example (hope it helps):

import pandas as pd
import matplotlib.pyplot as plt
from wordcloud import (WordCloud, get_single_color_func)

TEXT_COLUMN = 'pre_process'
TARGET_COLUMN = 'label'

data={"Col2":("Dog","cAt","rhIno","BUFFALO"),
      "Col1":(1,2,3,4)}

data=pd.DataFrame(data)
TARGET_COLUMN="Col1"

# visualize the data on a WordCloud
def visualize(label):
    words = ''
    for msg in data[data[TARGET_COLUMN] == label]["Col2"]:
        msg = msg.lower()
        words += msg + ' '
        
    wordcloud = WordCloud(width=600, height=600).generate(words)
    plt.imshow(wordcloud)
    plt.axis('off')
    plt.title('Wordcloud')
    filename='airasia_negcloud_'+str(label)+'.png'
    plt.savefig(filename,transparent=True,dpi=300,bbox_inches = 'tight',
        pad_inches = 0)
    plt.show()
    plt.clf()

# display wordcloud
visualize(1)
visualize(2)
Answered By: Richard Dvoƙák
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.