我想根据按天和 ID 分组的数据,使用 jupyter notebook +plotly 绘制图表。
数据框看起来像这样:
我在 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 :
完整代码:
这被设置为在离线 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/