How to disable trendline in plotly.express.line?

◇◆丶佛笑我妖孽 提交于 2020-01-05 04:31:46

问题


I am willing to plot 3 timeseries on the same chart. 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().

The 3 lines show on the chart, but each one is accompanied by some sort of trendline. I can't see in the function signature how to disable those trendlines. Can you please help?

I have made several attempts, e.g. using another color, but the trendlines just stay there.

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)

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.


回答1:


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.

I'm on plotly version '4.2.0' and unable to reproduce your issue. 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.



来源:https://stackoverflow.com/questions/59193976/how-to-disable-trendline-in-plotly-express-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!