I have a rather specific problem. For the publishing matter, I would like to make wordcloud plots for each topic generated with LDA.
My current code is like this:
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()
My desired output would be to have each wordcloud plot in a circle shape, having a black edge around it. I would like to use each Wordcloud on a PCA chart.
CodePudding user response:
You can try this :
code
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()