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:
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?