我创建了两个图形,分别在各自的函数中创建了它们,以保持逻辑的组织性。我知道可以通过在.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