本文整理汇总了Python中wordcloud.WordCloud.background_color方法的典型用法代码示例。如果您正苦于以下问题:Python WordCloud.background_color方法的具体用法?Python WordCloud.background_color怎么用?Python WordCloud.background_color使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wordcloud.WordCloud
的用法示例。
在下文中一共展示了WordCloud.background_color方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: output
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import background_color [as 别名]
def output():
# Load the dictionary
database_dict = pickle.load(open( data_folder_path + 'updated_database_dict.p', 'rb'))
# pull 'ID' (url_string) from input field and store it
url_string = request.args.get('ID')
# pull the number of clusters from the user form
if request.args.get('Nclusters'):
Nclusters = int(request.args.get('Nclusters'))
else:
Nclusters = 3
if len(url_string) < 5:
url_string = 'http://www.nytimes.com/2015/09/20/opinion/sunday/a-toxic-work-world.html'
if url_string not in database_dict.keys():
database_dict = update_database(url_string, database_dict)
headline = database_dict[url_string]['title']
abstract = database_dict[url_string]['abstract']
article_summary = dict(headline = headline, abstract = abstract)
# Generate a wordcloud plot for the article
Nkeywords = len(database_dict[url_string]['keyword_dict'])
word_freq_list = [(entry['value'], Nkeywords - float(entry['rank'])) for entry in database_dict[url_string]['keyword_dict']]
# clean up wordcloud styles
title_wordcloud = WordCloud().generate_from_frequencies(word_freq_list)
title_wordcloud.background_color = 'white'
title_wordcloud.recolor(color_func=custom_color_func)
# Prepare figure for output.html
fig = Figure()
fig.set_facecolor('None')
ax = fig.add_subplot(111)
ax.imshow(title_wordcloud)
ax.set_axis_off()
canvas = FigureCanvas(fig)
title_cloud_png_output = StringIO.StringIO()
canvas.print_png(title_cloud_png_output)
title_cloud_png_output = title_cloud_png_output.getvalue().encode('base64')
# Get the three representative comments
rep_comments = get_representative_comments(database_dict[url_string]['comments_df'], Nclusters)
senti_pos = database_dict[url_string]['comments_df']['senti_pos']
# Get the pie chart
fig = Figure()
fig.set_facecolor('None')
ax = fig.add_subplot(111)
color_repo = ['#4D4D4D', '#5DA5DA', '#FAA43A', '#60BD68', '#F17CB0', '#B2912F', '#B276B2', '#DECF3F', '#F15854']
sizes = [rep_comments[i]['count'] for i in range(Nclusters)]
colors = [color_repo[i] for i in range(Nclusters)]
sorted_sizes_args = np.argsort(sizes)[::-1]
labels = ['Cluster ' + str(i+1) for i in range(Nclusters)]
sorted_sizes = sorted(sizes)[::-1]
ax.pie(sorted_sizes, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=90)
ax.set_axis_off()
canvas = FigureCanvas(fig)
pie_png_output = StringIO.StringIO()
canvas.print_png(pie_png_output)
pie_png_output = pie_png_output.getvalue().encode("base64")
# Sentiment plot
fig = Figure()
fig.set_facecolor('None')
ax = fig.add_subplot(111)
ax.hist(senti_pos, bins = 20)
ax.set_xlim(0,1)
ax.set_xlabel('Sentiment Scale')
ax.set_ylabel('Number of Comments')
ax.set_xticks(np.linspace(0, 1, 5))
senti_labels = [item.get_text() for item in ax.get_xticklabels()]
senti_labels[0] = 'Neg'
senti_labels[2] = '0'
senti_labels[-1] = 'Pos'
ax.set_xticklabels(senti_labels)
canvas=FigureCanvas(fig)
png_output = StringIO.StringIO()
canvas.print_png(png_output)
png_output = png_output.getvalue().encode("base64")
# Return word clouds for different comments
word_cloud_comments = {} # key: cluster_label, val: fig_data
for lab in range(Nclusters):
comment_keywords = get_keywords(rep_comments[lab]['comment'])
# comment_keywords = rep_comments[lab]['cluster_keywords']
keyword_wordfreq_list = [ (word, len(comment_keywords) - i) for i,word in enumerate(comment_keywords)]
wordcloud = WordCloud().generate_from_frequencies(keyword_wordfreq_list)
wordcloud.background_color = 'white'
wordcloud.recolor(color_func=custom_color_func)
fig = Figure()
fig.set_facecolor('None')
ax = fig.add_subplot(111)
ax.imshow(wordcloud)
ax.set_axis_off()
canvas=FigureCanvas(fig)
#.........这里部分代码省略.........