iplot奇怪连接Y轴

时间:2018-03-08 18:56:10

标签: python plotly

当我运行de命令时:

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

它绘制了以下图表:

enter image description here

观察中这种奇怪的奇怪联系是什么?

仔细看看:

enter image description here

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

enter image description here

1 个答案:

答案 0 :(得分:1)

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