其他
多图“预警”(二)
本文作者:钱梦璇
文字编辑:孙晓玲
技术总编:张 邯
爬虫俱乐部将于2020年1月5日至11日在湖北武汉举行为期一周的Stata编程技术定制培训,此次采取初级班和高级班分批次培训。课程通过案例教学模式,旨在帮助大家在短期内掌握Stata软件编程、金融计量知识和实证分析方法,使大家熟悉Stata核心的爬虫技术,以及Stata与其他软件交互的高端技术。目前正在火热招生中~详细培训大纲及报名方式,请点击《爬虫俱乐部2020第一期Stata编程训练营开始报名啦!》或点击文末阅读原文呦~
import requests
import json
url='https://movie.douban.com/j/search_subjects?type=movie&tag=%E7%83%AD%E9%97%A8&sort=recommend&page_limit=20&page_start=0'
headers={
'Accept':'*/*',
'Accept-Encoding':'gzip, deflate, br',
'Accept-Language':'zh-CN,zh;q=0.9',
'Connection':'keep-alive',
'Cookie':'bid=GG64rKF1uSo; ll="118254"; __utmz=30149280.1574226060.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmz=223695111.1574226060.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _vwo_uuid_v2=D20958CEF499378872990726113CB0E21|0b07de9babe9417544bca3ba22160c95; __yadk_uid=NIu4zXoawDz9m1KQRjtxprXcTPzJ88sL; ap_v=0,6.0; _pk_ses.100001.4cf6=*; __utma=30149280.561143398.1574226060.1574226060.1574241149.2; __utmb=30149280.0.10.1574241149; __utmc=30149280; __utma=223695111.1869265892.1574226060.1574226060.1574241149.2; __utmb=223695111.0.10.1574241149; __utmc=223695111; _pk_id.100001.4cf6=b4251776f0f5c706.1574226060.2.1574241187.1574226148.',
'Host':'movie.douban.com',
'Referer':'https://movie.douban.com/explore',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
'X-Requested-With':'XMLHttpRequest'
}
result=requests.get(url,headers=headers)
data=json.loads(result.text)['subjects']
#print(data)
movie=[]
rate=[]
for index in range(0,20):
movie.append(data[index]['title'])
rate.append(data[index]['rate'])
print("电影名称:%s" % movie)
print("豆瓣评分:%s" % rate)
顺序多图Page
1.默认布局
from pyecharts import options as opts
from pyecharts.charts import Bar,EffectScatter,Page
from pyecharts.globals import ThemeType #引入主题
from pyecharts.globals import SymbolType #导入涟漪特效图形符号
bar=(
Bar(init_opts=opts.InitOpts(theme=ThemeType.MACARONS)) #创建Bar实例
.add_xaxis(movie)
.add_yaxis("豆瓣评分",rate)
.set_global_opts(title_opts=opts.TitleOpts(title = "豆瓣电影-柱状图",subtitle = "按热度排序的前20名电影")
,legend_opts=opts.LegendOpts(pos_top="6%")
,xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)))
)
Scatter=(
EffectScatter()
.add_xaxis(movie)
.add_yaxis("豆瓣评分",rate,symbol=SymbolType.ARROW)
.set_global_opts(title_opts=opts.TitleOpts(title = "豆瓣电影-动态散点图",subtitle = "按热度排序的前20名电影")
,xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)))
)
page=(
Page()
.add(Scatter,bar)
)
page.render_notebook()
from pyecharts import options as opts
from pyecharts.charts import Bar,EffectScatter,Page,Line,Pie
from pyecharts.globals import ThemeType
from pyecharts.globals import SymbolType
bar=(
Bar(init_opts=opts.InitOpts(theme=ThemeType.MACARONS,width="500px",height="500px")) #创建Bar实例
.add_xaxis(movie)
.add_yaxis("豆瓣评分",rate)
.set_global_opts(title_opts=opts.TitleOpts(title = "豆瓣电影-最近热门电影",subtitle = "按热度排序的前20名电影")
,legend_opts=opts.LegendOpts(pos_top="6%")
,xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45))
)
)
Scatter=(
EffectScatter(init_opts=opts.InitOpts(width="500px",height="500px"))
.add_xaxis(movie)
.add_yaxis("豆瓣评分",rate,symbol=SymbolType.ARROW)
.set_global_opts(title_opts=opts.TitleOpts(title = "豆瓣电影-最近热门电影",subtitle = "按热度排序的前20名电影")
,xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)))
)
line=(
Line(init_opts=opts.InitOpts(width="500px",height="500px"))
.add_xaxis(movie)
.add_yaxis("豆瓣电影",rate,color="#675bba",label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)))
)
Pie=(
Pie(init_opts=opts.InitOpts(width="500px",height="500px"))
.add("",[("男",46.54),("女",53.46)])
.set_global_opts(title_opts=opts.TitleOpts(title="观影性别占比"))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}%"))
)
#page=Page()
page=Page(layout=Page.SimplePageLayout) # 需要自行调整每个 chart 的 height/width,显示效果在不同的显示器上可能不同
page.add(bar,Scatter,line,Pie)
page.render(r'E:\ Page-SimplePageLayout.html')
from pyecharts import options as opts
from pyecharts.charts import Bar,EffectScatter,Page,Line,Pie,Tab
from pyecharts.components import Table
from pyecharts.globals import ThemeType
from pyecharts.globals import SymbolType
bar=(
Bar(init_opts=opts.InitOpts(theme=ThemeType.MACARONS,width="500px",height="500px")) #创建Bar实例
.add_xaxis(movie)
.add_yaxis("豆瓣评分",rate)
.set_global_opts(title_opts=opts.TitleOpts(title = "豆瓣电影-最近热门电影",subtitle = "按热度排序的前20名电影")
,legend_opts=opts.LegendOpts(pos_top="6%")
,xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45))
)
)
Scatter=(
EffectScatter(init_opts=opts.InitOpts(width="500px",height="500px"))
.add_xaxis(movie)
.add_yaxis("豆瓣评分",rate,symbol=SymbolType.ARROW)
.set_global_opts(title_opts=opts.TitleOpts(title = "豆瓣电影-最近热门电影",subtitle = "按热度排序的前20名电影")
,xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)))
)
line=(
Line(init_opts=opts.InitOpts(width="500px",height="500px"))
.add_xaxis(movie)
.add_yaxis("豆瓣电影",rate,color="#675bba",label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)))
)
Pie=(
Pie(init_opts=opts.InitOpts(width="500px",height="500px"))
.add("",[("男",46.54),("女",53.46)])
.set_global_opts(title_opts=opts.TitleOpts(title="观影性别占比"))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}%")) #格式化标签输出内容 b代表数据项名称,c代表数值
)
headers=["电影名称","豆瓣评分"]
rows=[
['小丑',8.9],
['小小的愿望',5.1],
['克劳斯:圣诞节的秘密',8.4],
['鹿皮',7.0],
['最普通的恋爱',7.2],
['罗小黑战记',8.2],
['犯罪现场',6.5],
['双子杀手',7.1],
['寄生虫',8.7],
['续命之徒:绝命毒师电影',8.1],
['别告诉她',7.5],
['哪吒之魔童降世',8.5],
['我在雨中等你',8.2],
['友情以上',6.3],
['陈情令之生魂',4.5],
['天使陷落',6.4],
['好小子们',7.2],
['送我上青云',7.1],
['青春期猪头少年不做怀梦少女的梦',8.7],
['小丑回魂2',6.4]]
table=(
Table()
.add(headers,rows)
.set_global_opts(title_opts=opts.ComponentTitleOpts(title="豆瓣电影-最近热门电影"))
)
#page=Page()
page=Page(layout=Page.DraggablePageLayout) # 需要自行调整每个 chart 的 height/width,显示效果在不同的显示器上可能不同
page.add(bar,Scatter,line,Pie,table)
page.render(r'E:\ Page-DraggablePageLayout.html')
from pyecharts import options as opts
from pyecharts.charts import Bar,EffectScatter,Page,Line,Pie,Tab
from pyecharts.components import Table
from pyecharts.globals import ThemeType
from pyecharts.globals import SymbolType
bar=(
Bar(init_opts=opts.InitOpts(theme=ThemeType.MACARONS))
.add_xaxis(movie)
.add_yaxis("豆瓣评分",rate)
.set_global_opts(title_opts=opts.TitleOpts(title = "豆瓣电影-最近热门电影",subtitle = "按热度排序的前20名电影")
,legend_opts=opts.LegendOpts(pos_top="6%")
,xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45))
)
)
Scatter=(
EffectScatter()
.add_xaxis(movie)
.add_yaxis("豆瓣评分",rate,symbol=SymbolType.ARROW)
.set_global_opts(title_opts=opts.TitleOpts(title = "豆瓣电影-最近热门电影",subtitle = "按热度排序的前20名电影")
,xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)))
)
line=(
Line()
.add_xaxis(movie)
.add_yaxis("豆瓣电影",rate,color="#675bba",label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=45)))
)
Pie=(
Pie()
.add("",[("男",46.54),("女",53.46)])
.set_global_opts(title_opts=opts.TitleOpts(title="观影性别占比"))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}%")) )
headers=["电影名称","豆瓣评分"]
rows=[
['小丑',8.9],
['小小的愿望',5.1],
['克劳斯:圣诞节的秘密',8.4],
['鹿皮',7.0],
['最普通的恋爱',7.2],
['罗小黑战记',8.2],
['犯罪现场',6.5],
['双子杀手',7.1],
['寄生虫',8.7],
['续命之徒:绝命毒师电影',8.1],
['别告诉她',7.5],
['哪吒之魔童降世',8.5],
['我在雨中等你',8.2],
['友情以上',6.3],
['陈情令之生魂',4.5],
['天使陷落',6.4],
['好小子们',7.2],
['送我上青云',7.1],
['青春期猪头少年不做怀梦少女的梦',8.7],
['小丑回魂2',6.4]]
table=(
Table()
.add(headers,rows)
.set_global_opts(title_opts=opts.ComponentTitleOpts(title="豆瓣电影-最近热门电影"))
)
tab=Tab()
tab.add(bar,"柱状图示例")
tab.add(Scatter,"散点图示例")
tab.add(line, "线图示例")
tab.add(Pie, "饼图示例")
tab.add(table, "表格示例")
tab.render(r'E:\Tab选项卡多图.html')
from example.commons import Faker
from pyecharts import options as opts
from pyecharts.charts import Map,Timeline
tl = Timeline()
for i in range(2015, 2020):
map=(
Map()
.add("商家", [list(z) for z in zip(Faker.provinces, Faker.values())],"china")
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-{}年某些数据".format(i)),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
tl.add(map,"{}年".format(i))
tl.render(r'E:\时间线轮播多图.html')
“气功”研究哪家强
数据长、宽整型之另辟蹊径
10分钟带你走进宝藏数据库BvD
给你一个贴心的reshape应用
关于我们
微信公众号“Stata and Python数据分析”分享实用的stata、python等软件的数据处理知识,欢迎转载、打赏。我们是由李春涛教授领导下的研究生及本科生组成的大数据处理和分析团队。
1)必须原创,禁止抄袭;
2)必须准确,详细,有例子,有截图;
注意事项:
1)所有投稿都会经过本公众号运营团队成员的审核,审核通过才可录用,一经录用,会在该推文里为作者署名,并有赏金分成。
2)邮件请注明投稿,邮件名称为“投稿+推文名称”。
3)应广大读者要求,现开通有偿问答服务,如果大家遇到有关数据处理、分析等问题,可以在公众号中提出,只需支付少量赏金,我们会在后期的推文里给予解答。