我有一個相當具體的問題。對于出版事宜,我想為 LDA 生成的每個主題制作 wordcloud 圖。
我目前的代碼是這樣的:
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()
我想要的輸出是讓每個 wordcloud 圖都呈圓形,周圍有一個黑邊。我想在 PCA 圖表上使用每個 Wordcloud。
uj5u.com熱心網友回復:
你可以試試這個:
代碼
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()
結果

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/359794.html
標籤:Python matplotlib 形状 词云
