Pyecharts
画图的(doge)
文章目录
一、 柱状图
使用Bar函数可以绘制柱状图。
单列柱状图
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = Bar()
bar.add_xaxis(['1号选手', '2号选手', '3号选手', '4号选手', '5号选手'])
bar.add_yaxis("第一轮", [87, 91, 86, 85, 93])
bar.set_global_opts(title_opts=opts.TitleOpts(title="各选手得分情况"))
bar.render_notebook()
运行结果为:
多列柱状图
在原代码基础上再加一行add_yaxis即可。
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = Bar()
bar.add_xaxis(['1号选手', '2号选手', '3号选手', '4号选手', '5号选手'])
bar.add_yaxis("第一轮", [87, 91, 86, 85, 93])
bar.add_yaxis("第二轮", [90, 90, 87, 86, 89])
bar.set_global_opts(title_opts=opts.TitleOpts(title="各选手得分情况"))
bar.render_notebook()
运行结果为:
水平柱状图
使用**bar.reversal_axis()**可以绘制水平柱状图。
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = Bar()
bar.add_xaxis(['1号选手', '2号选手', '3号选手', '4号选手', '5号选手'])
bar.add_yaxis("第一轮", [87, 91, 86, 85, 93])
bar.add_yaxis("第二轮", [90, 90, 87, 86, 89])
bar.set_global_opts(title_opts=opts.TitleOpts(title="各选手得分情况"))
bar.reversal_axis()
bar.render_notebook()
运行结果为:
二、 饼状图
饼状图通常用来表现不同类别的占比情况。
用Pie可以绘制饼状图。
一般饼状图
from pyecharts.charts import Pie
from pyecharts import options as opts
list1 = ['优秀', '良好', '及格', '不及格']
num = [11, 35, 7, 2]
c = Pie()
#这一步,是要将list1里面的元素和num里面的元素用zip函数对应打包成一个元组,然后形成一个列表。
c.add("", [list(z) for z in zip(list1, num)])
c.set_global_opts(title_opts=opts.TitleOpts(title='Pie-2班成绩占比'))
#这一步没怎么懂,应该是设置标签的形式
c.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
c.render_notebook()
运行结果为:
关于上面代码中的:c.set_series_opts(label_opts=opts.LabelOpts(formatter=“{b}: {c}”))
我试了一下把{b}去掉,运行后饼状图的标签就只剩数字了;相反,去掉{c}运行之后就只剩文字了;全部去掉运行也是文字。
玫瑰图
add()中:
参数radius用于设置饼状图半径,默认为[0, 75],前面为内半径,后面为外半径;
参数center用于设置饼状图中心坐标,默认为[50, 50];
参数rosetype用于设置玫瑰图,有两种表现形式,分别为radius和area。
from pyecharts.charts import Pie
from pyecharts import options as opts
list1 = ['优秀', '良好', '及格', '不及格']
num = [11, 35, 7, 2]
c = Pie()
c.add("", [list(z) for z in zip(list1, num)], radius=["40%", "55%"], center=[240, 210], rosetype='radius')
c.add("", [list(z) for z in zip(list1, num)], radius=["40%", "55%"], center=[640, 210], rosetype='area')
c.set_global_opts(title_opts=opts.TitleOpts(title='2班成绩占比-玫瑰图'))
c.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
c.render_notebook()
运行结果为:(左图为radius形式,右图为area形式)
三、折线图
通过Line绘制折线图。
from pyecharts.charts import Line
from pyecharts import options as opts
l = Line()
l.add_xaxis(['1号选手', '2号选手', '3号选手', '4号选手', '5号选手'])
l.add_yaxis("第一轮", [87, 91, 86, 85, 93])
l.set_global_opts(title_opts=opts.TitleOpts(title="各选手得分情况"))
l.render_notebook()
运行结果为:
四、 漏斗图
通过Funnel绘制漏斗图。
from pyecharts.charts import Funnel
from pyecharts import options as opts
labels = ['跑步', '足球', '篮球', '羽毛球', '乒乓球']
data = [65, 20, 53, 45, 49]
f = Funnel()
f.add('', [list(z) for z in zip(labels, data)], is_selected=True)
f.set_global_opts(title_opts=opts.TitleOpts(title="运动类型喜好"))
f.render_notebook()
运行结果为:
五、散点图
通过Scatter绘制散点图。
一般散点图
from pyecharts.charts import Scatter
from pyecharts import options as opts
labels = ['跑步', '足球', '篮球', '羽毛球', '乒乓球']
data = [65, 20, 53, 45, 49]
s = Scatter()
s.add_xaxis(labels)
s.add_yaxis('大一', data)
s.set_global_opts(title_opts=opts.TitleOpts(title="喜好不同运动类型的人数"))
s.render_notebook()
运行结果为:
涟漪散点图
from pyecharts.charts import EffectScatter
from pyecharts import options as opts
labels = ['跑步', '足球', '篮球', '羽毛球', '乒乓球']
data = [65, 20, 53, 45, 49]
es = EffectScatter()
es.add_xaxis(labels)
es.add_yaxis('大一', data)
es.set_global_opts(title_opts=opts.TitleOpts(title="喜好不同运动类型的人数"))
es.render_notebook()
运行结果为:(动态的,但这里看不出来)
六、K线图
通过Kline绘制K线图。
from pyecharts.charts import Kline
from pyecharts import options as opts
labels = ["2022年10月{}日".format(i) for i in range(1, 32)]
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],
]
k = Kline()
k.add_xaxis(labels)
k.add_yaxis("2022年10月份K线图", data)
k.set_global_opts(title_opts=opts.TitleOpts(title="数据抄的,不知道是啥"))
k.render_notebook()
运行结果为:
七、仪表盘
通过Gauge绘制仪表盘。
from pyecharts.charts import Gauge
from pyecharts import options as opts
g = Gauge()
g.add(" ", [("tempo", 70)], detail_label_opts=opts.GaugeDetailOpts(offset_center=[0, 90]))
g.set_global_opts(title_opts=opts.TitleOpts(title="任务完成率"), legend_opts=opts.LegendOpts(is_show=True))
g.render_notebook()
其中:detail_label_opts=opts.GaugeDetailOpts(offset_center=[0, 90]))是设置偏移量,这么做的话文字和数字就不会重合在一起,But!不知道为什么,将数字移走之后,就没有百分号了。
运行结果为:
八、词云图
通过WordCloud绘制词云图。
from pyecharts.charts import WordCloud
from pyecharts import options as opts
words = [
("where", 500),
("there", 400),
("is", 487),
("a", 355),
("will", 247),
("there", 666),
("is", 578),
("a", 184),
("way", 120),
]
w = WordCloud()
w.add("", words)
w.set_global_opts(title_opts=opts.TitleOpts(title="一句名言"))
w.render_notebook()
以元组的形式生成词云图,后面的数字表示相对大小?
运行结果为:
九、 Page-顺序多图
通过Page可以把多张图按顺序展现。
from pyecharts.charts import Line, Bar, Page
from pyecharts import options as opts
l = Line()
l.add_xaxis(['1号选手', '2号选手', '3号选手', '4号选手', '5号选手'])
l.add_yaxis("第一轮", [87, 91, 86, 85, 93])
l.set_global_opts(title_opts=opts.TitleOpts(title="各选手得分情况"))
bar = Bar()
bar.add_xaxis(['1号选手', '2号选手', '3号选手', '4号选手', '5号选手'])
bar.add_yaxis("第一轮", [87, 91, 86, 85, 93])
bar.set_global_opts(title_opts=opts.TitleOpts(title="各选手得分情况"))
p = Page()
p.add(l,bar)
p.render_notebook()
运行结果为:
十、Grid-并行多图
通过Grid来实现多图并行。
from pyecharts.charts import Line, Bar, Grid
from pyecharts import options as opts
l = Line()
l.add_xaxis(['1号', '2号', '3号', '4号', '5号'])
l.add_yaxis("十月", [30, 25, 18, 19, 20])
l.set_global_opts(title_opts=opts.TitleOpts(title="气温"))
bar = Bar()
bar.add_xaxis(['1号', '2号', '3号', '4号', '5号'])
bar.add_yaxis("十月", [30, 25, 18, 19, 20])
g = Grid()
g.add(bar, grid_opts=opts.GridOpts(pos_left="70%", pos_bottom="70%"))
g.add(l, grid_opts=opts.GridOpts(pos_right="10%"))
g.render_notebook()
其中:
pos_left="50%"是指在左侧留出50%的空间,其他同理。
运行结果为: