import plotly.express as px
import pandas as pd
# 假设这是你的词频数据
word_freq = {
'Python': 50,
'数据分析': 30,
'机器学习': 45,
'深度学习': 20,
'AI': 25,
'NLP': 15,
'Plotly': 35,
'可视化': 40,
'算法': 22,
'编程': 28
}
# 将数据转换为DataFrame
df = pd.DataFrame(list(word_freq.items()), columns=['word', 'freq'])
# 创建一个新列,用于表示字体大小
df['size'] = df['freq'] * 2 # 你可以根据需要调整倍率
# 添加随机位置用于散点图
import numpy as np
np.random.seed(42) # 为了结果可重复,这里设置随机种子
df['x'] = np.random.rand(len(df))
df['y'] = np.random.rand(len(df))
# 使用Plotly绘制带有文本的散点图
fig = px.scatter(df, x='x', y='y', size='size', text='word', size_max=60)
fig.update_traces(textposition='middle center',
marker=dict(opacity=0))
fig.update_layout(showlegend=False,
xaxis=dict(visible=False),
yaxis=dict(visible=False),
plot_bgcolor='white')
fig.show()