【Plotly-箱型图】一文搭建python中画出最美箱型图Boxplot用法技巧
本次修炼方法请往下查看
🌈 欢迎莅临我的个人主页 👈这里是我工作、学习、实践 IT领域、真诚分享 踩坑集合,智慧小天地!
🎇 免费获取相关内容文档关注:微信公众号,发送 pandas 即可获取
🎇 相关内容视频讲解 B站
🎓 博主简介:AI算法驯化师,混迹多个大厂搜索、推荐、广告、数据分析、数据挖掘岗位 个人申请专利40+,熟练掌握机器、深度学习等各类应用算法原理和项目实战经验。
🔧 技术专长: 在机器学习、搜索、广告、推荐、CV、NLP、多模态、数据分析等算法相关领域有丰富的项目实战经验。已累计为求职、科研、学习等需求提供近千次有偿|无偿定制化服务,助力多位小伙伴在学习、求职、工作上少走弯路、提高效率,近一年好评率100% 。
📝 博客风采: 积极分享关于机器学习、深度学习、数据分析、NLP、PyTorch、Python、Linux、工作、项目总结相关的实用内容。
下滑查看解决方法
🎯 1. 基本介绍
箱型图(Boxplot)是一种用于展示一组数据分布特征的统计图表,它能够提供数据的最小值、第一四分位数(Q1)、中位数(Q2)、第三四分位数(Q3)和最大值的摘要信息,并且可以直观地识别出数据中的异常值。Plotly是一个强大的图表库,它可以创建交互式的箱型图,增强了数据探索的能力。
🔍 2. 原理介绍
箱型图的构成基于以下统计量:
- 最小值:数据集中的最小非异常值。
第一四分位数(Q1):数据集中25%位置的值。
中位数(Q2,Median):数据集中50%位置的值。
第三四分位数(Q3):数据集中75%位置的值。
最大值:数据集中的最大非异常值。
四分位距(Interquartile Range, IQR):Q3与Q1之间的差值。
🔍 3. 画图实践
3.1 数据准备
我们准备的数据格式如下所示:
# plotly standard imports
import plotly.graph_objs as go
import chart_studio.plotly as py
# Cufflinks wrapper on plotly
import cufflinks
# Data science imports
import pandas as pd
import numpy as np
# Options for pandas
pd.options.display.max_columns = 30
# Display all cell outputs
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
from plotly.offline import iplot
import time
cufflinks.go_offline()
# Set global theme
cufflinks.set_config_file(world_readable=True, theme="pearl")
claps days_since_publication fans link num_responses publication published_date read_ratio read_time reads started_date tags text title title_word_count type views word_count claps_per_word editing_days <tag>Education <tag>Data Science <tag>Towards Data Science <tag>Machine Learning <tag>Python
119 2 574.858594 2 https://medium.com/p/screw-the-environment-but... 0 None 2017-06-10 14:25:00 41.98 7 68 2017-06-10 14:24:00 [Climate Change, Economics] Screw the Environment, but Consider Your Walle... Screw the Environment, but Consider Your Wallet 8 published 162 1859 0.001076 0 0 0 0 0 0
118 18 567.540639 3 https://medium.com/p/the-vanquishing-of-war-pl... 0 None 2017-06-17 22:02:00 32.93 14 54 2017-06-17 22:02:00 [Climate Change, Humanity, Optimism, History] The Vanquishing of War, Plague and Famine Part... The Vanquishing of War, Plague and Famine 8 published 164 3891 0.004626 0 0 0 0 0 0
121 50 554.920762 19 https://medium.com/p/capstone-project-mercedes... 0 None 2017-06-30 12:55:00 20.19 42 215 2017-06-30 12:00:00 [Machine Learning, Python, Udacity, Kaggle] Capstone Project: Mercedes-Benz Greener Manufa... Capstone Project: Mercedes-Benz Greener Manufa... 7 published 1065 12025 0.004158 0 0 0 0 1 1
122 0 554.078160 0 https://medium.com/p/home-of-the-scared-5af0fe... 0 None 2017-07-01 09:08:00 35.85 9 19 2017-06-30 18:21:00 [Politics, Books, News, Media Criticism] Home of the Scared A review of A Culture of Fe... Home of the Scared 4 published 53 2533 0.000000 0 0 0 0 0 0
114 0 550.090507 0 https://medium.com/p/the-triumph-of-peace-f485... 0
3.2 画图实践
我们根据上述的数据画出不同种类的统计柱状图,具体的代码如下所示:
df[df["read_time"] <= 10].pivot(columns="read_time", values="reads").iplot(
kind="box",
colorscale="set2",
xTitle="Read Time",
yTitle="Number of Reads",
title="Box Plot of Reads by Reading Time",
)
如果数据是两列,则可以用如下的方法:
df[["claps", "fans"]].iplot(
secondary_y="fans",
secondary_y_title="Fans",
kind="box",
yTitle="Claps",
title="Box Plot of Claps and Fans",
)
🔍 4. 注意事项
- go.Box中的boxpoints参数设置为’outliers’,表示只显示异常值。
- jitter参数用于在箱型图中为异常值添加轻微的随机偏移,以避免重叠。
- update_layout方法用于定制图表的布局,包括标题、轴标签和模板。
- Plotly图表默认在网页中显示,可以进行缩放、拖动等交互操作。
🔍 5. 总结
Plotly的箱型图为数据的分布特征提供了一种直观且交互性强的展示方式。通过本博客的代码示例,我们学习了如何使用Plotly绘制箱型图,并定制图表的样式和布局。希望这篇博客能够帮助你更好地利用Plotly进行数据探索和分析。