pyecharts(1)基本圖
阿新 • • 發佈:2020-08-19
from pyecharts.charts import * from pyecharts.components import Table from pyecharts import options as opts from pyecharts.commons.utils import JsCode import random import datetime import math import numpy as np from pyecharts.globals import CurrentConfig CurrentConfig.ONLINE_HOST = "https://cdn.kesci.com/lib/pyecharts_assets/" # 設定host地址
x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data = [123, 153, 89, 107, 98, 23]
'''直方圖'''
bar = (
Bar()
.add_xaxis(x_data)
.add_yaxis('', y_data)
)
bar.render_notebook()
<div id="572c58c83f20468abc55b98fb751efd5" style="width:900px; height:500px;"></div>
'''折線圖'''
line = (
Line()
.add_xaxis(x_data)
.add_yaxis('',y_data)
)
line.render_notebook()
<div id="7bb40231c90e46e5acb6abb1971c7f4e" style="width:900px; height:500px;"></div>
x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu'] y_data = [[random.randint(100, 200) for i in range(10)] for item in x_data] '''箱線圖''' box = ( Boxplot() .add_xaxis(x_data) ) box.add_yaxis('', box.prepare_data(y_data)) box.render_notebook()
<div id="1bf115b52ff74deca8e88f5ece41b69b" style="width:900px; height:500px;"></div>
'''散點圖'''
x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data = [123, 153, 89, 107, 98, 23]
scatter = (Scatter()
.add_xaxis(x_data)
.add_yaxis('', y_data)
)
scatter.render_notebook()
<div id="4f887c7199774a3e9e5c42a36096418e" style="width:900px; height:500px;"></div>
'''漣漪圖'''
effectscatter = (EffectScatter()
.add_xaxis(x_data)
.add_yaxis('', y_data)
)
effectscatter.render_notebook()
<div id="e90afe741b104135820e535ff9cfc6e3" style="width:900px; height:500px;"></div>
'''k線圖'''
date_list = ["2020/4/{}".format(i + 1) for i in range(30)]
y_data = [
[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],
]
kline = (Kline()
.add_xaxis(date_list)
.add_yaxis('', y_data)
)
kline.render_notebook()
<div id="a19fe02a4d2f44879dd3918998e7b84d" style="width:900px; height:500px;"></div>
data = [[i, j, random.randint(0, 100)] for i in range(24) for j in range(7)]
hour_list = [str(i) for i in range(24)]
week_list = ['週日', '週一', '週二', '週三', '週四', '週五', '週六']
'''熱力圖'''
heat = (HeatMap()
.add_xaxis(hour_list)
.add_yaxis('', week_list, data)
)
heat.render_notebook()
<div id="ce5daa5e83664d12a19455af95ff2beb" style="width:900px; height:500px;"></div>
x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data = [123, 153, 89, 107, 98, 23]
'''象形圖'''
pictorialbar = (PictorialBar()
.add_xaxis(x_data)
.add_yaxis('', y_data)
)
pictorialbar.render_notebook()
<div id="0c600183f0f5437f8c50c71b6d216b7f" style="width:900px; height:500px;"></div>
x_data = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
y_data_bar = [123, 153, 89, 107, 98, 23]
y_data_line = [153, 107, 23, 89, 123, 107]
bar = (Bar()
.add_xaxis(x_data)
.add_yaxis('', y_data_bar)
)
line = (Line()
.add_xaxis(x_data)
.add_yaxis('', y_data_line)
)
'''疊加圖'''
overlap = bar.overlap(line)
# overlap = line.overlap(bar)
overlap.render_notebook()
<div id="0dd4340dbde642aa9aa9cadd9e1cadc1" style="width:900px; height:500px;"></div>
province = [
'廣東',
'湖北',
'湖南',
'四川',
'重慶',
'黑龍江',
'浙江',
'山西',
'河北',
'安徽',
'河南',
'山東',
'西藏']
data = [(i, random.randint(50, 150)) for i in province]
'''GEO-地理座標'''
geo = (Geo()
.add_schema(maptype='china')
.add('', data)
)
geo.render_notebook()
<div id="2974bbff367449d79fb58c856b0bd217" style="width:900px; height:500px;"></div>
province = [
'廣東',
'湖北',
'湖南',
'四川',
'重慶',
'黑龍江',
'浙江',
'山西',
'河北',
'安徽',
'河南',
'山東',
'西藏']
data = [(i, random.randint(50, 150)) for i in province]
'''map地圖'''
map_ = (
Map()
.add("", data, 'china')
)
map_.render_notebook()
<div id="0ee8059f2b7a40349020e9e1c9b63872" style="width:900px; height:500px;"></div>
province = [
'廣東',
'湖北',
'湖南',
'四川',
'重慶',
'黑龍江',
'浙江',
'山西',
'河北',
'安徽',
'河南',
'山東',
'西藏']
data = [(i, random.randint(50, 150)) for i in province]
'''百度地圖'''
bmap = (
BMap()
.add_schema(baidu_ak="FAKE_AK", center=[120.13066322374, 30.240018034923])
.add("", data)
)
bmap.render_notebook()
<div id="3aefc7b86c2a4eec93326301a6b14dff" style="width:900px; height:500px;"></div>
cate = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
data = [123, 153, 89, 107, 98, 23]
'''餅圖'''
pie = (Pie()
.add('', [list(z) for z in zip(cate, data)])
)
pie.render_notebook()
<div id="4c53d9f5fbb34147b943bd9e49fa1b89" style="width:900px; height:500px;"></div>
cate = ['訪問', '註冊', '加入購物車', '提交訂單', '付款成功']
data = [30398, 15230, 10045, 3109, 1698]
'''漏斗圖'''
funnel = (Funnel()
.add('', [list(z) for z in zip(cate, data)])
)
funnel.render_notebook()
<div id="19c959f8d8a04fa88edde712c4d1571f" style="width:900px; height:500px;"></div>
'''儀表圖'''
gauge = (Gauge()
.add('', [('轉化率', 74)])
)
gauge.render_notebook()
<div id="6163cf20f90d46f4b99d4407994c3cfb" style="width:900px; height:500px;"></div>
'''水球圖'''
liqiud = (Liquid()
.add('', [0.52, 0.44, 0.04, 0.02])
)
liqiud.render_notebook()
<div id="15d5bcd67efc46f28ad68210ae31eaa8" style="width:900px; height:500px;"></div>
begin = datetime.date(2019, 1, 1)
end = datetime.date(2019, 12, 31)
data = [[str(begin + datetime.timedelta(days=i)), abs(math.cos(i/100))* random.randint(100, 120)]
for i in range((end - begin).days + 1)]
'''日曆圖'''
calendar = (Calendar()
.add('', data, calendar_opts=opts.CalendarOpts(range_='2019'))
)
calendar.render_notebook()
<div id="11304ff70bc84702b588ce746bd7c55b" style="width:900px; height:500px;"></div>
nodes = [
{"name": "結點1", "symbolSize": 1},
{"name": "結點2", "symbolSize": 2},
{"name": "結點3", "symbolSize": 3},
{"name": "結點4", "symbolSize": 4},
{"name": "結點5", "symbolSize": 5},
{"name": "結點6", "symbolSize": 6},
{"name": "結點7", "symbolSize": 7},
{"name": "結點8", "symbolSize": 8},
]
links = [{'source': '結點1', 'target': '結點2'},
{'source': '結點1', 'target': '結點3'},
{'source': '結點1', 'target': '結點4'},
{'source': '結點2', 'target': '結點1'},
{'source': '結點3', 'target': '結點4'},
{'source': '結點3', 'target': '結點5'},
{'source': '結點3', 'target': '結點6'},
{'source': '結點4', 'target': '結點1'},
{'source': '結點4', 'target': '結點2'},
{'source': '結點4', 'target': '結點7'},
{'source': '結點4', 'target': '結點8'},
{'source': '結點5', 'target': '結點1'},
{'source': '結點5', 'target': '結點4'},
{'source': '結點5', 'target': '結點6'},
{'source': '結點5', 'target': '結點7'},
{'source': '結點5', 'target': '結點8'},
{'source': '結點6', 'target': '結點1'},
{'source': '結點6', 'target': '結點7'},
{'source': '結點6', 'target': '結點8'},
{'source': '結點7', 'target': '結點1'},
{'source': '結點7', 'target': '結點2'},
{'source': '結點7', 'target': '結點8'},
{'source': '結點8', 'target': '結點1'},
{'source': '結點8', 'target': '結點2'},
{'source': '結點8', 'target': '結點3'},
]
'''關係圖'''
graph = (Graph()
.add('', nodes, links)
)
graph.render_notebook()
<div id="0baf17d541134c33b31ccaef51f1e865" style="width:900px; height:500px;"></div>
data = [
['一班', 78, 91, 123, 78, 82, 67, "優秀"],
['二班', 89, 101, 127, 88, 86, 75, "良好"],
['三班', 86, 93, 101, 84, 90, 73, "合格"],
]
'''平行座標系'''
parallel = (Parallel()
.add_schema([
opts.ParallelAxisOpts(
dim=0,
name='班級',
type_='category',
data=["一班", "二班", "三班"],
),
opts.ParallelAxisOpts(dim=1, name='英語'),
opts.ParallelAxisOpts(dim=2, name="數學"),
opts.ParallelAxisOpts(dim=3, name="語文"),
opts.ParallelAxisOpts(dim=4, name="物理"),
opts.ParallelAxisOpts(dim=5, name="生物"),
opts.ParallelAxisOpts(dim=6, name="化學"),
opts.ParallelAxisOpts(
dim=7,
name="評級",
type_="category",
data=["優秀", "良好", "合格"],
),
])
.add('', data)
)
parallel.render_notebook()
<div id="6073cf7958bb411bad3f7544deeaa1fb" style="width:900px; height:500px;"></div>
cate = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
data = [123, 153, 89, 107, 98, 23]
'''極座標'''
polar=(
Polar()
.add_schema(
radiusaxis_opts=opts.RadiusAxisOpts(data=cate)
)
.add('', data, type_='bar')
)
polar.render_notebook()
<div id="9640b9278bc8433c982dafe2a3617444" style="width:900px; height:500px;"></div>
data = [
[78, 91, 123, 78, 82, 67],
[89, 101, 127, 88, 86, 75],
[86, 93, 101, 84, 90, 73],
]
'''雷達圖'''
radar = (
Radar()
.add_schema(
schema=[
opts.RadarIndicatorItem(name='語文', max_=150),
opts.RadarIndicatorItem(name="數學", max_=150),
opts.RadarIndicatorItem(name="英語", max_=150),
opts.RadarIndicatorItem(name="物理", max_=100),
opts.RadarIndicatorItem(name="生物", max_=100),
opts.RadarIndicatorItem(name="化學", max_=100),
]
)
.add('', data)
)
radar.render_notebook()
<div id="cd9bd1b26e1f4150b360d425e3e5145c" style="width:900px; height:500px;"></div>
data = [
{"name": "湖南",
"children": [
{"name": "長沙",
"children": [
{"name": "雨花區", "value": 55},
{"name": "嶽麓區", "value": 34},
{"name": "天心區", "value": 144},
]},
{"name": "常德",
"children": [
{"name": "武陵區", "value": 156},
{"name": "鼎城區", "value": 134},
]},
{"name": "湘潭", "value": 87},
{"name": "株洲", "value": 23},
],
},
{"name": "湖北",
"children": [
{"name": "武漢",
"children": [
{"name": "洪山區", "value": 55},
{"name": "東湖高新", "value": 78},
{"name": "江夏區", "value": 34},
]},
{"name": "鄂州", "value": 67},
{"name": "襄陽", "value": 34},
],
},
{"name": "北京", "value": 235}
]
'''旭日圖'''
sunburst = (
Sunburst()
.add('', data_pair=data)
)
sunburst.render_notebook()
<div id="6bd5233240f349b0acc51c745fe50ec9" style="width:900px; height:500px;"></div>
nodes = [
{"name": "訪問"},
{"name": "註冊"},
{"name": "付費"},
{"name": "離開"},
]
links = [
{"source": "訪問", "target": "註冊", "value": 50},
{"source": "註冊", "target": "付費", "value": 10},
{"source": "註冊", "target": "離開", "value": 20},
]
'''桑基圖'''
sankey=(
Sankey()
.add('', nodes, links)
)
sankey.render_notebook()
<div id="30a7bf22dcf74c18aa47f5b41fb30748" style="width:900px; height:500px;"></div>
cate = ['Apple', 'Huawei', 'Xiaomi', 'Oppo', 'Vivo', 'Meizu']
date_list = ["2020/4/{}".format(i + 1) for i in range(30)]
data = [[day, random.randint(10, 50), c] for day in date_list for c in cate]
'''河流圖'''
river = (
ThemeRiver()
.add(
series_name=cate,
data=data,
singleaxis_opts=opts.SingleAxisOpts(type_='time')
)
)
river.render_notebook()
<div id="cbcac8316c0642f09f422b39af08a8d3" style="width:900px; height:500px;"></div>
words = [
('Hichens', 600),
("hey", 230),
("jude", 124),
("dont", 436),
("make", 255),
("it", 247),
("bad", 244),
("Take", 138),
("a sad song", 184),
("and", 12),
("make", 165),
("it", 247),
("better", 182),
("remember", 255),
("to", 150),
("let", 162),
("her", 266),
("into", 60),
("your", 82),
("heart", 173),
("then", 365),
("you", 360),
("can", 282),
("start", 273),
("make", 265),
('LJ', 600),
]
'''詞雲圖'''
wc = (
WordCloud()
.add('', words)
)
wc.render_notebook()
<div id="dbfacc00b888413ea80a1364f1e1cc2b" style="width:900px; height:500px;"></div>
headers = ["City name", "Area", "Population", "Annual Rainfall"]
rows = [
["Brisbane", 5905, 1857594, 1146.4],
["Adelaide", 1295, 1158259, 600.5],
["Darwin", 112, 120900, 1714.7],
["Hobart", 1357, 205556, 619.5],
["Sydney", 2058, 4336374, 1214.8],
["Melbourne", 1566, 3806092, 646.9],
["Perth", 5386, 1554769, 869.4],
]
'''表格'''
from pyecharts.components import Table
table = (
Table()
.add(headers, rows)
)
table.render_notebook()
<style>
.fl-table {
margin: 20px;
border-radius: 5px;
font-size: 12px;
border: none;
border-collapse: collapse;
max-width: 100%;
white-space: nowrap;
word-break: keep-all;
}
.fl-table th {
text-align: left;
font-size: 20px;
}
.fl-table tr {
display: table-row;
vertical-align: inherit;
border-color: inherit;
}
.fl-table tr:hover td {
background: #00d1b2;
color: #F8F8F8;
}
.fl-table td, .fl-table th {
border-style: none;
border-top: 1px solid #dbdbdb;
border-left: 1px solid #dbdbdb;
border-bottom: 3px solid #dbdbdb;
border-right: 1px solid #dbdbdb;
padding: .5em .55em;
font-size: 15px;
}
.fl-table td {
border-style: none;
font-size: 15px;
vertical-align: center;
border-bottom: 1px solid #dbdbdb;
border-left: 1px solid #dbdbdb;
border-right: 1px solid #dbdbdb;
height: 30px;
}
.fl-table tr:nth-child(even) {
background: #F8F8F8;
}
</style>
<div id="9b38942aea794f6cbc12c921f1ef2990" class="chart-container" style="">
<p class="title" style="font-size: 18px; font-weight:bold;" > </p>
<p class="subtitle" style="font-size: 12px;" > </p>
<table class="fl-table">
<tr>
<th>City name</th>
<th>Area</th>
<th>Population</th>
<th>Annual Rainfall</th>
</tr>
<tr>
<td>Brisbane</td>
<td>5905</td>
<td>1857594</td>
<td>1146.4</td>
</tr>
<tr>
<td>Adelaide</td>
<td>1295</td>
<td>1158259</td>
<td>600.5</td>
</tr>
<tr>
<td>Darwin</td>
<td>112</td>
<td>120900</td>
<td>1714.7</td>
</tr>
<tr>
<td>Hobart</td>
<td>1357</td>
<td>205556</td>
<td>619.5</td>
</tr>
<tr>
<td>Sydney</td>
<td>2058</td>
<td>4336374</td>
<td>1214.8</td>
</tr>
<tr>
<td>Melbourne</td>
<td>1566</td>
<td>3806092</td>
<td>646.9</td>
</tr>
<tr>
<td>Perth</td>
<td>5386</td>
<td>1554769</td>
<td>869.4</td>
</tr>
data = [(random.randint(0, 100), random.randint(0, 100), random.randint(0, 100)) for _ in range(100)]
'''3D散點圖'''
scatter3D = (
Scatter3D()
.add('', data)
)
scatter3D.render_notebook()
<div id="90cb6dee7ff44c2b820216a3c352156e" style="width:900px; height:500px;"></div>
data = []
for t in range(0, 1000):
x = math.cos(t/10)
y = math.sin(t/10)
z = t/10
data.append([x, y, z])
'''3D折線圖'''
line3D = (
Line3D()
.add('', data,
xaxis3d_opts=opts.Axis3DOpts(type_='value'),
yaxis3d_opts=opts.Axis3DOpts(type_='value')
)
)
line3D.render_notebook()
<div id="4be9bcb1459f4deebc1fc6b73d079d54" style="width:900px; height:500px;"></div>
data = [[i, j, random.randint(0, 100)] for i in range(24) for j in range(7)]
hour_list = [str(i) for i in range(24)]
week_list = ['週日', '週一', '週二', '週三', '週四', '週五', '週六']
'''3D直方圖'''
bar3D = (
Bar3D()
.add(
'',
data,
xaxis3d_opts=opts.Axis3DOpts(hour_list, type_='category'),
yaxis3d_opts=opts.Axis3DOpts(week_list, type_='category'),
zaxis3d_opts=opts.Axis3DOpts(type_='value'),
)
)
bar3D.render_notebook()
<div id="091f7df2be98457fa6644903267c6536" style="width:900px; height:500px;"></div>
province = [
'廣東',
'湖北',
'湖南',
'四川',
'重慶',
'黑龍江',
'浙江',
'山西',
'河北',
'安徽',
'河南',
'山東',
'西藏']
data = [(i, random.randint(50, 150)) for i in province]
'''3D地圖'''
map3D = (
Map3D()
.add('', data_pair=data, maptype='china')
)
map3D.render_notebook()
<div id="3a41a824d4c74174bfe925efa1e666fb" style="width:900px; height:500px;"></div>
'''3D地球'''
from pyecharts.faker import POPULATION
mapglobe = (
MapGlobe()
.add_schema()
.add(
series_name='',
maptype='world',
data_pair=POPULATION[1:]
)
)
mapglobe.render_notebook()
<div id="c8756cc33578425eb5657954a502ff15" style="width:900px; height:500px;"></div>