我的python版本是3.6的,所以安装的是最新的pyecharts库,我在网上看了好多篇文章说的都是之前pyecharts的示例,所以,我自己费尽心思,终于搜到几个新版的示例,我自己也做了几个,有些感悟,就跟大家分享一波。
注意!!我们用pyecharts做可视化的时候是需要联网的!!
首先,数据可视化基础的图表:条形统计图(柱状图)!!
代码如下:
from pyecharts.charts import Bar
bar = Bar()#创建条形图对象,实例化!!
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])#Bar条形图add_xais()函数只有一个参数,横轴的商品名称。
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
bar.add_yaxis("商家B",[10, 50, 78, 24, 47, 13])#Bar条形图add_yaxis()函数有两个主要参数,第一个是名称,第二个是数据。
bar.render()#如果参数为空,则默认编辑器的路径
运行结果,会在对应的编辑器路径下面生成HTML文件。打开:
接着,我们再看一下其他版本的条形图。
from pyecharts.charts import Bar
from pyecharts import options as opts#导入pyecharts的配置项
B = Bar()
B.add_xaxis(['上衣','裤子','头巾','手套','帽子'])#给条形图添加横轴数据
B.add_yaxis('销量',[100, 50, 26, 47, 62])#给条形图纵轴添加数据
B.set_global_opts(title_opts=opts.TitleOpts(title='主标题:各商品的销量对比',subtitle='副标题:2020年'))#设置主标题和副标题
B.render('E:/Echarts_Images/test3.html')#将文件另存为E盘下的Echarts_Images文件夹,文件名是test3,文件格式是HTML。
jupyter notebook编辑器输出结果:
我们去E盘找到文件,如下图:
打开,效果如下:
接下来,还有另一种写法,我看视频说,好像是称为“链式写法”。我觉得这种写法还是有一定好处的,毕竟只需要实例化一次,就可以调用里面的函数了。
结果:
打开:
设置主题:
打开文件:
接着,折线图
import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.faker import Faker
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-基本示例"))
.render("E:/Echarts_Images/line_base.html")
)
import pyecharts.options as opts
from pyecharts.charts import Line
"""
Gallery 使用 pyecharts 1.1.0
参考地址: https://echarts.baidu.com/examples/editor.html?c=line-smooth
目前无法实现的功能:
暂无
"""
x_data = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
y_data = [820, 932, 901, 934, 1290, 1330, 1320]
(
Line()
.set_global_opts(
tooltip_opts=opts.TooltipOpts(is_show=False),
xaxis_opts=opts.AxisOpts(type_="category"),
yaxis_opts=opts.AxisOpts(
type_="value",
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
)
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="",
y_axis=y_data,
symbol="emptyCircle",
is_symbol_show=True,
is_smooth=True,
label_opts=opts.LabelOpts(is_show=False),
)
.render("E:/Echarts_Images/smoothed_line_chart.html")
)
import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.faker import Faker
y = Faker.values()
y[3], y[5] = None, None
c = (
Line()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", y, is_connect_nones=True)
.set_global_opts(title_opts=opts.TitleOpts(title="Line-连接空数据"))
.render("E:/Echarts_Images/line_connect_null.html")
)
然后,我们到饼图
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.faker import Faker
c = (
Pie()#定义饼图
.add("", [list(z) for z in zip(Faker.choose(), Faker.values())])
.set_colors(["blue", "green", "yellow", "red", "pink", "orange", "purple"])#设置各个板块的颜色
.set_global_opts(title_opts=opts.TitleOpts(title="Pie-设置颜色"))#设置标题
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
.render("E:/Echarts_Images/pie_set_color.html")#另存为文件
)
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.faker import Faker
c = (
Pie()
.add(
"",
[list(z) for z in zip(Faker.choose(), Faker.values())],
radius=["40%", "55%"],
label_opts=opts.LabelOpts(
position="outside",
formatter="{a|{a}}{abg|}\n{hr|}\n {b|{b}: }{c} {per|{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": "#aaa",
"width": "100%",
"borderWidth": 0.5,
"height": 0,
},
"b": {"fontSize": 16, "lineHeight": 33},
"per": {
"color": "#eee",
"backgroundColor": "#334455",
"padding": [2, 4],
"borderRadius": 2,
},
},
),
)
.set_global_opts(title_opts=opts.TitleOpts(title="Pie-富文本示例"))
.render("E:/Echarts_Images/pie_rich_label.html")
)
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.faker import Faker
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-玫瑰图示例"))
.render("E:/Echarts_Images/pie_rosetype.html")
)
import pyecharts.options as opts
from pyecharts.charts import Scatter
"""
Gallery 使用 pyecharts 1.1.0
参考地址: https://echarts.baidu.com/examples/editor.html?c=scatter-simple
目前无法实现的功能:
1、暂无
"""
data = [
[10.0, 8.04],
[8.0, 6.95],
[13.0, 7.58],
[9.0, 8.81],
[11.0, 8.33],
[14.0, 9.96],
[6.0, 7.24],
[4.0, 4.26],
[12.0, 10.84],
[7.0, 4.82],
[5.0, 5.68],
]
data.sort(key=lambda x: x[0])
x_data = [d[0] for d in data]
y_data = [d[1] for d in data]
(
Scatter(init_opts=opts.InitOpts(width="1600px", height="1000px"))
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="",
y_axis=y_data,
symbol_size=20,
label_opts=opts.LabelOpts(is_show=False),
)
.set_series_opts()
.set_global_opts(
xaxis_opts=opts.AxisOpts(
type_="value", splitline_opts=opts.SplitLineOpts(is_show=True)
),
yaxis_opts=opts.AxisOpts(
type_="value",
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
tooltip_opts=opts.TooltipOpts(is_show=False),
)
.render("E:/Echarts_Images/basic_scatter_chart.html")
)
from pyecharts import options as opts
from pyecharts.charts import Scatter
from pyecharts.faker import Faker
c = (
Scatter()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title="Scatter-显示分割线"),
xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),
yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),
)
.render("E:/Echarts_Images/scatter_splitline.html")
)
from pyecharts import options as opts
from pyecharts.charts import Scatter
from pyecharts.faker import Faker
c = (
Scatter()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title="Scatter-VisualMap(Size)"),
visualmap_opts=opts.VisualMapOpts(type_="size", max_=150, min_=20),
)
.render("E:/Echarts_Images/scatter_visualmap_size.html")
)
结果:
这些图,大多数我是看示例教程做的,有一些是自己做的,不管怎么说,觉得pyecharts还是个不错的可视化库,除了要联网这个缺陷外。
最后,感谢大家前来观看鄙人的文章,文中或有诸多不妥之处,还望指出和海涵。