在 Plotly 中显示不同曲线的并排图。

huangapple go评论52阅读模式
英文:

Side by side plot display for different traces in plotly

问题

这是我的数据框的一部分:

这是我的数据框的一部分:

我正在尝试绘制这个图:

我正在尝试绘制这个图

commit_date 转换为日期格式:

将 'commit_date' 转换为日期格式:

将数据按 api_groupcommit_date 年份和 type_of_change 分组,计算数量并重置索引:

将数据按 'api_group'、'commit_date' 年份和 'type_of_change' 分组,计算数量并重置索引:

创建图形列表:

创建图形列表:

对于每个 api_group,创建一个分组的数据透视表:

对于每个 'api_group',创建一个分组的数据透视表:

创建图形对象:

创建图形对象:

添加 Breaking 和 Non-Breaking 的散点图:

添加 Breaking 和 Non-Breaking 的散点图:

更新图形布局:

更新图形布局:

将图形添加到图形列表:

将图形添加到图形列表:

对于每个图形,显示图形:

对于每个图形,显示图形:
英文:

This is a snippet of my dataframe:

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

I am trying to plot this graph:

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()

For every api_group, it displays the breaking and non breaking changes in it, but the issue is it plots all the graphs for all the 5 groups in a line, but I want them displayed side by side. The graph is this: 在 Plotly 中显示不同曲线的并排图。

Is there a way I could do this, because I am not sure how to plot multiple traces using subplot function in plotly.

答案1

得分: 1

我略微修改了您的数据以得到更好的示例

数据

import pandas as pd
import plotly.express as px

data = [['开发中', 2022, '破坏', 2],
       ['早期', 2016, '破坏', 711],
       ['早期', 2019, '破坏', 1793],
       ['传统', 2018, '不破坏', 5],
       ['传统', 2021, '不破坏', 12],
       ['传统', 2022, '不破坏', 3],
       ['成熟', 2016, '不破坏', 96],
       ['成熟', 2022, '不破坏', 30],
       ['其他', 2015, '不破坏', 23],
       ['其他', 2019, '不破坏', 148],
       ['其他', 2017, '破坏', 15],
       ['其他', 2019, '破坏', 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)

绘图

# 在这里我们利用plotly express制作子图
fig = px.line(
    df,
    x="commit_date",
    y="count",
    facet_col="api_group",
    color="type_of_change")

# 在这里我更改为'点',以防我们正在绘制'不破坏'
for d in fig.data:
    if d["legendgroup"] == '不破坏':
        d["line"]["dash"] = "dot"

fig.show()

在 Plotly 中显示不同曲线的并排图。

英文:

I slightly changed your data to have a better example

Data

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'])

Now ou want to be sure data is correclty ordered

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

Plot

# 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()

在 Plotly 中显示不同曲线的并排图。

答案2

得分: 0

当添加的样本数据是数据处理后的数据时,每个api_group的子图将绘制一个使用api_group中提取的数据的折线图。事先准备了一种线型的字典,从列值中搜索线型名称。将子图规格添加到图形规格中,假设每行有5列;对于5行和1列,设置是反向的。

英文:

When the added sample data is the data after data processing, the subplot for each api_group will draw a line graph with the data extracted in the api_group. A dictionary of line types is prepared beforehand, and line type names are searched in the dictionary from the column values. The subplot specification is added to the graph specification, assuming 5 columns per row; for 5 rows and 1 column, the setting is reversed.

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()

在 Plotly 中显示不同曲线的并排图。

huangapple
  • 本文由 发表于 2023年4月17日 07:04:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定