当我运行de命令时:

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


它绘制了以下图表:

python - iplot奇怪的连接的Y轴-LMLPHP

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

仔细看看是:

python - iplot奇怪的连接的Y轴-LMLPHP

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

python - iplot奇怪的连接的Y轴-LMLPHP

最佳答案

我试图通过添加重复的日期来用连接线在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()

10-04 22:12