📌  相关文章
📜  如何在Python中使用 Plotly 设置具有分组图例的多个子图?

📅  最后修改于: 2022-05-13 01:54:35.856000             🧑  作者: Mango

如何在Python中使用 Plotly 设置具有分组图例的多个子图?

先决条件: Python剧情

在本文中,我们将探讨如何在Python中使用 Plotly 设置具有分组图例的多个子图。

这是展示图例图例的有用方法,因为它可以揭示有关复杂信息的大量信息。 plotly 库中的图例基本上描述了图形元素。 Plotly 数据可视化最具欺骗性的强大功能之一是,当将光标指向出现的点标签时,查看者能够快速分析足够数量的数据信息。

示例 1:

在这里,在showlegend 参数之一的帮助下,带有 3 个分组图例的两个堆叠子图:真/假。

因此显示了 3 个不同的分组图例。

Python3
from plotly.subplots import make_subplots
import plotly.graph_objects as go
from plotly import offline
 
fig = make_subplots(rows=2, cols=1)
 
fig.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70],
                         name="2017", legendgroup="2017",
                         line=dict(color='blue')),
              row=1, col=1)
 
fig.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 60],
                         name="2018", legendgroup="2018",
                         line=dict(color='red')),
              row=1, col=1)
 
 
fig.add_trace(go.Scatter(x=[10, 20, 30], y=[20, 25, 30],
                         name="2017", legendgroup="2017",
                         line=dict(color='blue'),
                         showlegend=False
                         ),
              row=2, col=1)
 
fig.append_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70],
                            name="2018", legendgroup="2018",
                            line=dict(color='green'),
                            showlegend=True),
                 row=2, col=1)
 
 
fig.update_layout(height=600, width=600,
                  title_text="Geeksforgeeks - Stacked Subplots")
fig.show()


Python3
from plotly.subplots import make_subplots
import plotly.graph_objects as go
from plotly import offline
 
fig = make_subplots(rows=2, cols=2)
 
fig.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70],
                         name="2017", legendgroup="2017",
                         line=dict(color='blue')),
              row=1, col=1)
 
fig.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 60],
                         name="2018", legendgroup="2018",
                         line=dict(color='red')),
              row=1, col=1)
 
 
fig.add_trace(go.Scatter(x=[10, 20, 30], y=[20, 25, 30],
                         name="2017", legendgroup="2017",
                         line=dict(color='blue'),
                         showlegend=False
                         ),
              row=1, col=2)
 
fig.append_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70],
                            name="2018", legendgroup="2018",
                            line=dict(color='yellow'),
                            showlegend=True),
                 row=1, col=2)
 
fig.append_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70],
                            name="2018", legendgroup="2018",
                            line=dict(color='red'),
                            showlegend=False),
                 row=2, col=1)
 
 
fig.add_trace(go.Scatter(x=[10, 20, 30], y=[20, 25, 30],
                         name="2017", legendgroup="2017",
                         line=dict(color='pink'),
                         showlegend=True
                         ),
              row=2, col=2)
 
 
fig.update_layout(height=600, width=600,
                  title_text="Stacked Subplots")
fig.show()


输出:

示例 2:

在这里,四个堆叠的子图和 4 个分组的图例。

因此显示了 4 个不同的分组图例。

Python3

from plotly.subplots import make_subplots
import plotly.graph_objects as go
from plotly import offline
 
fig = make_subplots(rows=2, cols=2)
 
fig.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70],
                         name="2017", legendgroup="2017",
                         line=dict(color='blue')),
              row=1, col=1)
 
fig.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 60],
                         name="2018", legendgroup="2018",
                         line=dict(color='red')),
              row=1, col=1)
 
 
fig.add_trace(go.Scatter(x=[10, 20, 30], y=[20, 25, 30],
                         name="2017", legendgroup="2017",
                         line=dict(color='blue'),
                         showlegend=False
                         ),
              row=1, col=2)
 
fig.append_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70],
                            name="2018", legendgroup="2018",
                            line=dict(color='yellow'),
                            showlegend=True),
                 row=1, col=2)
 
fig.append_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70],
                            name="2018", legendgroup="2018",
                            line=dict(color='red'),
                            showlegend=False),
                 row=2, col=1)
 
 
fig.add_trace(go.Scatter(x=[10, 20, 30], y=[20, 25, 30],
                         name="2017", legendgroup="2017",
                         line=dict(color='pink'),
                         showlegend=True
                         ),
              row=2, col=2)
 
 
fig.update_layout(height=600, width=600,
                  title_text="Stacked Subplots")
fig.show()

输出: