Plotly在Python中表达的意外行 [英] Unintended Additional line drawn by Plotly express in Python
问题描述
从原始折线图的起点到终点绘制一条额外的对角线.
Plotly draws an extra diagonal line from the start to the endpoint of the original line graph.
其他数据,其他图形也可以正常工作.
Other data, other graphs work fine.
仅此数据会添加行.
为什么会这样?
我该如何解决?
下面是代码
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')
推荐答案
A similar question has been asked and answered in the post How to disable trendline in plotly.express.line?, but in your case I'm pretty sure the problem lies in temp.melt(id_vars="date"), x='date', y='value', color='variable'
. It seems you're transfomring your data from a wide to a long format. You're using color='variable'
without specifying that in temp.melt(id_vars="date")
. And when the color specification does not properly correspond to the structure of your dataset, an extra line like yours can occur. Just take a look at this:
命令1:
fig = px.line(data_frame=df_long, x='Timestamp', y='value', color='stacked_values')
情节1:
命令2:
fig = px.line(data_frame=df_long, x='Timestamp', y='value')
图2:
看到区别了吗?这就是为什么我认为您的fig=px.line(temp.melt(id_vars="date"), x='date', y='value', color='variable')
中存在错误的规定.
See the difference? That's why I think there's a mis-specification in your fig=px.line(temp.melt(id_vars="date"), x='date', y='value', color='variable')
.
因此,请分享您的数据,或者重现问题的数据样本,我们将有更大的机会验证您的问题.
So please share your data, or a sample of your data that reproduces the problem, and I'll have a better chance of verifying your problem.
这篇关于Plotly在Python中表达的意外行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!