建议使用jupyter notebook
本图代码参考自官网,仅供学习使用
柱状图1
#数据可视化库,需要使用pip install pyecharts
#官网: http://pyecharts.org/#/
import pandas as pd
from pyecharts.charts import Bar
bar = Bar()
#pyechars需要的数据只能是Python中内置结构,比如列表,字典,元组
# Numpy的数组,Series,DataFrame是不支持的,如果希望将Series中的数据做展示,可以使用tolist方法,将Series转为列表
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫"])
bar.add_yaxis("商家A", [5, 20, 36])
# render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
# 也可以传入路径参数,如 bar.render("mycharts.html")
# bar.render() 会渲染到本地的一个html页面上
bar.render_notebook() #渲染到当前的页面上
柱状图2
s = pd.Series([20,100,30,200,100],index=['a','b','c','d','e'])
# s.index
# s.values
# 可以调用tolist()方法,将Numpy的数组,Series转为列表
# s.index.tolist()
s.values.tolist()
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = (
Bar()
.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
.set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题"))
)
bar.render_notebook()
柱状图3
from pyecharts.charts import Bar
bar = Bar()
#pyechars需要的数据只能是Python中内置结构,比如列表,字典,元组
# Numpy的数组,Series,DataFrame是不支持的,如果希望将Series中的数据做展示,可以使用tolist方法,将Series转为列表
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫",'牛仔裤'])
bar.add_yaxis("商家A",[5,20,36,42])
bar.add_yaxis("商家B",[5,12,40,37])
# render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
# 也可以传入路径参数,如 bar.render("mycharts.html")
# bar.render() 会渲染到本地的一个html页面上
bar.render_notebook() #渲染到当前的页面上
堆叠图
#堆叠图
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = Bar()
#pyechars需要的数据只能是Python中内置结构,比如列表,字典,元组
# Numpy的数组,Series,DataFrame是不支持的,如果希望将Series中的数据做展示,可以使用tolist方法,将Series转为列表
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫",'牛仔裤','连衣裙','背心','围巾'])
bar.add_yaxis("销售方甲",[5,20,36,42,38,26,37],stack='stack1')
bar.add_yaxis("销售方乙",[5,12,40,37,29,12,39],stack='stack1')
bar.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
bar.set_global_opts(title_opts=opts.TitleOpts(title="柱状图数据堆叠图"))
# render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
# 也可以传入路径参数,如 bar.render("mycharts.html")
# bar.render() 会渲染到本地的一个html页面上
bar.render_notebook() #渲染到当前的页面上
饼图
#饼图示例
# zip可以将两个列表打包在一起
from pyecharts.charts import Pie
from pyecharts import options as opts
from pyecharts.charts import Page,Pie
datas = list(zip(['衬衫','羊毛衫','雪纺衫','裤子','高跟鞋','袜子'],[5,20,36,10,75,90]))
pie=Pie()
pie.add('',datas)
pie.set_global_opts(title_opts=opts.TitleOpts(title='Pie图'))
pie.set_series_opts(label_opts=opts.LabelOpts(formatter=" {b}:{c} "))
pie.render_notebook()
玫瑰图
from pyecharts.charts import Pie
from pyecharts import options as opts
from pyecharts.charts import Page,Pie
datas = list(zip(['衬衫','羊毛衫','雪纺衫','裤子','高跟鞋','袜子'],[5,20,36,10,75,90]))
rosetype=Pie()
rosetype.add("玫瑰图",datas,
radius=["30%", "75%"],center=["25%", "50%"],rosetype="radius",label_opts=opts.LabelOpts(is_show=False))
rosetype.add("玫瑰图",datas,
radius=["30%", "75%"],center=["75%", "50%"],rosetype="area")
rosetype.set_global_opts(title_opts=opts.TitleOpts(title="Pie-玫瑰图"))
#对图表进行渲染输出
rosetype.render_notebook()
from pyecharts.faker import Collector, Faker
Faker.choose()
Faker.values()
print(Faker.choose(),Faker.values())
datas = list(zip(['衬衫','羊毛衫','雪纺衫','裤子','高跟鞋','袜子'],[5,20,36,10,75,90]))
datas
玫瑰图2
#玫瑰图示例
#调用函数形式
#先导入要绘图的函数模组
from pyecharts import options as opts
from pyecharts.charts import Page, Pie
from pyecharts.commons.utils import JsCode
from pyecharts.faker import Collector, Faker
#使用绘图函数对表进行设置,并设置表的各种参数
def pie_rosetype() -> Pie:
v = Faker.choose()
c = (
Pie()
.add(
"",
[list(z) for z in zip(v, Faker.values())],
radius=["30%", "75%"],
center=["25%", "50%"],
rosetype="radius",
label_opts=opts.LabelOpts(is_show=False),
)
.add(
"",
[list(z) for z in zip(v, Faker.values())],
radius=["30%", "75%"],
center=["75%", "50%"],
rosetype="area",
)
.set_global_opts(title_opts=opts.TitleOpts(title="Pie-玫瑰图"))
)
return c
#调用函数,并对图表进行渲染输出
figure=pie_rosetype()
figure.render_notebook()
仪表盘
from pyecharts import charts
# 仪表盘
gauge = charts.Gauge()
gauge.add('Python使用小调查',[list(i) for i in zip(['好运来','张家乡','财神爷','赖某标','黄某强'],[38,42,24,37,59])])
#[('English',85),('Chinese',90),('Math',98)])
gauge.render_notebook()
折线图
#折线图
#调用函数形式
import pyecharts.options as opts
from pyecharts.faker import Faker
from pyecharts.charts import Line
c=(
Line()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title="Line折线"))
)
c.render_notebook()
雷达图
#雷达图
from pyecharts import options as opts
from pyecharts.charts import Page, Radar
v1 = [[4300, 10000, 28000, 35000, 50000, 19000]]
v2 = [[5000, 14000, 28000, 31000, 42000, 21000]]
#-> Radar:
def radar_base() -> Radar:
c = (
Radar()
.add_schema(
schema=[
opts.RadarIndicatorItem(name="销售", max_=6500),
opts.RadarIndicatorItem(name="管理", max_=16000),
opts.RadarIndicatorItem(name="信息技术", max_=30000),
opts.RadarIndicatorItem(name="客服", max_=38000),
opts.RadarIndicatorItem(name="研发", max_=52000),
opts.RadarIndicatorItem(name="市场", max_=25000),
]
)
.add("预算分配", v1)
.add("实际开销", v2)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="Radar图"))
)
return c
c=radar_base()
c.render_notebook()
词云图
#词云图
#定义函数形式显示
from pyecharts import options as opts
from pyecharts.charts import Page, WordCloud
from pyecharts.globals import SymbolType
words = [("Sam S Club", 10000),("Macys", 6181),("Amy Schumer", 4386),("Jurassic World", 4055),
("Charter Communications", 2467),("Chick Fil A", 2244),("Planet Fitness", 1868),
("Pitch Perfect", 1484),("Express", 1112),("Home", 865),("Johnny Depp", 847),("Lena Dunham", 582)]
def wordcloud_base() -> WordCloud:
c = (
WordCloud()
.add("", words,word_size_range=[20, 100])
.set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-基本示例"))
)
return c
c=wordcloud_base()
c.render_notebook()
散点图
from pyecharts.charts import Scatter
scatter = Scatter()
scatter.add_xaxis(['衬衫','羊毛衫', '雪纺衫', '裤子','高跟鞋','袜子'])
scatter.add_yaxis('商家A',[5, 20, 36,10, 75, 90])
scatter.set_global_opts(title_opts=opts.TitleOpts(title='Scatter-基本示例'))
scatter.render_notebook()
利用Geo绘制地理坐标图表
#利用Geo绘制地理坐标图表
'''
add_schema() :控制地图类型、视角中心点等
add():添加图表名称、传入数据集、选择geo图类型、调整图例等
set_series_opts() :系列配置项,可配置图元样式、文字样式、标签样式、点线样式等
set_global_opts() : 全局配置项,可配置标题、动画、坐标轴、图例等
render_notebook() : 在notebook中渲染显示图表
Geo()模块还有几个功能函数:
add_coordinate() : 新增一个坐标点
add_coordinate_json() :以json形式新增多个坐标点
get_coordinate() :根据地点查询对应坐标
'''
#python的链式调用,使代码更加简洁
from pyecharts.faker import Collector, Faker
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType
c = (
Geo()
.add_schema(maptype="china")
.add("geo", [list(z) for z in zip(Faker.provinces, Faker.values())])
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(),
title_opts=opts.TitleOpts(title="Geo-基本示例"),
)
)
c.render_notebook()
利用Geo绘制热力图表示
#你可以更换数据展现形式,比如用热力图表示,只需在add()函数中添加参数:type_=ChartType.HEATMAP
from pyecharts.faker import Collector, Faker
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType
c = (
Geo()
.add_schema(maptype="china")
.add("geo", [list(z) for z in zip(['江苏','浙江','湖北','湖南','河南'], [22,34,27,100,42])],type_=ChartType.HEATMAP)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(),
title_opts=opts.TitleOpts(title="全国各省xx数据分布"),
)
)
c.render_notebook()
以北京作为底图,展示北京市各区县xx数据的分布
#以北京作为底图,展示北京市各区县xx数据的分布。将函数add_schema(maptype="china")的maptype="china"修改为maptype="北京"
from pyecharts.faker import Collector, Faker
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType
c = (
Geo()
.add_schema(maptype="北京")
.add(
"geo",
[list(z) for z in zip(['大兴区','房山区','海淀区','朝阳区','东城区'], [150,100,300,200,500])],
type_=ChartType.EFFECT_SCATTER,
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(),
title_opts=opts.TitleOpts(title="Geo-HeatMap"),
)
)
c.render_notebook()
你可以修改地图的背景色
#你可以修改地图的背景色,在函数add_schema()中添加参数itemstyle_opts=opts.ItemStyleOpts(color="#323c48", border_color="#111"),
#颜色和边缘颜色可设任意色值。
from pyecharts.faker import Collector, Faker
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType
c = (
Geo()
.add_schema(maptype="北京",
itemstyle_opts=opts.ItemStyleOpts(color="#383c48", border_color="#111"))
.add(
"geo",
[list(z) for z in zip(['大兴区','房山区','海淀区','朝阳区','东城区'], [1500,10,300,20,5])],
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(),
title_opts=opts.TitleOpts(title="北京市各区县xx数据分布"),
)
)
c.render_notebook()
地理空间流动图
'''2、动态展示
pyecharts可以生成地理空间流动图,用来表示航班数量、人口流动等等。
下面以全国主要城市航班流动图为例(虚拟数据):
'''
from pyecharts.faker import Collector, Faker
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType
c = (
Geo()
.add_schema(maptype="china")
.add(
"",
[("深圳", 120), ("哈尔滨", 66), ("杭州", 77), ("重庆", 88), ("上海", 100), ("乌鲁木齐", 30),("北京", 30),("武汉",70),("南昌",48)],
type_=ChartType.EFFECT_SCATTER,
color="green",
)
.add(
"geo",
[("北京", "上海"), ("武汉", "深圳"),("重庆", "杭州"),
("南昌","北京"),("哈尔滨", "重庆"),("乌鲁木齐", "哈尔滨"),("深圳", "乌鲁木齐"),("武汉", "北京")],
type_=ChartType.LINES,
effect_opts=opts.EffectOpts(
symbol=SymbolType.ARROW, symbol_size=6, color="blue"
),
linestyle_opts=opts.LineStyleOpts(curve=0.2),
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="全国主要城市航班路线和数量"))
)
c.render_notebook()
绘制地图
#利用Map模块绘制地图
#Map模块的使用方法和Geo类似,数据展现在地图上,首先导入相关库:
from pyecharts.faker import Collector, Faker
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType
from pyecharts.charts import Map
c = (
Map()
.add("省份地图", [list(z) for z in zip(['江苏','浙江','湖北','湖南','河南'], [22,100,27,53,42])], "china")
.set_global_opts(title_opts=opts.TitleOpts(title="Map-基本示例"),
visualmap_opts=opts.VisualMapOpts(max_=200))
)
c.render_notebook()
世界地图
#世界地图:
from pyecharts.faker import Collector, Faker
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType
from pyecharts.charts import Map
c = (
Map()
.add("世界地图", [list(z) for z in zip(['China','Canada','Brazil','United States','Russia'], [22,100,27,53,42])], "world")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-世界地图"),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
c.render_notebook()
上海地图
from pyecharts.faker import Collector, Faker
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType
from pyecharts.charts import Map
c = (
Map()
.add("商家A", [list(z) for z in zip(['嘉定区','松江区','宝山区','浦东新区','奉贤区'], [22,100,27,53,42])], "上海")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-上海地图"),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
c.render_notebook()
t Geo
from pyecharts.globals import ChartType, SymbolType
from pyecharts.charts import Map
c = (
Map()
.add(“商家A”, [list(z) for z in zip([‘嘉定区’,‘松江区’,‘宝山区’,‘浦东新区’,‘奉贤区’], [22,100,27,53,42])], “上海”)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title=“Map-上海地图”),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
c.render_notebook()
![在这里插入图片描述](https://img-blog.csdnimg.cn/2021062017012474.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQ5NTAxNDUz,size_16,color_FFFFFF,t_70)
更多图详见官网