【Plotly-折线图】一文搭建python中画出最美折线图plotly.iplot用法技巧
本次修炼方法请往下查看
🌈 欢迎莅临我的个人主页 👈这里是我工作、学习、实践 IT领域、真诚分享 踩坑集合,智慧小天地!
🎇 免费获取相关内容文档关注:微信公众号,发送 pandas 即可获取
🎇 相关内容视频讲解 B站
🎓 博主简介:AI算法驯化师,混迹多个大厂搜索、推荐、广告、数据分析、数据挖掘岗位 个人申请专利40+,熟练掌握机器、深度学习等各类应用算法原理和项目实战经验。
🔧 技术专长: 在机器学习、搜索、广告、推荐、CV、NLP、多模态、数据分析等算法相关领域有丰富的项目实战经验。已累计为求职、科研、学习等需求提供近千次有偿|无偿定制化服务,助力多位小伙伴在学习、求职、工作上少走弯路、提高效率,近一年好评率100% 。
📝 博客风采: 积极分享关于机器学习、深度学习、数据分析、NLP、PyTorch、Python、Linux、工作、项目总结相关的实用内容。
下滑查看解决方法
🎯 1. 基本介绍
折线图是数据可视化中用于展示数据随时间或有序类别变化趋势的经典图表类型。Plotly是一个交互式图表库,它能够创建丰富、动态且高度可定制的折线图,为用户提供了探索数据的全新方式。
🔍 2. 画图实践
2.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")
user_id item_id category behavior time date hour
0 565283 1691396 903809 pv 1512116234 2017-12-01 16
1 312117 4381601 982926 pv 1511829760 2017-11-28 8
2 253828 5082804 2885642 pv 1512228469 2017-12-02 23
3 776488 5048431 4801426 pv 1512302885 2017-12-03 20
4 884522 1649923 4145813 pv 1511870178 2017-11-28 19
5 502737 4275081 600175 pv 1511701857 2017-11-26 21
6 986023 4355178 3898483 pv 1511707644 2017-11-26 22
7 103840 3793189 317735 pv 1511961741 2017-11-29 21
8 397937 3642490 2520377 pv 1512289398 2017-12-03 16
9 1986 1400268 2520377 pv 1511693349 2017-11-26 18
10 784120 5019683 4145813 pv 1512089120 2017-12-01 8
11 865508 2359495 982926 pv 1511685415 2017-11-26 16
2.2 画图实践
我们根据上述的数据画出不同时间段不同行为的折线图,具体的代码如下所示:
import plotly.express as px
# 使用color参数设置不同类别的颜色
fig = px.line(day_behavior_cnt, x="days", y="hour", color="behavior", markers=True, title="用户不同评分随时间变化趋势")
# 显示图表
fig.show()
如果数据是两列,则可以用如下的方法:
date pv uv
0 2017-11-25 52043 47680
1 2017-11-26 53585 49167
2 2017-11-27 50657 46585
3 2017-11-28 49366 45431
4 2017-11-29 51323 47284
5 2017-11-30 51397 47392
6 2017-12-01 54342 49805
7 2017-12-02 68838 63070
8 2017-12-03 68449 62685
pv_daily.iplot(
x='date',
y=['pv', 'uv'],
kind='scatter',
mode="lines+markers",
opacity=0.5,
size=8,
symbol=1,
xTitle="date",
yTitle="cnt",
title=go.layout.Title(text="pv vs uv " ,x=0.5)
)
🔍 3. 高阶用法
我们也可以很方便的设置散点图并对其进行线性的拟合,并查看线性拟合的效果,具体的代码如下所示:
tds.sort_values("read_time").iplot(
x="read_time",
y="read_ratio",
xTitle="Read Time",
yTitle="Reading Percent",
text="title",
mode="markers+lines",
bestfit=True,
bestfit_colors=["blue"],
title="Reading Percent vs Reading Time",
)
如果要对其中某个列别进行分类的话,可以采用如下的代码:
df.iplot(
x="read_time",
y="read_ratio",
categories="publication",
xTitle="Read Time",
yTitle="Reading Percent",
title="Reading Percent vs Read Time by Publication",
)
我们也可以很方便的设置layout来控制图片的输出,具体如下所示:
df.iplot(
x="word_count",
y="views",
categories="publication",
mode="markers",
text="title",
size=8,
layout=dict(
xaxis=dict(title="Word Count"),
yaxis=dict(title="Views"),
title="Views vs Word Count by Publication",
),
)
如果想要某个变量来控制图片的大小显示,同时需要对气泡的图片添加text文字,具体的操作如下所示:
text = [
f"Title: {t} <br> Ratio: {r:.2f}%" for t, r in zip(tds["title"], tds["read_ratio"])
]
tds.iplot(
x="word_count",
y="reads",
opacity=0.8,
size=tds["read_ratio"],
text=text,
mode="markers",
theme="pearl",
layout=dict(
xaxis=dict(type="log", title="Word Count"),
yaxis=dict(title="Reads"),
title="Reads vs Log Word Count Sized by Read Ratio",
),
)
🔍 4. 注意事项
- Plotly的go.Scatter函数中的mode参数设置为’lines+markers’,表示同时显示折线和数据点标记。
- update_layout方法用于定制图表的布局,如标题、轴标签等。
- Plotly图表默认在网页中显示,可以在多种环境下进行交互操作。
- 确保时间序列数据正确处理,使用Pandas的date_range生成日期范围。
🔍 5. 总结
Plotly提供了一种现代且交互式的方式来创建折线图,它不仅能够展示数据的趋势,还能够提供丰富的用户交互体验。通过本博客的代码示例,我们学习了如何使用Plotly绘制折线图,并定制图表的样式和布局。希望这篇博客能够帮助你更好地利用Plotly进行动态数据可视化。