html word cloud,wordcloud2.js

wordcloud2.js是什么

什么是wordcloud2.js,Tag cloud/Wordle presentation on 2D canvas or HTML

wordcloud2.js官网:官网

wordcloud2.js文档:文档

wordcloud2.js源码仓库:源码仓库

wordcloud2.js下载地址:点此下载 点此下载2

wordcloud2.js介绍、wordcloud2.js使用

Create a tag cloud/Wordle presentation on 2D canvas or HTML.

This library is a spin-off project from HTML5 Word Cloud.

Visit the demo page

Simple usage

Load wordcloud.js script to the web page, and run:

WordCloud(document.getElementById('my_canvas'), { list: list } );

where list is an array that look like this: [['foo', 12], ['bar', 6]].

Options available, see API documentation for detail.

Contact & help

Please read through the API documentation and CONTRIBUTING.md before filing an issue or contact me via e-mail.

Algorithm

Befo

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import pandas as pd import numpy as np import matplotlib.pyplot as plt import jieba import requests import re from io import BytesIO import imageio # 设置城市和时间 city = '上海' year = 2021 quarter = 2 # 爬取数据 url = f'http://tianqi.2345.com/t/wea_history/js/{city}/{year}/{quarter}.js' response = requests.get(url) text = response.content.decode('gbk') # 正则表达式匹配 pattern = re.compile(r'(\d{4}-\d{2}-\d{2})\|(\d{1,2})\|(\d{1,2})\|(\d{1,3})\|(\d{1,3})\|(\D+)\n') result = pattern.findall(text) # 数据整理 data = pd.DataFrame(result, columns=['日期', '最高温度', '最低温度', '空气质量指数', '风力等级', '天气']) data[['最高温度', '最低温度', '空气质量指数', '风力等级']] = data[['最高温度', '最低温度', '空气质量指数', '风力等级']].astype(int) data['日期'] = pd.to_datetime(data['日期']) # 可视化分析 # 统计天气情况 weather_count = data['天气'].value_counts() weather_count = weather_count[:10] # 分词统计 seg_list = jieba.cut(' '.join(data['天气'].tolist())) words = {} for word in seg_list: if len(word) < 2: continue if word in words: words[word] += 1 else: words[word] = 1 # 绘制柱状图和词云图 plt.figure(figsize=(10, 5)) plt.bar(weather_count.index, weather_count.values) plt.title(f'{city}{year}年第{quarter}季度天气情况') plt.xlabel('天气') plt.ylabel('次数') plt.savefig('weather_bar.png') wordcloud = pd.DataFrame(list(words.items()), columns=['word', 'count']) mask_image = imageio.imread('cloud_mask.png') wordcloud.plot(kind='scatter', x='count', y='count', alpha=0.5, s=300, cmap='Reds', figsize=(10, 5)) for i in range(len(wordcloud)): plt.text(wordcloud.iloc[i]['count'], wordcloud.iloc[i]['count'], wordcloud.iloc[i]['word'], ha='center', va='center', fontproperties='SimHei') plt.axis('off') plt.imshow(mask_image, cmap=plt.cm.gray, interpolation='bilinear') plt.savefig('weather_wordcloud.png')这个python代码有错误,请改正以使该代码运行成功
06-13
这段代码的问题在于缺少了一些必要的包和文件,比如 cloud_mask.png,同时也需要安装 jieba 和 imageio 这两个包。以下是修改后的代码: ``` import pandas as pd import numpy as np import matplotlib.pyplot as plt import jieba import requests import re from io import BytesIO import imageio # 设置城市和时间 city = '上海' year = 2021 quarter = 2 # 爬取数据 url = f'http://tianqi.2345.com/t/wea_history/js/{city}/{year}/{quarter}.js' response = requests.get(url) text = response.content.decode('gbk') # 正则表达式匹配 pattern = re.compile(r'(\d{4}-\d{2}-\d{2})\|(\d{1,2})\|(\d{1,2})\|(\d{1,3})\|(\d{1,3})\|(\D+)\n') result = pattern.findall(text) # 数据整理 data = pd.DataFrame(result, columns=['日期', '最高温度', '最低温度', '空气质量指数', '风力等级', '天气']) data[['最高温度', '最低温度', '空气质量指数', '风力等级']] = data[['最高温度', '最低温度', '空气质量指数', '风力等级']].astype(int) data['日期'] = pd.to_datetime(data['日期']) # 可视化分析 # 统计天气情况 weather_count = data['天气'].value_counts() weather_count = weather_count[:10] # 分词统计 seg_list = jieba.cut(' '.join(data['天气'].tolist())) words = {} for word in seg_list: if len(word) < 2: continue if word in words: words[word] += 1 else: words[word] = 1 # 绘制柱状图和词云图 plt.figure(figsize=(10, 5)) plt.bar(weather_count.index, weather_count.values) plt.title(f'{city}{year}年第{quarter}季度天气情况') plt.xlabel('天气') plt.ylabel('次数') plt.savefig('weather_bar.png') wordcloud = pd.DataFrame(list(words.items()), columns=['word', 'count']) mask_image = imageio.imread('cloud_mask.png') plt.figure(figsize=(10, 5)) plt.imshow(mask_image, cmap=plt.cm.gray, interpolation='bilinear') plt.axis('off') plt.savefig('mask.png') wordcloud.plot(kind='scatter', x='count', y='count', alpha=0.5, s=300, cmap='Reds', figsize=(10, 5)) for i in range(len(wordcloud)): plt.text(wordcloud.iloc[i]['count'], wordcloud.iloc[i]['count'], wordcloud.iloc[i]['word'], ha='center', va='center', fontproperties='SimHei') plt.axis('off') plt.savefig('weather_wordcloud.png') ``` 注意:这里的 cloud_mask.png 是一个用于生成词云图的模板图片,可以替换成自己喜欢的图片。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值