1

I am plotting a word cloud of a sentence in python using Matplotlib like below:

import matplotlib.pyplot as plt 
from wordcloud import WordCloud, STOPWORDS
word_cloud = WordCloud(collocations = False, background_color = 'white').generate("This is a test sentence with the purpose of plotting a word cloud and converting it to dash graph object")
plt.axis('off')
image = plt.imshow(word_cloud, interpolation='bilinear')

The result looks like below:

enter image description here

I want to pass this image to a graph in dash as a graph object. I wrote the following code:

import dash
from dash.dependencies import Input, Output, State
from dash import dcc
from dash import dash_table, html
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
import plotly.tools

app = dash.Dash(__name__)

app.layout = html.Div(children=[
html.A("Start",style={'backgroundColor':'green'},id='start'),
dcc.Graph(id='result')
])

@app.callback(
    Output('result', 'figure'),
    [Input('start', 'n_clicks')]
)
def update_figure(n1):
    word_cloud = WordCloud(collocations = False, background_color = 'white').generate("his is a test sentence with the purpose of plotting a word cloud and converting it to dash graph object")
    plt.axis('off')
    word_cloud_figure = plt.figure(word_cloud, interpolation='bilinear')
    if(n1):
        plotly.tools.mpl_to_plotly(word_cloud_figure)
    return word_cloud_figure

if __name__ == '__main__':
    app.run_server(debug=True, port=9872)

But I got this error:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'WordCloud'

How can I fix this issue and show the word cloud in dash's graph?

Naser.Sadeghi
  • 1,341
  • 1
  • 12
  • 35

1 Answers1

0

I found a way to solve this problem by using the code below. Also, I limited the number of words by adding an inout field with ID "max-word-count" to make the figure look nicer when the number of words is too many.

@app.callback(
    Output('indicator-graphic-for-word-clouds', 'figure'),
    [
        Input('description-dropdown', 'value'),
        Input('max-word-count', 'value')
    ], 
        State('analyzed-dataset', 'data')
    )
def update_graph(selected_description, max_word_count, analyzed_dataset):
    dataset = pd.DataFrame.from_dict(analyzed_dataset)
    text = ""
    if(selected_description == "All"):
        temp_dataset = dataset['description']
        for review in temp_dataset:
            text += review
    else:
         temp_dataset = dataset[dataset['comment'] == selected_description]['description']
        print(temp_dataset)
        for review in temp_dataset:
            text += review
            text += "\n"
    tokenizer = nltk.RegexpTokenizer(r"\w+")
    tokenized_review = tokenizer.tokenize(text.lower())
    filtered_review = [word for word in tokenized_review if word not in stopwords.words('english')]
    word_count = 0
    try:
        word_count = int(max_word_count)
        if(word_count > len(filtered_review)):
            word_count = len(filtered_review)
    except:
        word_count = len(filtered_review)
    colors = [plotly.colors.DEFAULT_PLOTLY_COLORS[random.randrange(1, 10)] for i in range(word_count)]
    weights = [random.randint(15, 35) for i in range(word_count)]
    data = go.Scatter(x=[random.random() for i in range(word_count)],
        y=[random.random() for i in range(word_count)],
        mode='text',
        text=filtered_review,
        marker={'opacity': 0.3},
        textfont={'size': weights,
                'color': colors})
    layout = go.Layout({'xaxis': {'showgrid': False, 'showticklabels': False, 'zeroline': False},
    'yaxis': {'showgrid': False, 'showticklabels': False, 'zeroline': False}})
    fig = go.Figure(data=[data], layout=layout)
    return fig
Naser.Sadeghi
  • 1,341
  • 1
  • 12
  • 35