海生色调

2024-05-19 01:38:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个数据帧Gas Price Brazil/ Data Frame
我只从这个DF得到汽油值,并想绘制每个地区(REGIAO)的平均价格(PREÇO MEDIO)随时间(YEARS-ANO)的变化图

我用了带色调的Seaborn

Seaborn Plot

但当我试图在Plotly绘制相同的东西时,结果是:

Plotly Plot

我怎么能和plotly得到相同的情节?你知道吗

我搜索并找到了:Seaborn Hue on Plotly

但这对我没用。你知道吗


Tags: 数据dfdata绘制seabornplotlypreframe
1条回答
网友
1楼 · 发布于 2024-05-19 01:38:56

答案:

使用plotly express和color属性可以实现同样的效果:

fig = px.line(dfm, x="dates", y="value", color='variable')

细节:

您还没有详细描述数据的结构,但是像这样分配色调通常意味着要应用于数据结构,例如。。。你知道吗

Date    Variable    Value
01.01.2020    A    100
01.01.2020    B    90
01.02.2020    A    110
01.02.2020    B    120

。。。其中,为与时间戳列关联的不同变量名指定了唯一的色调或颜色,其中每个时间戳出现的次数与变量出现的次数相同。你知道吗

似乎seaborn也是这样:

hue : name of variables in data or vector data, optional

Grouping variable that will produce points with different colors. Can be either categorical or numeric, although color mapping will behave differently in latter case.

您可以通过在go.Scatter()中使用color属性来实现同样的效果,但是您似乎也可以很好地利用plotly.express。在提供正确的数据样本之前,我将向您展示如何使用numpypandas在数据帧中使用一些采样数据。你知道吗

绘图:

enter image description here

代码:

# imports
import numpy as np
import pandas as pd
import plotly.express as px

# sample time series data
np.random.seed(123)
df = pd.DataFrame(np.random.randint(-10,12,size=(50, 4)), columns=list('ABCD'))
datelist = pd.date_range(pd.datetime(2020, 1, 1).strftime('%Y-%m-%d'), periods=50).tolist()
df['dates'] = datelist 
df = df.set_index(['dates'])
df.index = pd.to_datetime(df.index)
df.iloc[0]=0
df=df.cumsum().reset_index()

# melt data to provide the data structure mentioned earlier
dfm=pd.melt(df, id_vars=['dates'], value_vars=df.columns[1:])
dfm.set_index('dates')
dfm.head()

# plotly
fig = px.line(dfm, x="dates", y="value", color='variable')
fig.show()

相关问题 更多 >

    热门问题