描述
echarts是百度的一个开源的数据可视化工具,一个纯 Javascript 的图表库。凭借着良好的交互性,精巧的图表设计,得到了众多开发者的认可。
本篇基于简单实例,主要介绍词云、富文本饼图、组合饼图、热力图几种图形使用,作为新手练习或使用参考。
更多介绍与示例详见:pyecharts中文文档
脚本
- 词云
from pyecharts.charts import WordCloud
from pyecharts import options as opts
from pyecharts.globals import ThemeType
# 词云-任务列表
def show_WordCloud():
words = [
("任务1", 700),
("任务2", 680),
("任务3", 750),
("任务4", 550),
("任务5", 750),
("任务6", 600),
("任务7", 650),
]
c = (
WordCloud(init_opts=opts.InitOpts(theme=ThemeType.DARK,width="1000px", height="600px")) #暗黑主题,自定义宽度和高度
.add("", words, word_size_range=[30, 50], shape= "cardioid") #文字大小范围,形状为钻石
.set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-shape-diamond")) # 标题名称
.render("./html/wordcloud_diamond.html") # 输出文件路径名称
)
show_WordCloud()
效果如图:
2.富文本饼图
from pyecharts.charts import Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType
# 富文本饼图
def show_pie_usage():
c = (
Pie(init_opts=opts.InitOpts(theme=ThemeType.DARK,width="1200px", height="600px"))
.add(
"",
[['任务1', 42],['任务2', 6], ['任务3', 13], ['任务4', 13], ['任务5', 26]],
radius=["30%", "45%"],
label_opts=opts.LabelOpts(
position="outside",
formatter=" {a|工作目标}{abg|}\n{hr|}\n {b|{b}: }{per|{d}%} ", # 饼图、仪表盘、漏斗图: {a}(系列名称),{b}(数据项名称),{c}(数值), {d}(百分比)
background_color="#eee",
border_color="#aaa",
border_width=1,
border_radius=4,
rich={ #富文本相关属性
"a": {"color": "#999", "lineHeight": 22, "align": "center"},
"abg": {
"backgroundColor": "#e3e3e3",
"width": "100%",
"align": "right",
"height": 22,
"borderRadius": [4, 4, 0, 0],
},
"hr": {
"borderColor": "#404040",
"width": "100%",
"borderWidth": 0.5,
"height": 0,
},
"b": {"fontSize": 16, "lineHeight": 33 ,"color": "#999", "align": "left" ,"vertical_align": None ,"border_width" :"100%" ,"height" : 33 },
"per": {
"color": "#eee",
"backgroundColor": "#334455",
"padding": [2, 4],
"borderRadius": 2,
},
},
),
)
.set_global_opts(title_opts=opts.TitleOpts(title="Pie"))
.render("./html/pie_rich_label.html")
)
show_pie_usage()
效果如图:
3. 组合饼图
from pyecharts.charts import Pie
from pyecharts import options as opts
from pyecharts.globals import ThemeType
# 组合饼图
def new_label_opts() :
return opts.LabelOpts(formatter= " {b}:{c}{{d}%} \n", position="outside")
def show_fourpie_work():
c = (
Pie(init_opts=opts.InitOpts(theme=ThemeType.DARK,width="1200px", height="600px"))
.add(
"任务1",
[list(z) for z in zip(["A", "B","C","D"], [94, 34,11,16])],
center=["25%", "30%"],
radius=[50, 70],
label_opts=new_label_opts(),
)
.add(
"任务2",
[list(z) for z in zip(["A", "B","C","D"], [84, 120,58,39])],
center=["70%", "30%"],
radius=[50, 70],
label_opts=new_label_opts(),
)
.add(
"任务3",
[list(z) for z in zip(["A", "B","C","D"], [35, 40,51,30])],
center=["25%", "70%"],
radius=[50, 70],
label_opts=new_label_opts(),
)
.add(
"任务4",
[list(z) for z in zip(["A", "B"], [99,419])],
center=["70%", "70%"],
radius=[50, 70],
label_opts=new_label_opts(),
)
.render("./html/test.html")
)
show_fourpie_work()
效果如图:
4. 热力图
from pyecharts.charts import HeatMap
from pyecharts import options as opts
from pyecharts.globals import ThemeType
import pandas as pd
# 热力图-各功能项目使用情况
def show_heat_usage():
"读取文件数据画产品版图"
data =[]
project_list = ['山西', '广西', '辽宁', '黑龙江', '宁夏']
menu_list = ['A','B','C','D','E','F']
df = pd.read_excel('./项目情况.xlsx', sheet_name='项目使用', usecols=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
for x in range(len(menu_list)):
for y in range(len(project_list)):
data.append([x, y, int(df.iat[x, y+1])])
print(data)
# 以上是通过文件读取数据
c = (
HeatMap(init_opts=opts.InitOpts(theme=ThemeType.DARK,width="1200px", height="580px")) # ,width="1440px", height="720px"
.add_xaxis(menu_list)
.add_yaxis(
"人均使用次数",
project_list,
data,
label_opts=opts.LabelOpts(is_show=True, position="inside"),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="数据情况"),
tooltip_opts=opts.TooltipOpts(axis_pointer_type="cross"), # 指示器类型
visualmap_opts=opts.VisualMapOpts(min_=0, max_=25, is_calculable=True,is_show = False, orient="horizontal", pos_left="center"),
xaxis_opts=opts.AxisOpts(type_="category", is_show=True, axislabel_opts={"rotate": 20}), #X轴如果文字过长可以设置倾斜程度
)
.render("./html/test.html")
)
show_heat_usage()
文件内数据样式:
data输出的数据格式如下,其中[0, 0, 26]代表X轴为A,Y轴为山西的值:
[[0, 0, 26], [0, 1, 60], [0, 2, 50], [0, 3, 47], [0, 4, 27], [1, 0, 25], [1, 1, 27], [1, 2, 16], [1, 3, 5], [1, 4, 17], [2, 0, 21], [2, 1, 11], [2, 2, 23], [2, 3, 7], [2, 4, 0], [3, 0, 0], [3, 1, 1], [3, 2, 0], [3, 3, 1], [3, 4, 0], [4, 0, 0], [4, 1, 0], [4, 2, 1], [4, 3, 0], [4, 4, 1], [5, 0, 0], [5, 1, 0], [5, 2, 1], [5, 3, 1], [5, 4, 0]]
效果如图:
相关文章:【python】连接查询mysql数据库(pymysql)