如何在plotly.express.line中禁用趋势线? [英] How to disable trendline in plotly.express.line?

查看:504
本文介绍了如何在plotly.express.line中禁用趋势线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我愿意在同一张图表上绘制3个时间序列.数据源是一个pandas.DataFrame()对象,Timestamp的类型为datetime.date,并且使用plotly.express.line()的color参数从同一列Value中绘制了3个不同的时间序列.

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().

图表上显示了3条线,但每条线都带有某种趋势线. 我无法在函数签名中看到如何禁用那些趋势线.你能帮忙吗?

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?

我做了几次尝试,例如使用另一个color,但趋势线仅停留在该位置.

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()

(如果相关,我正在使用jupyterlab)

(If relevant, I am using jupyterlab)

屏幕上的时间戳显示如下(这是[常规]每周时间序列):

Timestamp on the screen appears like this (this are [regular] weekly timeseries) :

并且,根据类型:

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.

推荐答案

简介:

您提供的数据样本只是一张图像,使用起来并不容易,因此我将使用一些随机时间序列样本来提供建议.数据样本中的变量也不符合px.Scatter中使用的变量.

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.

我使用的版本为'4.2.0',无法重现您的问题.希望您无论如何都会发现此建议.

I'm on plotly version '4.2.0' and unable to reproduce your issue. Hopefully you'll find this suggestion useful anyway.

正在使用结构如下的数据...

     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

...我能够生成此图...

...使用此代码:

# 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()

如果您更改...

 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')

...相反,您将获得此图:

肉眼看不到趋势线.

编辑-我想我知道发生了什么事...

仔细查看了您的数据后,我意识到这些线不是趋势线.趋势线通常不会从序列的初始值开始,而不会在序列的最后值结束.这就是这三个系列的全部内容.因此,我认为您的某个地方存在一些糟糕的时间戳记或重复的时间戳记.

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.

这篇关于如何在plotly.express.line中禁用趋势线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆