本期主要利用pyecharts给大家展示 单轴散点图 的画法,希望对小伙伴们有所帮助,如有疑问或者需要改进的地方可以私信小编。
生成随机数据:
# 生成每周日期数据
hours = [str(i)+'h' for i in range(24)]
week_days = ['Sunday','Saturday', 'Friday', 'Thursday', 'Wednesday', 'Tuesday', 'Monday'];
data_days = [i for i in range(len(week_days))]
data_hours = [i for i in range(len(hours))]
# 随机生成每天每小时数据
data_points = [random.randint(0, 10) for j in range(len(week_days)) for i in range(len(hours))]
week_hours = [list(i) for i in itertools.product(data_days, data_hours)]
data_lastest = np.c_[week_hours,data_points].tolist()
以Sunday为例:
# 基本散点图
scatter = Scatter(init_opts=opts.InitOpts(width='900px', height='400px', theme='light'))
scatter.add_xaxis(xaxis_data=hours)
scatter.add_yaxis('',
y_axis=[item[2] for item in data_lastest if item[0] == 0],
symbol_size=JsCode('function(p) { return p[1] * 4;}'),
label_opts=opts.LabelOpts(is_show=False),
)
scatter.set_global_opts(xaxis_opts=opts.AxisOpts(is_show=True),
yaxis_opts=opts.AxisOpts(is_show=True),
title_opts=opts.TitleOpts(title='Sunday'),
)
scatter.render_notebook()
效果:
所有点设置一行(单轴):
# 单轴散点图
single_axis, titles = [], []
scatter = Scatter(init_opts=opts.InitOpts(width='900px', height='400px', theme='light'))
scatter.add_xaxis(xaxis_data=hours)
# 单轴配置
single_axis.append({"left":'15%',
"name": 'Sunday',
"nameGap": 20,
"nameLocation": 'start',
"type": 'category',
"boundaryGap": False,
"data": hours,
"top": '15%',
"height": '10%',
"gridIndex": 0,
"nameTextStyle": {"fontSize": 16},
"axisLabel": { "interval": 2 }
})
scatter.add_yaxis('',
y_axis=[item[2] for item in data_lastest if item[0] == 0],
symbol_size=JsCode('function(p) { return p[1] * 4;}'),
label_opts=opts.LabelOpts(is_show=False),
)
scatter.options['series'][0]['coordinateSystem'] = 'singleAxis'
scatter.options['series'][0]['singleAxisIndex'] = 0
scatter.options['singleAxis'] = single_axis
scatter.set_global_opts(xaxis_opts=opts.AxisOpts(is_show=False),
yaxis_opts=opts.AxisOpts(is_show=False),
)
scatter.render_notebook()
效果:
叠加所有时间数据:
single_axis, titles = [], []
scatter = Scatter(init_opts=opts.InitOpts(width='900px', height='400px', theme='light'))
for idx, day in enumerate(week_days):
pers = 100 / len(week_days)
scatter.add_xaxis(xaxis_data=hours)
# 单轴配置
single_axis.append({"left": '10%',
"nameGap": 20,
"nameLocation": 'start',
"type": 'category',
"boundaryGap": False,
"data": hours,
"top": '{}%'.format(idx * pers + 5),
"height": '{}%'.format(pers - 10),
"gridIndex": idx,
"nameTextStyle": {"fontSize": 16},
"axisLabel": { "interval": 2 }
})
titles.append(dict(text=day,top='{}%'.format(idx * pers + 5), left='2%'))
scatter.add_yaxis('',
y_axis=[item[2] for item in data_lastest if item[0] == idx],
symbol_size=JsCode('function(p) { return p[1] * 4;}'),
label_opts=opts.LabelOpts(is_show=False),
)
scatter.options['series'][idx]['coordinateSystem'] = 'singleAxis'
# 标题
scatter.options['singleAxis'] = single_axis
scatter.set_global_opts(
xaxis_opts=opts.AxisOpts(is_show=False),
yaxis_opts=opts.AxisOpts(is_show=False),
title_opts=titles
)
scatter.render_notebook()
实例地址: 【点我跳转】
以上就是本期为大家整理的全部内容了,赶快练习起来吧,原创不易,喜欢的朋友可以点赞、收藏也可以分享(注明出处)让更多人知道。
文章首发微信公众号 “Python当打之年” ,每天都有python编程技巧推送,希望大家可以喜欢