可视化 | Pyecharts 单轴散点图(附完整代码)


大家好,我是 👉 【Python当打之年】

本期主要利用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['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()

效果:
在这里插入图片描述

5. 实例

实例地址: 【点我跳转】


以上就是本期为大家整理的全部内容了,赶快练习起来吧,原创不易,喜欢的朋友可以点赞、收藏也可以分享注明出处)让更多人知道。

推荐阅读


文章首发微信公众号 “Python当打之年” ,每天都有python编程技巧推送,希望大家可以喜欢
在这里插入图片描述

  • 3
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Python当打之年

您的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值