# 折线图
import random
import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.commons.utils import JsCode
line = (
Line()
.add_xaxis(['{}月第{}周周赛'.format(y,z)
for y in range(1, 3) # 1、2月
for z in range(1, 5)]) # 1-4周
.add_yaxis('A题', [random.randint(10, 20) for _ in range(8)],
is_smooth=True, # 平滑
markpoint_opts=opts.MarkPointOpts(
# 使用coord这个属性设置自定义标记点数值,我这儿随便写
data=[opts.MarkPointItem(name='自定义标记点',coord=[2,18],value='标注值')]
)
)
.add_yaxis('B题', [random.randint(5, 20) for _ in range(8)])
.add_yaxis('C题', [random.randint(5, 20) for _ in range(8)])
.set_series_opts(label_opts=opts.LabelOpts(
formatter=JsCode( # 通过定义JavaScript回调函数自定义标签
"function(params){"
"return params.value[1].toString() + '%';}" # 外层单引号内存双引号亲测不行!
)
))
.set_global_opts(xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-30)), # 设置x轴标签旋转角度
yaxis_opts=opts.AxisOpts(name='AC率', min_=3),
title_opts=opts.TitleOpts(title='折线示例_ACM题目分析'))
)
line.render('折线图.html')
# 折线面积图
import random
import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.commons.utils import JsCode
line = (
Line()
.add_xaxis(['{}月第{}周周赛'.format(y,z)
for y in range(1, 3) # 1、2月
for z in range(1, 5)]) # 1-4周
.add_yaxis('蔡队',
[random.randint(10, 20) for _ in range(8)],
is_symbol_show=False,
areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_='average', name='均值'),
opts.MarkPointItem(type_='max', name='最大值'),
opts.MarkPointItem(type_='min', name='最小值')],
symbol_size=50)
)
.add_yaxis('旺神',
[random.randint(6, 20) for _ in range(8)],
is_smooth=True,
is_symbol_show=False,
areastyle_opts=opts.AreaStyleOpts(opacity=0.5)
)
.set_global_opts(xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-30)), # 设置x轴标签旋转角度
yaxis_opts=opts.AxisOpts(name='完成积分', min_=5),
title_opts=opts.TitleOpts(title='折线面积图示例_周赛分析'))
)
line.render('折线面积图.html')
# 散点图
from pyecharts.charts import Scatter
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode
import pandas as pd
def scatter_simple() -> Scatter:
# 数据源
df = pd.DataFrame({'AC':[21,22,23,24,28,30,34,35,40,44,45], # 刷题数
'ACB':[140,120,380,120,200,190,160,300,300,400,500],
'姓名':['小军','NIL','假冒NOI','小白','弱刚','晓雷','窜天','云云','依图','蔡队','旺神',]})
# inplace=True:不创建新的对象,直接对原始对象进行修改
# 升序
df.sort_values('AC', inplace=True, ascending=True)
c = (
Scatter()
.add_xaxis(df.AC.values.tolist())
.add_yaxis(
'刷题_能力_姓名',
df[['ACB','姓名']].values.tolist(),
label_opts=opts.LabelOpts(
formatter=JsCode(
'function(params){return params.value[2];}' #通过定义JavaScript回调函数自定义标签
)
)
)
.set_global_opts(
title_opts=opts.TitleOpts(title='散点图示例--ACM集训队队员能力'),
xaxis_opts=opts.AxisOpts(name='AC(刷题数)', type_='value', min_=20), #x轴从20开始,原点不为0
yaxis_opts=opts.AxisOpts(name='ACB(能力值)', min_=100), # y轴起始点的值
legend_opts=opts.LegendOpts(is_show=True)
)
)
return c
scatter_simple().render('散点图.html')
# 雷达图
import random
from pyecharts import options as opts
from pyecharts.charts import Page, Radar
def radar_simple() -> Radar:
c = (
Radar()
.add_schema(
# 各项的max_值可以不同
schema=[
opts.RadarIndicatorItem(name='计算几何学', max_=100),
opts.RadarIndicatorItem(name='动态规划', max_=100),
opts.RadarIndicatorItem(name='图论', max_=100),
opts.RadarIndicatorItem(name='搜索', max_=100),
opts.RadarIndicatorItem(name='模拟', max_=100),
opts.RadarIndicatorItem(name='数论', max_=100),
]
)
.add('旺神', [[random.randint(10, 101) for _ in range(6)]],
color='red',
areastyle_opts = opts.AreaStyleOpts( #设置填充的属性
opacity = 0.5,
color='red'
),)
.add('蔡队', [[random.randint(10, 101) for _ in range(6)]],
color='blue',
areastyle_opts = opts.AreaStyleOpts(
opacity = 0.5,#透明度
color='blue'
),)
.set_series_opts(label_opts=opts.LabelOpts(is_show=True))
.set_global_opts(title_opts=opts.TitleOpts(title='雷达图示例-ACM集训队队员能力'))
)
return c
radar_simple().render('雷达图.html')
# 箱线图--描述离散程度
from pyecharts import options as opts
from pyecharts.charts import Boxplot
def boxpolt_base() -> Boxplot:
v_sophomore = [
[1.1, 2.2, 2.6, 3.2, 3.7, 4.2, 4.7, 4.7, 5.5, 6.3, 8.0],
[2.5, 2.5, 2.8, 3.2, 3.7, 4.2, 4.7, 4.7, 5.5, 6.3, 7.0]
]
v_junior = [
[3.6, 3.7, 4.7, 4.9, 5.1, 5.2, 5.3, 5.4, 5.7, 5.8, 5.8],
[3.6, 3.7, 4.7, 4.9, 5.1, 5.2, 5.3, 5.4, 5.7, 5.8, 5.8]
]
# 最小值,下四分位数,中位数、上四分位数、最大值
# [min, Q1, median (or Q2), Q3, max]
c = (
Boxplot()
.add_xaxis(['寒假作业','暑假作业'])
.add_yaxis('大二队员', Boxplot.prepare_data(v_sophomore))
.add_yaxis('大三队员', Boxplot.prepare_data(v_junior))
.set_series_opts(label_opts=opts.LabelOpts(is_show=True))
.set_global_opts(title_opts=opts.TitleOpts(title='ACM集训队祖传练习完成时长离散度'),
xaxis_opts=opts.AxisOpts(name='单位:小时'),
legend_opts=opts.LegendOpts(is_show=True))
.reversal_axis() #翻转XY轴
)
return c
boxpolt_base().render('箱线图.html')
# 词云图
from pyecharts import options as opts
from pyecharts.charts import WordCloud
from pyecharts.globals import SymbolType
words = [
('背包问题', 10000),
('大整数', 6181),
('Karatsuba乘法算法', 4386),
('穷举搜索', 4055),
('傅里叶变换', 2467),
('状态树遍历', 2244),
('剪枝', 1868),
('Gale-shapley', 1484),
('最大匹配与匈牙利算法', 1112),
('线索模型', 865),
('关键路径算法', 847),
('最小二乘法曲线拟合', 582),
('二分逼近法', 555),
('牛顿迭代法', 550),
('Bresenham算法', 462),
('粒子群优化', 366),
('Dijkstra', 360),
('A*算法', 282),
('负极大极搜索算法', 273),
('估值函数', 265)
]
def wordcloud_base() -> WordCloud:
c = (
WordCloud()
.add("", words, word_size_range=[20, 100], shape=SymbolType.ROUND_RECT)
.set_global_opts(title_opts=opts.TitleOpts(title='WordCloud示例-OJ搜索关键字'))
)
return c
wordcloud_base().render('词云图.html')
上一篇:用Python pyecharts v1.x 绘制图形(一):柱状图、柱状堆叠图、条形图、直方图、帕累托图、饼图、圆环图、玫瑰图