python - Python 中 Plotly express 绘制的意外附加线

标签 python jupyter-notebook plotly plotly-python plotly-express

plotly_with_extra_diagonal_line

Plotly 从原始折线图的起点到终点绘制一条额外的对角线。

其他数据,其他图表工作正常。

只有此数据添加了该行。

为什么会发生这种情况?

我怎样才能解决这个问题?

下面是代码

temp = pd.DataFrame(df[{KEY_WORD}])
temp['date'] = temp.index
fig=px.line(temp.melt(id_vars="date"), x='date', y='value', color='variable')
fig.show()
plotly.offline.plot(fig,filename='Fig_en1')

最佳答案

刚刚遇到了同样的问题——尝试检查 X 轴上的重复值。我正在使用以下代码:

fig = px.line(df, x="weekofyear", y="interest", color="year")
fig.show()

这创造了以下 plotly :

plot with extra lines

我意识到这是因为在某些年份,我与前几年的周 52/53 相关的某些日期的周数因此创建了重复项,例如下面的索引 93 和 145:

    date    interest    query   year    weekofyear
39  2015-12-20  44  home insurance  2015    51
40  2015-12-27  55  home insurance  2015    52
41  2016-01-03  69  home insurance  2016    53
92  2016-12-25  46  home insurance  2016    51
93  2017-01-01  64  home insurance  2017    52
144 2017-12-24  51  home insurance  2017    51
145 2017-12-31  79  home insurance  2017    52
196 2018-12-23  46  home insurance  2018    51
197 2018-12-30  64  home insurance  2018    52
248 2019-12-22  57  home insurance  2019    51
249 2019-12-29  73  home insurance  2019    52

通过修改这些(对于 1 月的日期较高的周数,我从年份列中减去 1)我似乎已经摆脱了这种现象:

plot with no extra lines

注意:由于数据集有点流动,图表之间可能存在一些其他差异。

关于python - Python 中 Plotly express 绘制的意外附加线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60799206/

相关文章:

Python 多处理/套接字 - 错误的文件描述符

python - ipython Notebook调试出现bug时如何更正数值后继续运行?

javascript - 如何在 Plotly 中将图例放在顶部而不是底部?

javascript - plotly js去除标题和标题区域

python - 将 networkx 2D 图转换为 3D 交互式图

带有 gettext 和 .po 文件的 javascript i18n

python - 属性错误: 'module' object has no attribute 'connect'

python - 在 Pandas 表中插入链接

python - 从 python 实现 R 包 TSdist

python - 发生错误时运行函数而不尝试