我有两个用 plotly 创建的数字,它们在自己的函数中创建,以保持逻辑有序。我知道使用 plotly 可以通过在 .add_trace() 函数中生成数字来添加数字到子图中,但我想添加我的两个预制数字。有什么方法可以做到这一点,或者我需要在.add_trace()函数中工作我的两个函数的逻辑,以创建他们的数字?
这是我的两个函数,它制作了我的两个数字。
def bar_chart_sentiment(negative, mixed, neutral, positive):
x = ["negative", "mixed", "neutral", "positive"]
y = [negative, mixed, neutral, positive]
sentiment_chart = go.Figure(data=[go.Bar(
x=x, y=y,
hovertext=[f'{((negative / 25) * 100)}%', f'{((mixed / 25) * 100)}%', f'{((neutral / 25) * 100)}%',
f'{((positive / 25) * 100)}%'],
textposition='auto',
)])
sentiment_chart.update_traces(marker_color='rgb(158,202,225)', marker_line_color='rgb(8,48,107)',
marker_line_width=1.5, opacity=0.6)
sentiment_chart.update_layout(
title="Sentiment of r/wallstreetbets",
xaxis_title="Sentiment",
yaxis_title="Amount",
font=dict(
family="Courier New, monospace",
size=18,
color="#7f7f7f"
)
)
return sentiment_chart
def bar_chart_entities(content):
nltk.download('punkt')
nltk.download('stopwords')
tokenized_word = word_tokenize(content)
stop_words = set(stopwords.words("english"))
filtered_sent = []
for w in tokenized_word:
if w not in stop_words:
filtered_sent.append(w)
fdist = FreqDist(filtered_sent)
fd = pd.DataFrame(fdist.most_common(10), columns=["Word", "Frequency"]).drop([0]).reindex()
entities_chart = px.bar(fd, x="Word", y="Frequency")
entities_chart.update_traces(marker_color='rgb(158,202,225)', marker_line_color='rgb(8,48,107)',
marker_line_width=1.5, opacity=0.6)
entities_chart.update_layout(
title="Top 10 Words of r/wallstreetbets",
font=dict(
size=18,
color="#7f7f7f"
)
)
return entities_chart
def dashboard(sentiment_chart, entities_chart):
# Here is where the subplot should be made and my two pre-made charts added to the subplot