pycharts1.6的简单使用
系统版本信息
import pyecharts
print("Echarts version:", pyecharts.__version__)
Echarts version: 1.6.0
Bar图
import numpy as np
import matplotlib.pyplot as plt
from pyecharts.charts import Bar
from pyecharts import options as opts
def mydice(number):
rd = np.random.random(number)*6+1
return rd.astype(int)
#np.random.seed(19680801)
x=mydice(1000000)
print(np.mean(x))
tag=[]
sta=[]
for itr in range(len(set(x))):
tag.append("%d"%(itr+1))
sta.append(len(x[x==(itr+1)]))
bar = Bar()
bar.add_xaxis(tag)
bar.add_yaxis("点数", sta)
bar.set_global_opts(title_opts=opts.TitleOpts(title="统计图", subtitle="骰子"))
bar.render_notebook()

Bar图2
#coding=utf-8
from __future__ import unicode_literals
import matplotlib.pyplot as plt
import numpy as np
from pyecharts.charts import Bar
from pyecharts import options as opts #引入配置项入口
bar = Bar()
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
bar.render_notebook()

Line图
import random
attr = ["{}天".format(i) for i in range(30)]
v1 = [random.randint(1, 30) for _ in range(30)]
#bar = Bar()
line = Line()
line.add_xaxis(attr)
line.add_yaxis("",v1)
line.render_notebook()

overlap图
from pyecharts.charts import Bar, Line
attr = ['A', 'B', 'C', 'D', 'E', 'F']
v1 = [10, 20, 30, 40, 50, 60]
v2 = [38, 28, 58, 48, 78, 68]
bar = Bar()
bar.add_xaxis(attr)
bar.add_yaxis("",v1)
#bar.render_notebook()
line = Line()
line.add_xaxis(attr)
line.add_yaxis("",v2)
#line.render_notebook()
line.overlap(bar)
line.render_notebook()

Boxplot图
from pyecharts.charts import Boxplot
boxplot = Boxplot()
x_axis = ['expr1', 'expr2', 'expr3', 'expr4', 'expr5']
y_axis = [
[850, 740, 900, 1070, 930, 850, 950, 980, 980, 880,
1000, 980, 930, 650, 760, 810, 1000, 1000, 960, 960],
[960, 940, 960, 940, 880, 800, 850, 880, 900, 840,
830, 790, 810, 880, 880, 830, 800, 790, 760, 800],
[880, 880, 880, 860, 720, 720, 620, 860, 970, 950,
880, 910, 850, 870, 840, 840, 850, 840, 840, 840],
[890, 810, 810, 820, 800, 770, 760, 740, 750, 760,
910, 920, 890, 860, 880, 720, 840, 850, 850, 780],
[890, 840, 780, 810, 760, 810, 790, 810, 820, 850,
870, 870, 810, 740, 810, 940, 950, 800, 810, 870]
]
boxplot.add_xaxis(x_axis)
boxplot.add_yaxis("",y_axis)
boxplot.render_notebook()`在这里插入代码片`

Funnel图
from pyecharts.charts import Funnel
attr = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
value = [20, 40, 60, 80, 100, 120]
funnel=(
Funnel()
.set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题"))
.add("商品", [list(z) for z in zip(attr, value)])
)
funnel.render_notebook()

Graph图
from pyecharts.charts import Graph
nodes = [{"name": "结点1", "symbolSize": 10},
{"name": "结点2", "symbolSize": 20},
{"name": "结点3", "symbolSize": 30},
{"name": "结点4", "symbolSize": 40},
{"name": "结点5", "symbolSize": 50},
{"name": "结点6", "symbolSize": 40},
{"name": "结点7", "symbolSize": 30},
{"name": "结点8", "symbolSize": 20}]
links = []
for i in nodes:
for j in nodes:
links.append({"source": i.get('name'), "target": j.get('name')})
graph = Graph()
graph.add("", nodes, links, repulsion=8000)
graph.render_notebook()

Kline图
from pyecharts.charts import Kline
v1 = [[2320.26, 2320.26, 2287.3, 2362.94], [2300, 2291.3, 2288.26, 2308.38],
[2295.35, 2346.5, 2295.35, 2345.92], [2347.22, 2358.98, 2337.35, 2363.8],
[2360.75, 2382.48, 2347.89, 2383.76], [2383.43, 2385.42, 2371.23, 2391.82],
[2377.41, 2419.02, 2369.57, 2421.15], [2425.92, 2428.15, 2417.58, 2440.38],
[2411, 2433.13, 2403.3, 2437.42], [2432.68, 2334.48, 2427.7, 2441.73],
[2430.69, 2418.53, 2394.22, 2433.89], [2416.62, 2432.4, 2414.4, 2443.03],
[2441.91, 2421.56, 2418.43, 2444.8], [2420.26, 2382.91, 2373.53, 2427.07],
[2383.49, 2397.18, 2370.61, 2397.94], [2378.82, 2325.95, 2309.17, 2378.82],
[2322.94, 2314.16, 2308.76, 2330.88], [2320.62, 2325.82, 2315.01, 2338.78],
[2313.74, 2293.34, 2289.89, 2340.71], [2297.77, 2313.22, 2292.03, 2324.63],
[2322.32, 2365.59, 2308.92, 2366.16], [2364.54, 2359.51, 2330.86, 2369.65],
[2332.08, 2273.4, 2259.25, 2333.54], [2274.81, 2326.31, 2270.1, 2328.14],
[2333.61, 2347.18, 2321.6, 2351.44], [2340.44, 2324.29, 2304.27, 2352.02],
[2326.42, 2318.61, 2314.59, 2333.67], [2314.68, 2310.59, 2296.58, 2320.96],
[2309.16, 2286.6, 2264.83, 2333.29], [2282.17, 2263.97, 2253.25, 2286.33],
[2255.77, 2270.28, 2253.31, 2276.22]]
kline=(
Kline()
.add_xaxis(["2017/7/{}".format(i + 1) for i in range(31)])
.add_yaxis("kline", v1)
.set_global_opts(
yaxis_opts=opts.AxisOpts(is_scale=True),
xaxis_opts=opts.AxisOpts(is_scale=True),
title_opts=opts.TitleOpts(title="Kline-基本示例"),
)
)
kline.render_notebook()

Geo图
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType
data = [("海门", 9), ("鄂尔多斯", 12), ("招远", 12), ("舟山", 12), ("齐齐哈尔", 14), ("盐城", 15)]
geo = (
Geo()
.add_schema(maptype="china")
.add("",
[("海门", 9), ("鄂尔多斯", 12), ("招远", 12), ("舟山", 12), ("齐齐哈尔", 14), ("盐城", 15)],
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(),
title_opts=opts.TitleOpts(title="全国主要城市空气质量"),
)
)
geo.render_notebook()

本文介绍PyChats 1.6版本的数据可视化应用,涵盖Bar、Line、Boxplot等多种图表类型,通过实例展示如何创建并渲染统计图、折线图、箱型图等,适用于数据分析师和技术爱好者。
693

被折叠的 条评论
为什么被折叠?



