python - 如何在绘图中为一个 x 值绘制具有多个 y 值的折线图?

标签 python jupyter-notebook plotly

我想根据按天和 ID 分组的数据,使用 jupyter notebook +plotly 绘制图表。

数据框看起来像这样:

DataFrame

我在 matplotlib 中尝试过这个,但我也无法弄清楚。

我想要的是一个图表,其中 x = 小时列,y 显示计数。 例如:

Hour 0: x[0] = [2622, 48, 374, 210, 305, 1427, 83, 12]

Hour 1: x[1] = [2920, 25, 357, 140, 283, 79, 14, 53]

... 

with x = [0, 1, 2, ..., 23]

这意味着每个 x 都有许多 y 值。我怎样才能用 plotly 来绘制这个? 我必须进行降维吗?如果是,怎么办?

预先感谢您的帮助!

最佳答案

此建议不会 100% 符合您的数据结构。但如果这正是您所需要的,我们稍后会处理。

<小时/>

您可以通过构建具有多条迹线的绘图来实现您想要的目的。如果您希望数据点有点抖动以避免重叠,可以使用 go.Box() 来获取:

plotly :

enter image description here

完整代码:

这被设置为在离线 Jupyter Notebook 中工作。绘图数据直接在笔记本中创建。

# imports
import plotly
from plotly import tools
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import pandas as pd
import numpy as np
import plotly.plotly as py
import plotly.graph_objs as go

# setup
init_notebook_mode(connected=True)
np.random.seed(123)

# dataframe
df = pd.DataFrame({'x0':[2622, 48, 374, 210, 305, 1427, 83, 12],
                    'x1':[2920, 25, 357, 140, 283, 79, 14, 53]})

# build traces for each x
traces = {}
for col in df.columns:
    traces['trace_' + col] = go.Box(name = col, y=df[col],
                                    boxpoints = 'all',
                                    pointpos = 0,
                                    marker = dict(color = 'rgb(84, 173, 39)'),
                                    line = dict(color = 'rgba(0,0,0,0)'),
                                    fillcolor = 'rgba(0,0,0,0)')
# convert data to form required by plotly
data = list(traces.values())

# build figure
fig = go.Figure(data, layout)

# plot figure
iplot(fig)

关于python - 如何在绘图中为一个 x 值绘制具有多个 y 值的折线图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56258754/

相关文章:

python - 如何将 Django TestCases 收集到 TestSuites 中并运行它们?

python - 在没有拆分/剥离/内置函数的情况下清理字符串

python - Athena 查询在控制台中有效,但在 sagemaker 中不适用于 boto3 客户端(将 csv 转换为表格)

r - 以 R 语法导出 Plotly

python - 如何处理来自 Gtk.Switch 的 "notify::active"信号? (MVC架构)

python - sqlalchemy 多对多,但相反?

python-3.x - 当我尝试导入任何东西时,如何修复 Jupyter notebook 中的错误

amazon-web-services - Amazon Sagemaker Notebook 实例在我注销时停止执行

flask - 用 flask 完全脱机

R Plotly 默认取消选择跟踪