简体   繁体   English

如何在 plotly.express.line 中禁用趋势线?

[英]How to disable trendline in plotly.express.line?

I am willing to plot 3 timeseries on the same chart.我愿意在同一张图表上绘制 3 个时间序列。 Datasource is a pandas.DataFrame() object, the type of Timestamp being datetime.date , and the 3 different time series drawn from the same column Value using the color argument of plotly.express.line() .数据源是一个pandas.DataFrame()对象, Timestamp的类型是datetime.date ,以及使用plotly.express.line()的颜色参数从同一列Value绘制的 3 个不同的时间序列。

The 3 lines show on the chart, but each one is accompanied by some sort of trendline.图表上显示了 3 条线,但每条线都伴随着某种趋势线。 I can't see in the function signature how to disable those trendlines.我在函数签名中看不到如何禁用这些趋势线。 Can you please help?你能帮忙吗?

I have made several attempts, eg using another color , but the trendlines just stay there.我做了几次尝试,例如使用另一种color ,但趋势线只是停留在那里。

Please find below the code snippet and the resulting chart.请在下面的代码片段和结果图表中找到。

import plotly.io as pio
import plotly.express as px
pio.renderers = 'jupyterlab'
fig = px.line(data_frame=df, x='Timestamp', y='Value', color='Position_Type')
fig.show()

(If relevant, I am using jupyterlab ) (如果相关,我正在使用jupyterlab

散点图截图

Timestamp on the screen appears like this (this are [regular] weekly timeseries) :屏幕上的时间戳显示如下(这是 [常规] 每周时间序列):

大豆时间序列数据

And, as per the type:而且,根据类型:

type(df.Timestamp[0])
> datetime.date

I am adding that it looks like the lines that I first thought were trendlines would rather be straight lines from the first datapoint to the last datapoint of each time series.我补充说,看起来我最初认为是趋势线的线条更像是从每个时间序列的第一个数据点到最后一个数据点的直线。

Introduction:介绍:

Your provided data sample is an image, and not very easy to work with, so I'm going to use some sampled random time series to offer a suggestion.您提供的数据样本是一个图像,不太容易处理,因此我将使用一些采样的随机时间序列来提供建议。 The variables in your datasample don't match the ones you've used in px.Scatter either by the way.顺便说一下,您的数据样本中的变量与您在px.Scatter使用的变量不匹配。

I'm on plotly version '4.2.0' and unable to reproduce your issue.我使用的是'4.2.0'版,无法重现您的问题。 Hopefully you'll find this suggestion useful anyway.希望无论如何你会发现这个建议很有用。

Using data structured like this...使用这样结构的数据......

     Timestamp Position_type      value
145 2020-02-15        value3  86.418593
146 2020-02-16        value3  78.285128
147 2020-02-17        value3  79.665202
148 2020-02-18        value3  84.502445
149 2020-02-19        value3  91.287312

...I'm able to produce this plot... ......我能够制作这个情节......

在此处输入图片说明

...using this code: ...使用此代码:

# imports
from plotly.subplots import make_subplots
import plotly.graph_objs as go
import pandas as pd
import numpy as np

# data
np.random.seed(123)
frame_rows = 50
n_plots = 2
frame_columns = ['V_'+str(e) for e in list(range(n_plots+1))]
df = pd.DataFrame(np.random.uniform(-10,10,size=(frame_rows, len(frame_columns))),
                  index=pd.date_range('1/1/2020', periods=frame_rows),
                    columns=frame_columns)
df=df.cumsum()+100
df.iloc[0]=100
df.reset_index(inplace=True)

df.columns=['Timestamp','value1', 'value2', 'value3' ]
varNames=df.columns[1:]


# melt dataframe with timeseries from wide to long format.
# YOUR dataset seems to be organized in a long format since
# you're able to set color using a variable name
df_long = pd.melt(df, id_vars=['Timestamp'], value_vars=varNames, var_name='Position_type', value_name='value')
#df_long.tail()

# plotly time
import plotly.io as pio
import plotly.express as px
#pio.renderers = 'jupyterlab'
fig = px.scatter(data_frame=df_long, x='Timestamp', y='value', color='Position_type')
#fig = px.line(data_frame=df_long, x='Timestamp', y='value', color='Position_type')
fig.show()

If you change...如果你改变...

 px.scatter(data_frame=df_long, x='Timestamp', y='value', color='Position_type')

...to... ...到...

fig = px.line(data_frame=df_long, x='Timestamp', y='value', color='Position_type')

...you'll get this plot instead: ...你会得到这个情节:

No trendlines as far as the eye can see.就眼睛所见,没有趋势线。

在此处输入图片说明

Edit - I think I know what's going on...编辑 - 我想我知道发生了什么......

Having taken a closer look at your figure, I've realized that those lines are not trendlines.仔细观察你的图后,我意识到这些线不是趋势线。 A trendline doesn't normally start at the initial value of a series and end up at the last value of the series.趋势线通常不会以系列的初始值开始并以系列的最后一个值结束。 And that's what happening here for all three series.这就是所有三个系列发生的事情。 So I think you've got some bad or duplicate timestamps somewhere.所以我认为你在某处有一些错误或重复的时间戳。

df_melt = df_melt.sort_values('datetime_id')

Sorting got rid of those "wrap-arounds".排序摆脱了那些“环绕”。 Thanks for the suggestions above.感谢上面的建议。 Using Plotly 4.8.2.使用 Plotly 4.8.2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM