绘图子图用相同的颜色和单个图例表示相同的yaxis名称

2024-06-28 11:11:15 发布

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

我试图在一个子图中为两个类别创建一个绘图。第一列表示FF类,第二列表示子批次中的RF类。在

x-axis总是时间,而y-axis是剩余的列。换言之,这是一个只有一列的图。在

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

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

例如,在第一行时间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)

output of the code


Tags: columnsfromimport绘图forfigparamsplotly
2条回答

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

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

如果将数据合并到single tidy data frame,则可以使用一个简单的Plotly Express调用来生成图表:px.line()与{}、facet_row和{}

相关问题 更多 >