当前位置:网站首页>将列表分箱,并通过Pyechart绘制柱状图
将列表分箱,并通过Pyechart绘制柱状图
2022-06-22 00:46:00 【呆萌的代Ma】
用matplotlib可以一步到位,但是用pyechart时:
- 需要手动分箱并绘图,分箱的方法使用的是:
pd.cut(array, bins) - 统计x对应的下标与y轴对应的频数,将数据统计好后,给
def plot_bar()进行绘制
示例代码如下:
示例代码
import numpy as np
import pandas as pd
def plot_bar(bar_index, value_list):
"""绘制柱状图 :param bar_index: x轴的下标 :param value_list: y轴的值 """
from pyecharts import options as opts
from pyecharts.charts import Bar
bar = Bar()
bar.add_xaxis(bar_index)
bar.add_yaxis("", value_list)
bar.set_global_opts(
title_opts=opts.TitleOpts(title="柱状图"),
yaxis_opts=opts.AxisOpts(name="X轴"),
xaxis_opts=opts.AxisOpts(name="Y轴"),
datazoom_opts=opts.DataZoomOpts(type_="slider", is_realtime=True, range_start=0, range_end=100),
)
bar.set_series_opts(
label_opts=opts.LabelOpts(is_show=False),
markline_opts=opts.MarkLineOpts(
data=[
opts.MarkLineItem(type_="max", name="最大值"),
opts.MarkLineItem(type_="average", name="平均值"),
]
),
).render("bar_chart_display_delay.html")
def plot_result(array, bins=20):
"""准备转换数据 :param array: 待绘制的数组 :param bins: 柱状图柱子的数量 """
# 开始准备数据,准备绘图
num_list = [] # 记录每个bins的数量
index_list = [] # 记录下标
value_dis_bins = pd.cut(array, bins=bins) # 数据分箱
for i in range(bins):
bin_code = np.argwhere(value_dis_bins.codes == i).flatten()
num_list.append(len(bin_code))
index_list.append(((value_dis_bins.categories[i].left + value_dis_bins.categories[i].right) / 2).round(5))
plot_bar(index_list, num_list)
plot_result(array=np.random.random(size=200))
效果如下所示:
边栏推荐
猜你喜欢

Cancellation meeting and automatic refund processing of message queue

The appearance, space, safety and power are all upgraded. The xinjietu x70s will be put on the market from 87900 yuan

SparkRDD 案例:计算总成绩

ShardingSphere-proxy-5.0.0分布式哈希取模分片实现(四)

03 FastJson 解决循环引用

Counter完之后,想统计字符串长度大于2的结果

Spark RDD case: word frequency statistics

Planification dynamique - 01 sac à dos, partitions et sous - ensembles, poids de la dernière pierre

Processing of the scenario of more or less delivery by suppliers in SAP mm import purchase business

3分钟,带你玩转聊天机器人自动化【顶级模板】
随机推荐
Integer和int区别
redis实现分布式锁
Spark RDD案例:词频统计
[redis] install redis in Ubuntu and the basic usage and configuration of redis
3746. academic circle of cattle II
MySQL 8.0 新特性梳理汇总
Pytorch神经网络【手写数字识别】
Find find files with different extensions
SQL操作:WITH表达式及其应用
Sending webhook of message queue to realize cross application asynchronous callback
ShardingSphere-proxy-5.0.0分布式哈希取模分片实现(四)
MySql DUMP 自动备份数据库 Shell 脚本
【CYW20189】七、HCI命令格式详解
3746. 牛的学术圈 II
[dailyfresh] course record 3 -- product search related
I just learned a cool 3D pyramid stereoscopic effect. Come and have a look
1876. 长度为三且各字符不同的子字符串
使用 gomonkey Mock 函数及方法时遇到 panic: permission denied 的问题及解决方法
修改字典的方法
Precautions for using timestamp type of SQLite3 database