gpt4 book ai didi

python - iplot 奇怪的连接 Y 轴

转载 作者:太空宇宙 更新时间:2023-11-03 14:01:55 25 4
gpt4 key购买 nike

当我运行 de 命令时:

iplot(df_final[['ds', 'yhat']].set_index('ds').to_iplot())

它绘制了以下图表:

enter image description here

观察结果之间的这种奇怪的联系是什么?

仔细一看是:

enter image description here

当我不使用iplot时,它是正确的:

enter image description here

最佳答案

我尝试通过添加重复日期来使用连接线在 iplot() 中重新创建绘图。摆脱这些线条的一种解决方案是在绘图之前按 x 轴即日期(此处为“ds”列)对数据框进行排序。

# Creating sample data
ds = pd.date_range(pd.datetime.today(), periods=100).tolist()
df = pd.DataFrame(ds, columns=['ds'])
df['yhat'] = np.sin(30*3.14/180)*np.random.randn(100)
df.iplot(x='ds', y='yhat')
# plot link below

Plot without duplicate dates using iplot()

# Create duplicate dates and adding it to the dataframe
df2 = df.append(df.sample(n=5, replace=False)) # this line creates 5 duplicate rows
df2[['ds', 'yhat']].iplot(x='ds', y='yhat')
# plot link below

Plot with duplicate dates and connecting lines using iplot()

# Same plot with plot() works just fine
df2.plot(x='ds', y='yhat')
# plot link below

Plot with duplicate dates using plot()

# Possible solution:
# Sort by date column
df2 = df2.sort_values(by='ds', ascending=True)
# Plot with sorted values
df2[['ds', 'yhat']].iplot(x='ds', y='yhat')
# plot link below

Plot with sorted date column with duplicate dates using iplot()

关于python - iplot 奇怪的连接 Y 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49180494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com