大家好,我是欧K~。
本期主要利用pyecharts给大家展示单轴散点图的画法,希望对你有所帮助。
1. 示例数据
生成随机数据:
# 生成每周日期数据 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()
示例数据(2*3):
2. 基础散点图
以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()
效果:
3. 单轴散点图
所有点设置一行(单轴):
# 单轴散点图 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()
效果:
4. 单轴叠加
叠加所有时间数据:
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['series'][idx]['singleAxisIndex'] = idx # 标题 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()
效果:
所有参数均可以根据实际需要调整。
END
以上就是本期为大家整理的全部内容了,赶快练习起来吧,喜欢的朋友可以点赞、点在看也可以分享让更多人知道