Python 如何使用Plotly设置多个子图和分组图例

Python 如何使用Plotly设置多个子图和分组图例

多个子图是通过分配不同的图表来定义的。Plotly是一个强大的Python库,用于创建交互式数据研究和演示的可视化图表。其能够设置多个子图,即在单个图表中放置的独立图表,是其主要特点之一。这个功能允许我们将各种数据集或变量并排比较和展示,以便更简单地概览数据。在Python中,我们有一些内置的函数- add_trace,update_layout和show,可以用来设置使用Plotly在Python中的多个子图和分组图例。

语法

以下是示例中使用的语法-

add_trace()

这是Python中的一个内置方法,使用名为plotly的模块。它接受Scatter作为参数,用于创建新的轨迹到图表中。例如,散点图可能有多个轨迹,每个轨迹代表不同的数据点集。

update_layout()

update_layout()方法用于更改Plotly图表的布局。

show()

show 方法用于在程序结束时将所需的图形结果作为输出。

步骤

以下步骤为:

第1步: 通过使用名为 plotly.graph_objects 的模块创建子图数据点,并将对象引用命名为 go。

第2步: 使用 plotly.subplots 模块中的 make_subplots 函数创建另一个对象引用。

第3步: 然后使用 make_subplots 对象引用,通过使用 subplot_title 设置每个子图标题的名称。

第4步: 创建跟踪以设置组图例和数据点。

第5步: 使用内置函数 update_layout 更新图形布局,该函数接受参数 width 和 height。

第6步: 最后,使用 show() 方法显示结果。

示例

在下面的示例中,我们将使用内置方法 add_trace 显示包含组图例的两个子图的图形。

import plotly.graph_objects as go
from plotly.subplots import make_subplots
# Create subplots with the shared legend
fig = make_subplots(rows=2, cols=2, subplot_titles=("Plot 1", "Plot 2"))
# Add traces to subplots
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6], name="Trace 1"), row=1, col=1)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[14, 13, 15], name="Trace 2"), row=1, col=2)
# Update layout for legend grouping
fig.update_layout(width=800, height=600)
# Show the plot
fig.show()

输出

Python 如何使用Plotly设置多个子图和分组图例

示例

在以下示例中,使用make_subplots,此代码生成一个2×2的子图网格,并将两个散点图添加到网格中。布局被修改以自定义图例组和显示图形。

import plotly.graph_objects as go
from plotly.subplots import make_subplots
# Create subplots with the shared legend
fig = make_subplots(rows=2, cols=2, subplot_titles=("Plot 1", "Plot 2"))
# Add traces to subplots
fig.add_trace(go.Scatter(x=[1, 2, 1, 3, 5], y=[4, 5, 2, 3, 6], name="Reliance"), row=1, col=1)
fig.add_trace(go.Scatter(x=[3, 2, 6], y=[13, 14, 15], name="TATA"), row=1, col=2)
# Update layout for customized legend grouping
fig.update_layout(width=800, height=600)
# Show the plot
fig.show()

输出

Python 如何使用Plotly设置多个子图和分组图例

示例

在下面的示例中,我们将展示四个不同子图的图形,这些图形包含指向数据、子图标题和图例,分别在内置函数add_trace and update_layout中使用。这将帮助我们构建具有图例名称的多个子图。

import plotly.graph_objects as go
from plotly.subplots import make_subplots
# Create subplots with the shared legend
fig = make_subplots(rows=2, cols=2, subplot_titles=("Plot 1", "Plot 2", "Plot 3", "Plot 4"))
# Add traces to subplots
fig.add_trace(go.Scatter(x=[1, 4, 1, 3, 5, 2], y=[4, 5, 6, 5, 4, 2], name="Trace 1"), row=1, col=1)
fig.add_trace(go.Scatter(x=[1, 2, 3, 4, 3, 5], y=[7, 8, 9, 3, 2, 6], name="Trace 2"), row=1, col=2)
fig.add_trace(go.Scatter(x=[3, 5, 6, 4, 5, 6], y=[1, 3, 5, 4, 2, 3], name="Trace 3"), row=2, col=1)
fig.add_trace(go.Scatter(x=[1, 2, 3, 2, 6, 4], y=[3, 4, 5, 6, 2, 1], name="Trace 4"), row=2, col=2)
# Update layout for customized legend grouping
fig.update_layout(legend=dict(tracegroupgap=70))
# Show the plot
fig.show()

输出

Python 如何使用Plotly设置多个子图和分组图例

结论

我们讨论了使用Plotly模块绘制多个子图和组图例的三种不同方式。这使我们能够轻松比较数据,并且数据科学家可以创建详细的可视化图表,提高对复杂数据集的理解和沟通。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程