C:\Users\gzh\AppData\Local\Temp\ipykernel_13008\931651818.py:5: MatplotlibDeprecationWarning:
In a future release, get_sample_data will automatically load numpy arrays. Set np_load to True to get the array and suppress this warning. Set asfileobj to False to get the path to the data file and suppress this warning.
C:\Users\gzh\AppData\Local\Temp\ipykernel_13008\952908628.py:23: MatplotlibDeprecationWarning:
In a future release, get_sample_data will automatically load numpy arrays. Set np_load to True to get the array and suppress this warning. Set asfileobj to False to get the path to the data file and suppress this warning.
from pyecharts import options as opts from pyecharts.charts import Funnel, Page from random import randint deffunnel_base() -> Funnel: c = ( Funnel().add("豪车", [list(z) for z inzip(['宝马', '法拉利', '奔驰', '奥迪', '⼤众', '丰⽥', '特斯拉'], [randint(1, 20) for _ inrange(7)])]).set_global_opts(title_opts=opts.TitleOpts(title="豪车漏⽃图")) ) return c funnel_base().render('./data/car_fnnel.html')
以7种车型及某个属性值绘制的漏⽃图,属性值⼤越靠近漏⽃的⼤端。
12 pyecharts⽇历图
import datetime import random from pyecharts import options as opts from pyecharts.charts import Calendar defcalendar_interval_1() -> Calendar: begin = datetime.date(2023, 1, 1) end = datetime.date(2023, 12, 31) data = [ [str(begin + datetime.timedelta(days=i)), random.randint(1000, 25000)] for i inrange(0, (end - begin).days + 1, 2) # 隔天统计 ] calendar = ( Calendar(init_opts=opts.InitOpts(width="1200px")).add( "", data, calendar_opts=opts.CalendarOpts(range_="2023")) .set_global_opts( title_opts=opts.TitleOpts(title="Calendar-2023年步数统计"), visualmap_opts=opts.VisualMapOpts( max_=25000, min_=1000, orient="horizontal", is_piecewise=True, pos_top="230px", pos_left="100px", ), ) ) return calendar calendar_interval_1().render('./data/calendar.html')
import json import os from pyecharts import options as opts from pyecharts.charts import Graph, Page defgraph_base() -> Graph: nodes = [ {"name": "cus1", "symbolSize": 10}, {"name": "cus2", "symbolSize": 30}, {"name": "cus3", "symbolSize": 20} ] links = [] for i in nodes: if i.get('name') == 'cus1': continue for j in nodes: if j.get('name') == 'cus1': continue links.append({"source": i.get("name"), "target": j.get("name")}) c = ( Graph() .add("", nodes, links, repulsion=8000) .set_global_opts(title_opts=opts.TitleOpts(title="customer-influence")) ) return c graph_base().render("./data/graph.html")
构建图,其中客户点1与其他两个客户都没有关系( link),也就是不存在有效边:
14 pyecharts⽔球图
from pyecharts import options as opts from pyecharts.charts import Liquid, Page from pyecharts.globalsimport SymbolType defliquid() -> Liquid: c = ( Liquid() .add("lq", [0.67, 0.30, 0.15]) .set_global_opts(title_opts=opts.TitleOpts(title="Liquid")) ) return c liquid().render('./data/liquid.html')
⽔球图的取值 [0.67, 0.30, 0.15]表⽰下图中的 三个波浪线 ,⼀般代表三个百分⽐:
15 pyecharts饼图
from pyecharts import options as opts from pyecharts.charts import Pie from random import randint defpie_base() -> Pie: c = ( Pie() .add("", [list(z) for z inzip(['宝马', '法拉利', '奔驰', '奥迪', '⼤众', '丰⽥', '特斯拉'], [randint(1, 20) for _ inrange(7)])]) .set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本⽰例")) .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}")) ) return c pie_base().render('./data/pie_pyecharts.html')
16 pyecharts极坐标图
import random from pyecharts import options as opts from pyecharts.charts import Page, Polar defpolar_scatter0() -> Polar: data = [(alpha, random.randint(1, 100)) for alpha inrange(101)] # r = random.randint(1, 100) # print(data) c = ( Polar() .add("", data, type_="bar", label_opts=opts.LabelOpts(is_show=False)) .set_global_opts(title_opts=opts.TitleOpts(title="Polar")) ) return c polar_scatter0().render('./data/polar.html')
极坐标表⽰为 (夹角,半径) ,如(6,94)表⽰夹⾓为6,半径94的点:
17 pyecharts词云图
from pyecharts import options as opts from pyecharts.charts import Page, WordCloud from pyecharts.globalsimport SymbolType words = [ ("Python", 100), ("C++", 80), ("Java", 95), ("R", 50), ("JavaScript", 79), ("C", 65) ] defwordcloud() -> WordCloud: c = ( WordCloud() # word_size_range: 单词字体⼤⼩范围 .add("", words, word_size_range=[20, 100], shape='cardioid') .set_global_opts(title_opts=opts.TitleOpts(title="WordCloud")) ) return c wordcloud().render('./data/wordcloud.html')
(“C”,65)表⽰在本次统计中C语⾔出现65次
18 pyecharts系列柱状图
from pyecharts import options as opts from pyecharts.charts import Bar from random import randint defbar_series() -> Bar: c = ( Bar() .add_xaxis(['宝马', '法拉利', '奔驰', '奥迪', '⼤众', '丰⽥', '特斯拉']) .add_yaxis("销量", [randint(1, 20) for _ inrange(7)]) .add_yaxis("产量", [randint(1, 20) for _ inrange(7)]) .set_global_opts(title_opts=opts.TitleOpts(title="Bar的主标题", subtitle="Bar的副标题")) ) return c bar_series().render('./data/bar_series.html')
19 pyecharts热⼒图
import random from pyecharts import options as opts from pyecharts.charts import HeatMap defheatmap_car() -> HeatMap: x = ['宝马', '法拉利', '奔驰', '奥迪', '⼤众', '丰⽥', '特斯拉'] y = ['中国','⽇本','南⾮','澳⼤利亚','阿根廷','阿尔及利亚','法国','意⼤利','加拿⼤'] value = [[i, j, random.randint(0, 100)] for i inrange(len(x)) for j inrange(len(y))] c = ( HeatMap() .add_xaxis(x) .add_yaxis("销量", y, value) .set_global_opts( title_opts=opts.TitleOpts(title="HeatMap"), visualmap_opts=opts.VisualMapOpts(), ) ) return c heatmap_car().render('./data/heatmap_pyecharts.html')
defdraw_chart(): fig = plt.figure(1, figsize=(16, 9)) axs = fig.add_subplot(111) axs.set_xticks([]) axs.set_yticks([]) # ⽣成数据 frames = Data.create() defanimate(fi): axs.cla() # clear last frame axs.set_xticks([]) axs.set_yticks([]) return axs.bar(list(range(Data.data_count)), # X [d.value for d in frames[fi]], # Y 1, # width color=[d.color for d in frames[fi]] # color ) # 动画展⽰ anim = animation.FuncAnimation(fig, animate, frames=len(frames)) plt.show() draw_chart()
21 pyecharts绘图属性设置⽅法
这是pyecharts中⼀般的绘图步骤:
from pyecharts.faker import Faker from pyecharts import options as opts from pyecharts.charts import Bar from pyecharts.commons.utils import JsCode defbar_base() -> Bar: c = ( Bar() .add_xaxis(Faker.choose()) .add_yaxis("商家A", Faker.values()) .set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本⽰例", subtitle="我是副标题")) ) return c bar_base().render('./data/bar.html')
from pyecharts.charts import Bar bar = Bar() bar.add_xaxis(["衬衫", "⽺⽑衫", "雪纺衫", "裤⼦", "⾼跟鞋", "袜⼦"]) bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90]) # render 会⽣成本地 HTML ⽂件,默认会在当前⽬录⽣成 render.html ⽂件 # 也可以传⼊路径参数,如 bar.render("mycharts.html") bar.render()
第⼀⾏代码: from pyecharts.charts import Bar,先上⼀张源码中包的结构图 :
bar.py模块中定义了类 Bar(RectChart),如下所⽰:
classBar(RectChart): """ <<< Bar Chart >>> Bar chart presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent. """
1)为什么根据图1中的包结构,为什么不这么写: from pyecharts.charts.basic_charts import Bar