Python Pyecharts繪制箱線圖詳解
本篇博客只是單純的記錄一下自己學(xué)習(xí)Boxplot,沒有過多的解釋,官網(wǎng):》》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'] # 用來正常顯示中文標(biāo)簽 plt.rcParams['axes.unicode_minus']=False # 用來正常顯示負(fù)號 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")
處理數(shù)據(jù):
cnbotypegb=cnboo.groupby(['TYPE','SHOWYEAR'])['BO'].sum().replace() cnbotypegbrst=cnbotypegb.reset_index().replace()

filmtype=cnbotypegbrst['TYPE'].unique()

對行標(biāo)簽和列標(biāo)簽進行轉(zhuǎn)置:
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個,因此循環(huán)12次:
y_data=[]
for i in range(0,12):
y_data.append(cnbopvtv2.iloc[i].tolist())
得到的y_data數(shù)據(jù):
[[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}:系列名稱,:數(shù)據(jù)名稱,{c}:數(shù)值數(shù)組,72xnby2:無
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("電影數(shù)據(jù)",y_data)
)
boxplot.render_notebook()

以上就是Python Pyecharts繪制箱線圖詳解的詳細(xì)內(nèi)容,更多關(guān)于Python Pyecharts箱線圖的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python實現(xiàn)將range()函數(shù)生成的數(shù)字存儲在一個列表中
這篇文章主要介紹了python實現(xiàn)將range()函數(shù)生成的數(shù)字存儲在一個列表中,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
詳解Pytorch中的tensor數(shù)據(jù)結(jié)構(gòu)
torch.Tensor 是一種包含單一數(shù)據(jù)類型元素的多維矩陣,類似于 numpy 的 array,這篇文章主要介紹了Pytorch中的tensor數(shù)據(jù)結(jié)構(gòu),需要的朋友可以參考下2022-09-09
error?conda:ProxyError:Conda?cannot?proceed?due?to?an?
這篇文章主要為大家介紹了error conda:ProxyError:Conda cannot proceed due to an error in your proxy configuration解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
LyScript實現(xiàn)對內(nèi)存堆棧掃描的方法詳解
LyScript插件中提供了三種基本的堆棧操作方法,其中push_stack用于入棧,pop_stack用于出棧,peek_stac可用于檢查指定堆棧位置處的內(nèi)存參數(shù)。所以本文將利用這一特性實現(xiàn)對內(nèi)存堆棧掃描,感興趣的可以了解一下2022-08-08

