不同轨迹的并排绘图显示

问题描述 投票:0回答:2

这是我的数据框的片段:

api_group   commit_date type_of_change  count
Other           2015    Non-Breaking    23
Mature          2016    Non-Breaking    96
Developing      2022    Breaking        2
Legacy          2022    Non-Breaking    3
Other           2019    Non-Breaking    148
Legacy          2018    Non-Breaking    5
Early           2019    Breaking        1793
Legacy          2021    Non-Breaking    12
Early           2016    Breaking        711
Mature          2022    Non-Breaking    30

我正在尝试绘制这张图:

import pandas as pd
import plotly.graph_objs as go

new['commit_date'] = pd.to_datetime(new['commit_date'])

df_grouped = new.groupby([new['api_group'], new['commit_date'].dt.year, 'type_of_change'])['type_of_change'].count().reset_index(name='count')

figs = []
for api_group in df_grouped['api_group'].unique():
    breaking_pivot = df_grouped[df_grouped['api_group'] == api_group].pivot_table(index='commit_date', columns='type_of_change', values='count', fill_value=0)

    fig = go.Figure()
    
    fig.add_trace(go.Scatter(x=breaking_pivot.index, y=breaking_pivot['Breaking'], name='Breaking', line=dict(dash='solid'), line_width=2.5))
    fig.add_trace(go.Scatter(x=breaking_pivot.index, y=breaking_pivot['Non-Breaking'], name='Non-Breaking', line=dict(dash='dot'), line_width=2.5))

    fig.update_layout(title=f'Evolution of Breaking Changes Over Time for {api_group}', width=600, height=500, template='ggplot2', xaxis_title='Year', yaxis_title='Number of Releases')
    
    figs.append(fig)

for fig in figs:
    fig.show()

对于每个

api_group
,它都会显示其中的中断和非中断更改,但问题是它绘制了一行中所有 5 个组的所有图表,但我希望它们并排显示。图是这样的:

有什么办法可以做到这一点,因为我不确定如何在 plotly 中使用

subplot
函数绘制多条轨迹。

plotly plotly-python
2个回答
0
投票

当添加的样本数据为数据处理后的数据时,subplot for each api_group 会用api_group中提取的数据绘制折线图。预先准备线型字典,根据列值在字典中搜索线型名称。子图规范添加到图形规范中,假设每行 5 列;对于 5 行和 1 列,设置相反。

import pandas as pd
import plotly.graph_objs as go
from plotly.subplots import make_subplots

line_dashs = {'Breaking': 'solid', 'Non-Breaking': 'dot'}
api_groups = new['api_group'].unique()

#fig = go.Figure()
fig = make_subplots(rows=1, cols=len(api_groups), subplot_titles=api_groups)

for i,api_group in enumerate(new['api_group'].unique()):
    breaking_pivot = new.query('api_group == @api_group')
    #print(breaking_pivot)
    fig.add_trace(go.Scatter(mode='markers+lines',
                             x=breaking_pivot['commit_date'],
                             y=breaking_pivot['count'],
                             name=api_group,
                             line=dict(dash=line_dashs[breaking_pivot.loc[:,'type_of_change'].unique()[0]]),
                             line_width=2.5,
                            ), row=1, col=i+1)

fig.update_layout(width=1250,
                 height=500,
                 template='ggplot2',
                 xaxis_title='Year',
                 yaxis_title='Number of Releases')
fig.update_xaxes(type='date', tickformat='%Y')

fig.show()


0
投票

我稍微更改了您的数据以获得更好的示例

数据

import pandas as pd
import plotly.express as px

data = [['Developing', 2022, 'Breaking', 2],
       ['Early', 2016, 'Breaking', 711],
       ['Early', 2019, 'Breaking', 1793],
       ['Legacy', 2018, 'Non-Breaking', 5],
       ['Legacy', 2021, 'Non-Breaking', 12],
       ['Legacy', 2022, 'Non-Breaking', 3],
       ['Mature', 2016, 'Non-Breaking', 96],
       ['Mature', 2022, 'Non-Breaking', 30],
       ['Other', 2015, 'Non-Breaking', 23],
       ['Other', 2019, 'Non-Breaking', 148],
       ['Other', 2017, 'Breaking', 15],
       ['Other', 2019, 'Breaking', 5]]

df = pd.DataFrame(
        data, 
        columns=[
            'api_group',
            'commit_date',
            'type_of_change',
            'count'])

现在你想确保数据是正确排序的

df = df.sort_values(
    ["api_group",
     "type_of_change",
     "commit_date"])\
    .reset_index(drop=True)

剧情

# here we leverage plotly express for
# subplots
fig = px.line(
    df,
    x="commit_date",
    y="count",
    facet_col="api_group",
    color="type_of_change")

# here I change to `dot` in case we are plotting
# 'Non-Breaking'
for d in fig.data:
    if d["legendgroup"] == 'Non-Breaking':
        d["line"]["dash"] = "dot"

fig.show()

© www.soinside.com 2019 - 2024. All rights reserved.