Python Pyecharts绘制箱线图详解
作者:渴望成为寂寞胜者 时间:2021-03-31 05:57:05
本篇博客只是单纯的记录一下自己学习Boxplot,没有过多的解释,官网:》》Boxplot
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['font.sans-serif']=['Microsoft YaHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号
from datetime import datetime
plt.figure(figsize=(16,10))
import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.faker import Faker
from pyecharts.charts import Bar
import os
from pyecharts.options.global_options import ThemeType
cnboo=pd.read_excel("c.xls")
处理数据:
cnbotypegb=cnboo.groupby(['TYPE','SHOWYEAR'])['BO'].sum().replace()
cnbotypegbrst=cnbotypegb.reset_index().replace()
filmtype=cnbotypegbrst['TYPE'].unique()
对行标签和列标签进行转置:
cnbotypegbrst.T.index
cnbopvt=cnbotypegbrst.pivot(index='TYPE',
columns='SHOWYEAR',
values='BO'
)
cnbopvtv2=cnbopvt.iloc[:,2:].replace()
cnbopvtv2.index
cnbopvtv2=cnbopvtv2.fillna(0).replace()
xlist=cnbopvtv2.index.tolist()
xlist一共有12个,因此循环12次:
y_data=[]
for i in range(0,12):
y_data.append(cnbopvtv2.iloc[i].tolist())
得到的y_data数据:
[[47923.0,
64988.0,
0.0,
80506.0,
0.0,
69628.0,
69960.0,
0.0,
104853.0,
539542.0,
157535.0],
[48249.0,
160800.0,
153735.0,
336616.0,
370696.0,
263476.0,
916503.0,
1010848.0,
1828313.0,
1835840.0,
875026.0],
[30916.0,
160800.0,
86419.0,
65659.0,
39472.0,
263476.0,
201318.0,
309825.0,
226052.0,
1835840.0,
152997.0],
[30916.0,
160800.0,
18648.0,
65659.0,
39472.0,
263476.0,
201318.0,
309825.0,
226052.0,
1835840.0,
152997.0],
[53837.0,
91838.0,
36093.0,
100303.0,
58872.0,
285139.0,
647028.0,
451028.0,
765806.0,
1063170.0,
454325.0],
[53837.0,
22874.0,
14934.0,
100303.0,
124699.0,
285139.0,
320647.0,
430395.0,
235246.0,
89988.0,
15283.0],
[20510.0,
22874.0,
14934.0,
18806.0,
124699.0,
41184.0,
320647.0,
430395.0,
235246.0,
89988.0,
15283.0],
[40329.0,
22874.0,
85732.0,
36994.0,
124699.0,
41184.0,
320647.0,
430395.0,
118754.0,
89988.0,
15283.0],
[44745.0,
22874.0,
85732.0,
36994.0,
124699.0,
41184.0,
62967.0,
430395.0,
118754.0,
89988.0,
15283.0],
[28092.0,
72729.0,
82385.0,
182193.0,
255790.0,
259325.0,
62967.0,
160092.0,
118754.0,
136152.0,
112725.0],
[51321.0,
213633.0,
148063.0,
225026.0,
258684.0,
563843.0,
344841.0,
82557.0,
179793.0,
139666.0,
465533.0],
[15524.0,
38100.0,
86684.0,
225026.0,
31579.0,
150820.0,
344841.0,
82557.0,
179793.0,
139666.0,
465533.0]]
最后绘制图表:
import pyecharts.options as opts
from pyecharts.charts import Grid, Boxplot, Scatter
scatter_data = [650, 620, 720, 720, 950, 970]
box_plot = Boxplot({"Theme":ThemeType.ESSOS})
box_plot = (
box_plot.add_xaxis(xaxis_data=xlist)
.add_yaxis(series_name="", y_axis=box_plot.prepare_data(y_data))
.set_global_opts(
title_opts=opts.TitleOpts(
pos_left="center", title="2009-2019中国电影票房分类箱型图"
),
tooltip_opts=opts.TooltipOpts(trigger="item", axis_pointer_type="shadow"),
xaxis_opts=opts.AxisOpts(
type_="category",
boundary_gap=True,
splitarea_opts=opts.SplitAreaOpts(is_show=False),
axislabel_opts=opts.LabelOpts(formatter="{value}"),
splitline_opts=opts.SplitLineOpts(is_show=False),
),
yaxis_opts=opts.AxisOpts(
type_="value",
name="票房(万元)",
splitarea_opts=opts.SplitAreaOpts(
is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)
),
),
)
.set_series_opts(tooltip_opts=opts.TooltipOpts(formatter="{a}: {c}"))
)# {a}:系列名称,{b}:数据名称,{c}:数值数组,{d}:无
scatter = (
Scatter()
.add_xaxis(xaxis_data=xlist)
.add_yaxis(series_name="", y_axis=scatter_data)
.set_global_opts(
title_opts=opts.TitleOpts(
pos_left="10%",
pos_top="90%",
title="upper: Q3 + 1.5 * IQR \nlower: Q1 - 1.5 * IQR",
title_textstyle_opts=opts.TextStyleOpts(
border_color="#999", border_width=1, font_size=14
),
),
yaxis_opts=opts.AxisOpts(
axislabel_opts=opts.LabelOpts(is_show=False),
axistick_opts=opts.AxisTickOpts(is_show=False),
),
)
)
grid = (
Grid(init_opts=opts.InitOpts(width="600px", height="400px"))
.add(
box_plot,
grid_opts=opts.GridOpts(pos_left="10%", pos_right="10%", pos_bottom="15%"),
)
.add(
scatter,
grid_opts=opts.GridOpts(pos_left="10%", pos_right="10%", pos_bottom="15%"),
)
)
grid.render_notebook()
也可以直接使用下面的代码,简单的绘制一张图:
boxplot=(Boxplot()
.add_xaxis(xlist)
.add_yaxis("电影数据",y_data)
)
boxplot.render_notebook()
来源:https://blog.csdn.net/wxfighting/article/details/123775978
标签:Python,Pyecharts,箱线图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python中如何使用正则表达式的集合字符示例
2022-05-31 13:00:56
使用Python从零开始撸一个区块链
2021-06-02 07:28:22
![](https://img.aspxhome.com/file/2023/7/66827_0s.jpg)
Python和php通信乱码问题解决方法
2023-03-04 14:50:43
php strftime函数的详细用法
2023-06-07 19:09:37
Python基础教程之输入输出和运算符
2021-10-20 13:41:42
Python解决pip install时出现的Could not fetch URL问题
2023-08-01 14:24:45
![](https://img.aspxhome.com/file/2023/7/61997_0s.png)
利用Python小工具实现3秒钟将视频转换为音频
2023-05-26 21:11:34
![](https://img.aspxhome.com/file/2023/9/124199_0s.jpg)
python日记(使用TCP实现的对话客户端和服务器)
2023-01-25 19:42:54
numpy 数组拷贝地址所引起的同步替换问题
2023-11-07 18:00:06
PHP实现获取第一个中文首字母并进行排序的方法
2023-10-30 12:29:08
Python编程实现简易的音乐播放器基本操作
2022-05-25 08:41:16
![](https://img.aspxhome.com/file/2023/5/80685_0s.jpg)
拖拽翻页(ThrowPage)详解 cool
2009-12-02 09:54:00
![](https://img.aspxhome.com/file/UploadPic/200912/2/01-46s.jpg)
深入浅析python中的多进程、多线程、协程
2022-06-05 15:59:27
详解Go语言中关于包导入必学的 8 个知识点
2023-07-09 05:38:11
![](https://img.aspxhome.com/file/2023/9/87619_0s.png)
Sun正式发布MySQL 5.1版 简化数据库应用
2008-12-11 15:15:00
利用XMLBean轻轻松松读写XML
2008-09-04 11:25:00
通过FSO进行页面计数
2008-11-27 16:02:00
python微信跳一跳系列之棋子定位颜色识别
2023-01-16 04:52:49
![](https://img.aspxhome.com/file/2023/4/66814_0s.png)
Golang CSP并发机制及使用模型
2023-09-15 16:13:27
![](https://img.aspxhome.com/file/2023/4/101674_0s.jpg)
Python K-means实现简单图像聚类的示例代码
2023-06-30 10:40:58
![](https://img.aspxhome.com/file/2023/6/99126_0s.png)