简体   繁体   English

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

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

I am plotting a word cloud of a sentence in python using Matplotlib like below:我正在使用 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')

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' TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是“WordCloud”

How can I fix this issue and show the word cloud in dash's graph?如何解决此问题并在破折号的图表中显示词云?

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.此外,我通过添加一个 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM