pyechart
官网地址:pyecharts - A Python Echarts Plotting Library built with love.
柱状图:
from pyecharts.charts import Bar from pyecharts import options as opts # 内置主题类型可查看 pyecharts.globals.ThemeType from pyecharts.globals import ThemeType bar = (Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT)).add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]).add_yaxis("商家A", [5, 20, 36, 10, 75, 90]).add_yaxis("商家B", [15, 6, 45, 20, 35, 66]).set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题")) )
柱状图解:
from pyecharts.charts import Bar from pyecharts import options as opts from pyecharts.globals import ThemeType bar = (Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK)).add_xaxis(["男孩","女孩","男人","女人"]).add_yaxis(series_name="肯德基",y_axis=[25,200,3,79]).add_yaxis(series_name="麦当劳",y_axis=[4,68,209,48]).set_global_opts(title_opts={"text":"主标题","subtext":"副标题"},#左上角顶标题 #键的参数需要规定才成功legend_opts=opts.LegendOpts(is_show=True),visualmap_opts=opts.VisualMapOpts(#视觉映射配置表颜色的状态is_show=True,min_=20,max_=200,),toolbox_opts=opts.ToolboxOpts(#工具箱子设置is_show=True),datazoom_opts=opts.DataZoomOpts(#中央底部区域缩放配置is_show=True),tooltip_opts=opts.TooltipOpts(#触碰时提示框配置is_show=True,background_color="#ff5384",textstyle_opts=opts.TextStyleOpts(color="blue",font_size=20)) ) ) bar.render("pyecharts_柱状图.html")
地图表:
import requests from pyecharts.charts import Map from snapshot_selenium import snapshot from pyecharts.render import make_snapshot import datetime from pyecharts import options as opts from pyecharts.globals import ThemeType url="https://lab.isaaclin.cn/nCoV/api/area" headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'} html_json = requests.get(url=url,headers=headers).json() china_list = [] for item in html_json["results"]:if item["countryName"] == "中国":china_list.append([item["provinceShortName"],item["confirmedCount"]]) print(china_list) date_now = datetime.date.today() #数据格式: [['山东',100],['湖北',100]] map = (Map(init_opts=opts.InitOpts(theme=ThemeType.DARK)).add("中国疫情可视化",china_list,"china").set_global_opts(title_opts=opts.TitleOpts(title="中国疫情可视化",subtitle=f"更新日期{date_now}"),visualmap_opts=opts.VisualMapOpts(is_show = True,min_ = 0,max_=100000)) ) make_snapshot(snapshot,map.render(),"国内疫情情况.png")
饼图:
from pyecharts import options as opts from pyecharts.charts import Pie result = [['哈士奇',134],['萨摩耶',72],['泰迪',136],['金毛', 129], ['牧羊犬', 75], ['吉娃娃', 76], ['柯基', 56]] pie = (Pie().add("饼图对比",result).set_colors(["blue", "green", "yellow", "red", "pink", "orange", "purple"]) .set_global_opts(title_opts=opts.TitleOpts(title="爱宠调查图")) # 饼图:{a}(系列名称),{b}(数据项名称),{c}(数值), {d}(百分比).set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}--->{d}%")) ) pie.render("饼图.html")
导出:
基础导出:
bar.render("pyecharts_柱状图.html")
渲染图片导出:
from snapshot_selenium import snapshot from pyecharts.render import make_snapshot make_snapshot(snapshot,map.render(),"国内疫情情况.png")