目录
十二.PyEcharts常用视图(1)
1-PyEcharts-Faker
2-常用视图
2.1 柱状图
普通柱状图
条形柱状图
堆叠柱状图
象形柱状图
颜色自定义
2.2 折线图
基本折线图
折线图常用属性
面积图
堆叠面积图
2.3 散点图
2.4 双轴图
2.5 饼图
基本饼图
空心饼图
玫瑰图
图例调整
文字调整
2.6 雷达图
单例模式
2.7 时间轴
感谢度娘及开发者.
1-PyEcharts-Faker
from pyecharts.faker import Faker
方法属性 | 说明 | 对应内容 |
---|---|---|
Faker.choose() | [“衬衫”, “毛衣”, “领带”, “裤子”, “风衣”, “高跟鞋”, “袜子”] | |
Faker.values() | [106, 111, 145, 33, 20, 138, 141] | |
Faker.drinks | [“可乐”, “雪碧”, “橙汁”, “绿茶”, “奶茶”, “百威”, “青岛”] | |
Faker.phones | [“小米”, “三星”, “华为”, “苹果”, “魅族”, “VIVO”, “OPPO”] | |
Faker.fruits | [“草莓”, “芒果”, “葡萄”, “雪梨”, “西瓜”, “柠檬”, “车厘子”] | |
Faker.animal | [“河马”, “蟒蛇”, “老虎”, “大象”, “兔子”, “熊猫”, “狮子”] | |
Faker.cars | [“宝马”, “法拉利”, “奔驰”, “奥迪”, “大众”, “丰田”, “特斯拉”] | |
Faker.dogs | [“哈士奇”, “萨摩耶”, “泰迪”, “金毛”, “牧羊犬”, “吉娃娃”, “柯基”] | |
Faker.week | [“周一”, “周二”, “周三”, “周四”, “周五”, “周六”, “周日”] | |
Faker.week_en | [‘Saturday’, ‘Friday’, ‘Thursday’, ‘Wednesday’, ‘Tuesday’, ‘Monday’, ‘Sunday’] | |
Faker.clock | [‘12a’,‘1a’,‘2a’,‘3a’,‘4a’,‘5a’,‘6a’,...,4p’,‘5p’,‘6p’,‘7p’,‘8p’,‘9p’,‘10p’,‘11p’] | |
Faker.visual_color | [ “#313695”, “#4575b4”, “#74add1”, “#abd9e9”, .....] | |
Faker.months | ‘1月’, ‘2月’, ‘3月’, ‘4月’, ‘5月’, ‘6月’, ‘7月’, ‘8月’, ‘9月’, ‘10月’, ‘11月’, ‘12月’] | |
Faker.provinces | [“广东”, “北京”, “上海”, “江西”, “湖南”, “浙江”, “江苏”] | |
Faker.guangdong_city | [“汕头市”, “汕尾市”, “揭阳市”, “阳江市”, “肇庆市”, “广州市”, “惠州市”] | |
Faker.country | [‘China’, ‘Canada’, ‘Brazil’, ‘Russia’, ‘United States’, ‘Africa’, ‘Germany’] | |
Faker.days_attrs | [‘0天’,‘1天’,‘2天’,‘3天’,‘4天’,‘5天’,‘6天’, ‘7天’,‘8天’,... ...,‘28天’,‘29天’] | |
Faker.days_values | 生成的从1-30之间的随机天数,顺序是打乱的 |
2-常用视图
import pyecharts.options as opts from pyecharts.faker import Faker from pyecharts.globals import ThemeType
2.1 柱状图
普通柱状图
from pyecharts.charts import Bar bar_img = (Bar().add_xaxis(Faker.choose()).add_yaxis("淘宝",Faker.values()).add_yaxis("京东",Faker.values()).set_global_opts(# 旋转x轴的字体(更美观,防止重合.)xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-45))) ) bar_img.render_notebook()
条形柱状图
横着显示.
bar_img = (Bar().add_xaxis(Faker.choose()).add_yaxis("淘宝",Faker.values()).add_yaxis("京东",Faker.values())# 翻转轴.reversal_axis().set_series_opts(# 字体显示在右边label_opts=opts.LabelOpts(position="right")) ) bar_img.render_notebook()
堆叠柱状图
# 堆叠柱状图 bar_img = (Bar().add_xaxis(Faker.choose()).add_yaxis("淘宝",Faker.values(),stack="one").add_yaxis("京东",Faker.values(),stack="one").set_series_opts(#柱的值显不显示.label_opts=opts.LabelOpts(is_show=True)) ) bar_img.render_notebook()
象形柱状图
from pyecharts.charts import PictorialBar from pyecharts.globals import SymbolType pbar_img = (PictorialBar().add_xaxis(["小明","小红","小刚","王强","张伟","大伟","小帅"]).add_yaxis("票数",Faker.values(),# label_opts=opts.LabelOpts(is_show=False), #柱的值显不显示.(默认显示)label_opts=opts.LabelOpts(position="right"),#值在哪边显示.symbol=SymbolType.ROUND_RECT, # 符号类型symbol_repeat="fixed", # 重复方式symbol_size=30, # 符号大小is_symbol_clip=True # 特征长度.(不一样长)).reversal_axis() #轴翻转..set_global_opts(xaxis_opts=opts.AxisOpts(is_show=False), #不显示x轴yaxis_opts=opts.AxisOpts(axistick_opts=opts.AxisTickOpts(is_show=False), # y轴刻度隐藏axisline_opts=opts.AxisLineOpts(is_show=False) # y轴线隐藏)) ) pbar_img.render_notebook()
颜色自定义
--嵌套js代码,修改颜色.
from pyecharts.commons.utils import JsCode ##js代码看if条件. color_function = """function(params){if(params.value>=0 && params.value<=49){return 'red';} else if (params.value>=50 && params.value<=99){return 'green';}else if (params.value>=100 && params.value<=149){return 'blue';}} """ bar_img = (Bar().add_xaxis(Faker.choose())# .add_yaxis("淘宝",Faker.values(),itemstyle_opts=opts.ItemStyleOpts(color="blue")).add_yaxis("淘宝",Faker.values(),itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_function))).add_yaxis("京东",Faker.values(),itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_function))).add_yaxis("天猫",Faker.values(),itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_function))) ) bar_img.render_notebook()
2.2 折线图
from pyecharts.charts import Line
基本折线图
line_img = (Line().add_xaxis(Faker.choose()).add_yaxis("淘宝",Faker.values()).add_yaxis("京东",Faker.values(),is_smooth=True) # 平滑曲线(曲线).set_global_opts(#添加提示线.tooltip_opts=opts.TooltipOpts(trigger="axis")) ) line_img.render_notebook()
折线图常用属性
line_img = (Line(init_opts=opts.InitOpts(width="1000px",height="600px") #设置像素大小.).add_xaxis(Faker.week).add_yaxis("淘宝",Faker.values()).add_yaxis("京东",Faker.values(),symbol="triangle", # 形状symbol_size=15, # 大小# 设置线条样式linestyle_opts=opts.LineStyleOpts(# 颜色, 宽度, 样式.color="green",width=3,type_="dashed"),label_opts=opts.LabelOpts(is_show=False), # 不显示标签# 设置点的属性itemstyle_opts=opts.ItemStyleOpts(border_color="red",border_width=2),# 标注点markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max"),# 最大值标注opts.MarkPointItem(type_="min"),# 最大值标注]),# 标注线markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")]))#添加提示线..set_global_opts(tooltip_opts=opts.TooltipOpts(trigger="axis") ) ) line_img.render_notebook()
面积图
import pyecharts.options as opts from pyecharts.charts import Line from pyecharts.faker import Faker line_img = (Line().add_xaxis(Faker.choose()).add_yaxis("商家A", Faker.values(), is_smooth=True).add_yaxis("商家B", Faker.values(), is_smooth=True).set_series_opts(# 添加面积,透明度0.5areastyle_opts=opts.AreaStyleOpts(opacity=0.5),label_opts=opts.LabelOpts(is_show=False), #显不显示点值(默认显示). ).set_global_opts(title_opts=opts.TitleOpts(title="面积图"),xaxis_opts=opts.AxisOpts(# 紧贴 Y 轴(默认不紧贴,False紧贴)boundary_gap=False, ),#添加提示线.tooltip_opts=opts.TooltipOpts(trigger="axis") ) ) line_img.render_notebook()
堆叠面积图
line_img = (Line().add_xaxis(Faker.week).add_yaxis("淘宝",stack="堆叠",y_axis=Faker.values(),areastyle_opts=opts.AreaStyleOpts(opacity=0.7),# 添加面积部分颜色label_opts=opts.LabelOpts(is_show=False) # 隐藏数据).add_yaxis("天猫",stack="堆叠",y_axis=Faker.values(),areastyle_opts=opts.AreaStyleOpts(opacity=0.7),# 添加面积部分颜色label_opts=opts.LabelOpts(is_show=False)).add_yaxis("京东",stack="堆叠",y_axis=Faker.values(),areastyle_opts=opts.AreaStyleOpts(opacity=0.7),# 添加面积部分颜色label_opts=opts.LabelOpts(is_show=False)).add_yaxis("拼多多",stack="堆叠",y_axis=Faker.values(),areastyle_opts=opts.AreaStyleOpts(opacity=0.7),# 添加面积部分颜色label_opts=opts.LabelOpts(is_show=False)) .set_global_opts(tooltip_opts=opts.TooltipOpts(trigger="axis"),# 紧贴y轴xaxis_opts=opts.AxisOpts(boundary_gap=False), ) ) line_img.render_notebook()
2.3 散点图
from pyecharts.charts import Scatter from pyecharts.faker import Faker sca_img = (Scatter().add_xaxis(Faker.choose()).add_yaxis("商家A", Faker.values()).add_yaxis("商家B", Faker.values()).add_yaxis("商家C", Faker.values()).add_yaxis("商家D", Faker.values()).set_global_opts(#设置主标题title_opts=opts.TitleOpts(title="Scatter-VisualMap(Color)"),visualmap_opts=opts.VisualMapOpts(max_=150), #显不显示网格线:(x轴和y轴的)xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),) ) sca_img.render_notebook()
2.4 双轴图
import pyecharts.options as opts from pyecharts.charts import Bar, Line x_data = ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"] bar = (Bar().add_xaxis(xaxis_data=x_data).add_yaxis(series_name="蒸发量",y_axis=[2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3],label_opts=opts.LabelOpts(is_show=False), # 不在展示坐标轴的数字).add_yaxis(series_name="降水量",y_axis=[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3],label_opts=opts.LabelOpts(is_show=False), # 不在展示坐标轴的数字).extend_axis(yaxis=opts.AxisOpts(name="温度",type_="value", # 连续数值min_=0,max_=25,interval=5, # 间隔为5axislabel_opts=opts.LabelOpts(formatter="{value} °C"),)).set_global_opts(tooltip_opts=opts.TooltipOpts(# 提示线展示, 触发类型;轴触发 ,格式化x轴标签is_show=True, trigger="axis",axis_pointer_type="cross"),xaxis_opts=opts.AxisOpts(# 坐标轴指示配置项,展示x轴阴影axispointer_opts=opts.AxisPointerOpts(is_show=True, type_="shadow"),),yaxis_opts=opts.AxisOpts(name="水量",type_="value",min_=0,max_=250,interval=50,axislabel_opts=opts.LabelOpts(formatter="{value} ml"),# 坐标轴刻度配置项axistick_opts=opts.AxisTickOpts(is_show=True),# x轴的网格线splitline_opts=opts.SplitLineOpts(is_show=True),),) ) line = (Line().add_xaxis(xaxis_data=x_data).add_yaxis(series_name="平均温度",yaxis_index=1,y_axis=[2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2],label_opts=opts.LabelOpts(is_show=False),) ) bar.overlap(line).render_notebook()
2.5 饼图
基本饼图
from pyecharts.charts import Pie data_pie = list(zip(Faker.choose(),Faker.values())) pie_img = (Pie().add("",[list(x) for x in data_pie]).set_colors(["red","yellow","orange","pink","green","blue","black"]).set_global_opts(title_opts=opts.TitleOpts(title="饮料价格"),legend_opts=opts.LegendOpts(type_="scroll",pos_right="10px",orient="vertaical")).set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}")) ) pie_img.render_notebook()
空心饼图
from pyecharts.charts import PictorialBar from pyecharts.globals import SymbolType pie_img = (Pie().add("",[list(z) for z in zip(Faker.choose(), Faker.values())],radius=["40%", "75%"],).set_global_opts(title_opts=opts.TitleOpts(title="空心图s"),legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="2%"),).set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))) pie_img.render_notebook()
玫瑰图
key_pie = Faker.choose() pie_img = (Pie().add("",[list(x) for x in zip(key_pie,Faker.values())],radius=["30%","75%"],# 半径大小center=["25%","50%"], # 位置rosetype="radius").add("",[list(x) for x in zip(key_pie,Faker.values())],radius=["30%","75%"],# 半径大小center=["80%","50%"], # 位置rosetype="radius") ) pie_img.render_notebook()
图例调整
pie_image = (Pie().add("",[list(z)for z in zip(Faker.choose() + Faker.choose() + Faker.choose(),Faker.values() + Faker.values() + Faker.values(),)],center=["40%", "50%"],).set_global_opts(title_opts=opts.TitleOpts(title="Pie-Legend 滚动"),# 滚动类型,垂直展示legend_opts=opts.LegendOpts(type_="scroll", pos_left="80%", orient="vertical"),).set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}")) ) pie_image.render_notebook()
文字调整
pie_image = (Pie().add(series_name="商品信息",data_pair=[list(z) for z in zip(Faker.choose(), Faker.values())],radius=["50%", "70%"],label_opts=opts.LabelOpts(is_show=False, position="center"),).set_global_opts(legend_opts=opts.LegendOpts(pos_left="legft", orient="vertical")).set_series_opts(tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"),# label_opts=opts.LabelOpts(formatter="{b}: {c}")) ) pie_image.render_notebook()
2.6 雷达图
from pyecharts.charts import Radar num1 = [[8000,10000,7500,6800,13000,15000]] num2 = [[7000,8000,9500,8800,11000,12500]] radar_img = (Radar().add_schema(schema=[opts.RadarIndicatorItem(name="demo1",max_=9000),opts.RadarIndicatorItem(name="demo2",max_=11000),opts.RadarIndicatorItem(name="demo3",max_=10000),opts.RadarIndicatorItem(name="demo4",max_=9000),opts.RadarIndicatorItem(name="demo5",max_=14000),opts.RadarIndicatorItem(name="demo6",max_=16000),]).add("数据1",num1,color="green").add("数据2",num2,color="orange") ) radar_img.render_notebook()
单例模式
----一次只能选一个按钮显示数据.
from pyecharts.charts import Radar v1 = [[4300, 10000, 28000, 35000, 50000, 19000]] v2 = [[5000, 14000, 28000, 31000, 42000, 21000]] radar_img = (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.color='blue').add("实际开销", v2).set_series_opts(label_opts=opts.LabelOpts(is_show=False)).set_global_opts(# 单例模式legend_opts=opts.LegendOpts(selected_mode="single"),title_opts=opts.TitleOpts(title="单例模式"),)) radar_img.render_notebook()
2.7 时间轴
from pyecharts import options as opts from pyecharts.charts import Pie, Timeline from pyecharts.faker import Faker attr = Faker.choose() tl = Timeline() for i in range(2015, 2020):pie = (Pie().add("商家A",[list(z) for z in zip(attr, Faker.values())], # rosetype="radius",radius=["30%", "55%"],).set_global_opts(title_opts=opts.TitleOpts("某商店{}年营业额".format(i))))tl.add(pie, "{}年".format(i)) tl.render_notebook() #上面为空心饼图-- from pyecharts import options as opts from pyecharts.charts import Bar, Timeline from pyecharts.faker import Faker x = Faker.choose() tl = Timeline() for i in range(2015, 2020):bar = (Bar().add_xaxis(x).add_yaxis("商家A", Faker.values()).add_yaxis("商家B", Faker.values()).set_global_opts(title_opts=opts.TitleOpts("某商店{}年营业额".format(i))))tl.add(bar, "{}年".format(i)) tl.render_notebook()