【问题标题】:Plotly subplot represent same y-axis name with same color and single legend绘制子图表示具有相同颜色和单个图例的相同 y 轴名称
【发布时间】:2019-08-20 12:23:24
【问题描述】:

我正在尝试为子图中的两个类别创建一个图。第一列代表类别 FF,第二列代表子图中的类别 RF。

x-axis 始终是时间y-axis 是剩余的列。换句话说,它是一列与其余的图。

第一类和第二类总是有相同的列名,只是值不同。

我试图在 for loop 中生成绘图,但问题是 plotly 将每个列名称视为不同的,因此它表示 y-axis 具有相同名称的不同颜色的线。因此,在传说中也创建了一个条目。

例如,在第一行 Time vs price2010 我希望子图 FF 和 RF 都以相同的颜色(比如蓝色)和图例中的单个条目表示。

我尝试在go.Scatter 中添加legendgroup,但没有帮助。

import pandas as pd
from pandas import DataFrame
from plotly import tools
from plotly.offline import init_notebook_mode, plot, iplot
import plotly.graph_objs as go
from plotly.subplots import make_subplots

CarA = {'Time': [10,20,30,40 ],
        'Price2010': [22000,26000,27000,35000],
        'Price2011': [23000,27000,28000,36000],
        'Price2012': [24000,28000,29000,37000],
        'Price2013': [25000,29000,30000,38000],
        'Price2014': [26000,30000,31000,39000],
        'Price2015': [27000,31000,32000,40000],
        'Price2016': [28000,32000,33000,41000]
        }

ff = DataFrame(CarA)

CarB = {'Time': [8,18,28,38 ],
        'Price2010': [19000,20000,21000,22000],
        'Price2011': [20000,21000,22000,23000],
        'Price2012': [21000,22000,23000,24000],
        'Price2013': [22000,23000,24000,25000],
        'Price2014': [23000,24000,25000,26000],
        'Price2015': [24000,25000,26000,27000],
        'Price2016': [25000,26000,27000,28000]
        }

rf = DataFrame(CarB)

Type = {
'FF' : ff,
'RF' : rf
}

fig = make_subplots(rows=len(ff.columns), cols=len(Type), subplot_titles=('FF','RF'),vertical_spacing=0.3/len(ff.columns))

labels = ff.columns[1:]
for indexC, (cat, values) in enumerate(Type.items()):
    for indexP, params in enumerate(values.columns[1:]):
        trace = go.Scatter(x=values.iloc[:,0], y=values[params], mode='lines', name=params,legendgroup=params)
        fig.append_trace(trace,indexP+1, indexC+1)
        fig.update_xaxes(title_text=values.columns[0],row=indexP+1, col=indexC+1)
        fig.update_yaxes(title_text=params,row=indexP+1, col=indexC+1)
        fig.update_layout(height=2024, width=1024,title_text="Car Analysis")
iplot(fig)

【问题讨论】:

    标签: python matplotlib plotly plotly-dash plotly-python


    【解决方案1】:

    这可能不是一个好的解决方案,但到目前为止我只能想出这个技巧。

    fig = make_subplots(rows=len(ff.columns), cols=len(Type), subplot_titles=('FF','RF'),vertical_spacing=0.2/len(ff.columns))
    
    labels = ff.columns[1:]
    colors = [ '#a60000', '#f29979', '#d98d36', '#735c00', '#778c23', '#185900', '#00a66f']
    legend = True
    for indexC, (cat, values) in enumerate(Type.items()):
        for indexP, params in enumerate(values.columns[1:]):
            trace = go.Scatter(x=values.iloc[:,0], y=values[params], mode='lines', name=params,legendgroup=params, showlegend=legend, marker=dict(
                color=colors[indexP]))
            fig.append_trace(trace,indexP+1, indexC+1)
            fig.update_xaxes(title_text=values.columns[0],row=indexP+1, col=indexC+1)
            fig.update_yaxes(title_text=params,row=indexP+1, col=indexC+1)
            fig.update_layout(height=1068, width=1024,title_text="Car Analysis")
        legend = False
    

    【讨论】:

      【解决方案2】:

      如果您将数据合并到single tidy data frame,您可以使用简单的Plotly Express 调用来制作图表:px.line()colorfacet_rowfacet_col

      【讨论】:

        猜你喜欢
        • 2021-09-16
        • 2015-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-26
        • 2015-04-04
        • 2014-05-20
        相关资源
        最近更新 更多