简体   繁体   English

使用python中的多个子图将线型映射到绘图图中的数据

[英]Map linetype to data in plotly figure with multiple subplots in python

I'm trying to map linetype to a categorical data column in a pandas dataframe in a plotly figure with multiple subplots.我正在尝试将线型映射到带有多个子图的plotly图中的pandas数据plotly的分类数据列。

I have a dataframe df equivalent to:我有一个数据框df相当于:

from datetime import datetime
import numpy as np
df = pd.DataFrame({'date':pd.date_range(start='01/01/2020',periods=100),
                   'y_one':np.linspace(1,100,100),
                   'y_two':np.linspace(100,1,100)})
df['today'] = df.date.apply(lambda date: 'the_future' if date > datetime.today() else 'the_past')

I need to plot multiple lines ( y_one , y_two ) over date_range .我需要绘制多行( y_oney_two以上) date_range I'd like to have the lines solid for the past and dashed for the future, ie have linetype mapped to df['today'] .我想让过去的线条是实线,未来是虚线,即将线型映射到df['today']

The plotly code I've implemented so far is:到目前为止我实现的情节代码是:

import plotly.graph_objects as go
from plotly.offline import plot
fig = go.Figure()
fig.add_trace(
    go.Scatter(
        x=df["date"],
        y=df["y_one"],
        mode="lines",
        line=dict(color='black',
                    )
        )
    )
fig.add_trace(
    go.Scatter(
        x=df["date"],
        y=df["y_two"],
        mode="lines",
        line=dict(color='red'),
        )
    )
plot(fig)

Is there a way to implement this use case in plotly with multiple subplots?是否有实现这个用例的方式plotly多的次要情节?

Possibly not the most elegant solution but you can eventually work with loops if you have a large number of columns to plot you can consider to use a loop.可能不是最优雅的解决方案,但如果您有大量要绘制的列,您最终可以使用循环工作,您可以考虑使用循环。

import plotly.graph_objects as go
import pandas as pd
import numpy as np
df = pd.DataFrame({'date':pd.date_range(start='01/01/2020',periods=100),
                   'y_one':np.linspace(1,100,100),
                   'y_two':np.linspace(100,1,100)})

df["is_future"] = df["date"]>pd.datetime.today()

fig = go.Figure()
fig.add_trace(
    go.Scatter(
        x=df[df["is_future"]==False]["date"],
        y=df[df["is_future"]==False]["y_one"],
        mode="lines",
        legendgroup="y_one",
        name = "y_one",
        line=dict(color='black',)))

fig.add_trace(
    go.Scatter(
        x=df[df["is_future"]==True]["date"],
        y=df[df["is_future"]==True]["y_one"],
        mode="lines",
        legendgroup="y_one",
        name = "y_one",
        showlegend=False,
        line=dict(color='black',dash='dash')))

fig.add_trace(
    go.Scatter(
        x=df[df["is_future"]==False]["date"],
        y=df[df["is_future"]==False]["y_two"],
        mode="lines",
        legendgroup="y_two",
        name = "y_two",
        line=dict(color='red'),))

fig.add_trace(
    go.Scatter(
        x=df[df["is_future"]==True]["date"],
        y=df[df["is_future"]==True]["y_two"],
        mode="lines",
        legendgroup="y_two",
        name = "y_two",
        showlegend=False,
        line=dict(color='red', dash='dash'),))


fig.show()

在此处输入图片说明

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

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