我看到这个功能可以在线生成词云,但是我无法弄清楚如何更改def_random_func. 假设我想要橙色,现在代码总是生成绿色的词云。这是代码:
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS
def random_color_func(word=None, font_size=None, position=None, orientation=None, font_path=None, random_state=None):
h = int(360.0 * 45.0 / 255.0)
s = int(100.0 * 255.0 / 255.0)
l = int(100.0 * float(random_state.randint(60, 120)) / 255.0)
return "hsl({}, {}%, {}%)".format(h, s, l)
file_content = open("rr.txt").read()
wordcloud = WordCloud(font_path=r'C:\Windows\Fonts\Verdana.ttf',
stopwords=STOPWORDS,
background_color='white',
width=1200,
height=1000,
color_func=random_color_func
).generate(file_content)
plt.imshow(wordcloud)
plt.axis('off')
plt.show()
Run Code Online (Sandbox Code Playgroud)