在 Python 中将 Matplotlib 的图形传递给 Plotly Dash Graph

Passing Matplotlib's figure to a Plotly Dash Graph in Python

我正在使用 Matplotlib 绘制 python 中句子的词云,如下所示:

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')

结果如下所示:

我想将此图像作为图形对象传递给破折号中的图形。我写了下面的代码:

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)

但是我得到了这个错误:

TypeError: int() 参数必须是字符串、类字节对象或数字,而不是 'WordCloud'

如何解决这个问题并在 dash 的图表中显示词云?

我找到了使用以下代码解决此问题的方法。另外,我通过添加 ID 为“max-word-count”的 inout 字段来限制字数,以便在字数过多时使图形看起来更好。

@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