python pyecharts 库 数据可视化
作者微信公众号:小卫星
操作系统: Windows 10
IDE: PyCharm Community 2018.1
Python 3.7
$ pip install pyecharts
或者源码安装:
$ git clone https://github.com/pyecharts/pyecharts.git
$ cd pyecharts
$ pip install -r requirements.txt
$ python setup.py install
安装地图:
$ pip install echarts-countries-pypkg
$ pip install echarts-china-provinces-pypkg
$ pip install echarts-china-cities-pypkg
$ pip install echarts-china-counties-pypkg
$ pip install echarts-china-misc-pypkg
$ pip install echarts-united-kingdom-pypkg
import random from pyecharts import Polar, Page, Style import math #from app.charts.constants import WIDTH, HEIGHT WIDTH = 600 HEIGHT = 400 def create_charts(): page = Page() style = Style(width=WIDTH, height=HEIGHT) data = [] for i in range(361): t = i / 180 * math.pi r = math.sin(2 * t) * math.cos(2 * t) data.append([r, i]) chart = Polar("极坐标系-画小花", **style.init_style) chart.add("Flower", data, start_angle=0, symbol=None, axis_range=[0, None]) page.add(chart) return page create_charts().render()运行完之后,在文件目录下会出现一个reader.html文件,用浏览器打开,即可以看到动态散点图:
地图测试:
from pyecharts import GeoLines, Style, Page style = Style( title_top="#fff", title_pos = "center", width = 600, height = 300, background_color="#404a59" ) style_geo = style.add( is_label_show=True, line_curve=0.2, line_opacity=0.6, legend_text_color="#eee", legend_pos="right", geo_effect_symbol="plane", geo_effect_symbolsize=15, label_color=['#a6c84c', '#ffa022', '#46bee9'], label_pos="right", label_formatter="{b}", label_text_color="#eee", ) def create_charts(): page = Page() data_guangzhou = [ ["广州", "上海"], ["广州", "北京"], ["广州", "南京"], ["广州", "重庆"], ["广州", "兰州"], ["广州", "杭州"] ] data_beijing = [ ["北京", "上海"], ["北京", "广州"], ["北京", "南京"], ["北京", "重庆"], ["北京", "兰州"], ["北京", "杭州"] ] charts = GeoLines("GeoLines-默认示例", **style.init_style) charts.add("从广州出发", data_guangzhou, is_legend_show=False) page.add(charts) charts = GeoLines("GeoLines-稍加配置", **style.init_style) charts.add("从广州出发", data_guangzhou, **style_geo) page.add(charts) charts = GeoLines("GeoLines-多例模式", **style.init_style) charts.add("从广州出发", data_guangzhou, **style_geo) charts.add("从北京出发", data_beijing, **style_geo) page.add(charts) charts = GeoLines("GeoLines-单例模式", **style.init_style) charts.add("从广州出发", data_guangzhou, **style_geo) charts.add("从北京出发", data_beijing, legend_selectedmode="single", **style_geo) page.add(charts) return page create_charts().render()
直接复制图片,它只有流图没有地图,显然它是一个多层结构。
3、参考资料
http://pyecharts.org/#/zh-cn/
http://pyecharts.herokuapp.com/geo