首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有特定形状的多个字云图

具有特定形状的多个字云图
EN

Stack Overflow用户
提问于 2021-11-16 23:11:40
回答 1查看 686关注 0票数 0

我有一个相当具体的问题。在出版方面,我想为用LDA生成的每一个主题做一个字云情节。

我目前的代码如下:

代码语言:javascript
复制
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from wordcloud import WordCloud
sns.set()

topics_df = pd.DataFrame({'Unnamed: 0': {0: 'Topic1', 1: 'Topic2', 2: 'Topic3', 3: 'Topic4', 4: 'Topic5'}, 'Terms per Topic': {0: 'charge, model, infrastructure, station, time, base, propose, drive, public, service, range, problem, study, paper, network, user, travel, battery, city, method', 1: 'cost, emission, fuel, car, high, price, low, reduce, hybrid, carbon, battery, gas, alternative, benefit, compare, find, conventional, efficiency, gasoline, total', 2: 'market, technology, policy, paper, change, development, support, mobility, business, industry, government, focus, study, transition, innovation, decision, develop, technological, case, lead', 3: 'consumer, policy, adoption, effect, model, purchase, bev, factor, evs, study, incentive, choice, preference, subsidy, influence, environmental, state, level, suggest, analysis', 4: 'energy, system, electricity, demand, transport, scenario, impact, increase, power, phev, transportation, sector, potential, base, show, consumption, reserve, term, economic, grid'}})

wc = WordCloud(background_color="white", colormap="Dark2",
               max_font_size=150, random_state=42)

plt.rcParams['figure.figsize'] = [20, 15]

# Create subplots for each topic
for i in range(5):
    wc.generate(text=topics_df["Terms per Topic"][i])   
    plt.subplot(5, 4, i+1)
    plt.imshow(wc, interpolation="bilinear")
    plt.axis("off")
    plt.title(topics_df.index[i])
plt.show()

我想要的输出结果是,每个字云图都是圆形的,周围有一个黑色的边缘。我想使用PCA图表上的每一个字云。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-17 00:51:25

你可以试试这个:

代码语言:javascript
复制
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from wordcloud import WordCloud
sns.set()
plt.rcParams['figure.figsize'] = [20, 15]

topics_df = pd.DataFrame({'Unnamed: 0': {0: 'Topic1', 1: 'Topic2', 2: 'Topic3', 3: 'Topic4', 4: 'Topic5'}, 'Terms per Topic': {0: 'charge, model, infrastructure, station, time, base, propose, drive, public, service, range, problem, study, paper, network, user, travel, battery, city, method', 1: 'cost, emission, fuel, car, high, price, low, reduce, hybrid, carbon, battery, gas, alternative, benefit, compare, find, conventional, efficiency, gasoline, total', 2: 'market, technology, policy, paper, change, development, support, mobility, business, industry, government, focus, study, transition, innovation, decision, develop, technological, case, lead', 3: 'consumer, policy, adoption, effect, model, purchase, bev, factor, evs, study, incentive, choice, preference, subsidy, influence, environmental, state, level, suggest, analysis', 4: 'energy, system, electricity, demand, transport, scenario, impact, increase, power, phev, transportation, sector, potential, base, show, consumption, reserve, term, economic, grid'}})

x, y = np.ogrid[:300, :300]
mask = (x - 150) ** 2 + (y - 150) ** 2 > 130 ** 2
mask = 255 * mask.astype(int)

wordcloud = WordCloud(background_color="white", mask=mask, contour_width=0.1, 
                      contour_color="black",  max_font_size=150, random_state=42,
                      colormap="Dark2")

for i in range(5):
    wordcloud.generate(text=topics_df["Terms per Topic"][i])   
    plt.subplot(5, 4, i+1)
    plt.imshow(wordcloud, interpolation="bilinear")
    plt.axis("off")
    plt.title(topics_df.index[i])
plt.show()

结果

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69997140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档