📌  相关文章
📜  如何在Python中使用 Plotly Express 在辅助 Y 轴上绘图?

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

如何在Python中使用 Plotly Express 在辅助 Y 轴上绘图?

附加条件 Python Ploty

在本文中,我们将学习如何使用 plotly express 在辅助 y 轴上绘图。

Plotly 数据可视化最具欺骗性的强大功能之一是,当将光标指向出现的点标签时,查看者能够快速分析足够数量的数据信息。它为我们提供了从文档中读取和分析信息的最简单方法。多轴图表用于沿两个或多个轴绘制来自查询的数据点。在这里,我们将讨论在 plotly express 中绘制多个 y 轴的不同方法,以使其更加清晰。

示例 1:两个 Y 轴

在此示例中,我们使用两个数据图,第一个是条形图,另一个是散点图。 Update_layout使用字典和/或关键字参数更新图形布局的属性,我们将在上述每个轴的语法的帮助下定义连续的辅助 yaxis(即yaxis, yaxis2 )。

Python3
import plotly.graph_objects as go
 
fig = go.Figure()
 
fig.add_trace(go.Bar(x=[1, 2, 3], y=[40, 50, 60],
                     name="yaxis1 data", yaxis='y'))
 
fig.add_trace(go.Scatter(x=[2, 3, 4], y=[4, 5, 6],
                      name="yaxis2 data", yaxis="y2"))
 
# Create axis objects
fig.update_layout(xaxis=dict(domain=[0.3, 0.7]),
    #create 1st y axis             
    yaxis=dict(
        title="yaxis title",
        titlefont=dict(color="#1f77b4"),
        tickfont=dict(color="#1f77b4")),
                   
    #create 2nd y axis      
    yaxis2=dict(title="yaxis2 title",overlaying="y",
                side="left",position=0.15))
 
# title
fig.update_layout(
    title_text="Geeksforgeeks - Three y-axes",
    width=800,
)
 
fig.show()


Python3
import plotly.graph_objects as go
 
fig = go.Figure()
 
fig.add_trace(go.Bar(x=[1, 2, 3], y=[4, 5, 6],
                     name="yaxis1 data", yaxis='y'))
 
fig.add_trace(go.Scatter(x=[2, 3, 5], y=[40, 50, 60],
                         name="yaxis2 data", yaxis="y2"))
 
fig.add_trace(go.Bar(x=[4, 5, 6], y=[40000, 50000, 60000],
                     name="yaxis3 data", yaxis="y3"))
 
# Create axis objects
fig.update_layout(xaxis=dict(domain=[0.3, 0.7]),
 
                  # create 1st y axis
                  yaxis=dict(
    title="yaxis title",
    titlefont=dict(color="#1f77b4"),
    tickfont=dict(color="#1f77b4")),
 
    # create 2nd y axis
    yaxis2=dict(title="yaxis2 title", overlaying="y",
                side="left", position=0.15),
 
    # create 3rd y axis
    yaxis3=dict(
        title="yaxis3 title",
        anchor="x", overlaying="y", side="right"))
 
 
# title
fig.update_layout(
    title_text="Geeksforgeeks - Three y-axes",
    width=800,
)
 
fig.show()


Python3
import plotly.graph_objects as go
 
fig = go.Figure()
 
fig.add_trace(go.Bar(x=[1, 2, 3], y=[4, 5, 6],
                     name="yaxis1 data"))
 
fig.add_trace(go.Scatter(x=[2, 3, 4], y=[40, 50, 60],
                         name="yaxis2 data", yaxis="y2"))
 
fig.add_trace(go.Scatter(x=[4, 5, 6],
                         y=[40000, 50000, 60000],
                         name="yaxis3 data", yaxis="y3"))
 
fig.add_trace(go.Bar(
    x=[5, 6, 7], y=[400000, 500000, 600000],
    name="yaxis4 data", yaxis="y4"))
 
 
# Create axis objects
fig.update_layout(
    xaxis=dict(
        domain=[0.3, 0.7]
    ),
    yaxis=dict(
        title="yaxis title", titlefont=dict(color="#1f77b4"),
        tickfont=dict(color="#1f77b4")),
 
    yaxis2=dict(
        title="yaxis2 title",
        titlefont=dict(color="#ff7f0e"),
        tickfont=dict(color="#ff7f0e"),
        anchor="free", overlaying="y",
        side="left", position=0.15),
 
    yaxis3=dict(
        title="yaxis3 title",
        titlefont=dict(color="#d62728"),
        tickfont=dict(color="#d62728"),
        anchor="x", overlaying="y", side="right"),
 
    yaxis4=dict(
        title="yaxis4 title",
        titlefont=dict(color="#9467bd"),
        tickfont=dict(color="#9467bd"),
        anchor="free", overlaying="y",
        side="right", position=0.85)
)
 
# Update layout properties
fig.update_layout(
    title_text="Four y-axes",
    width=800,
)
 
fig.show()


输出:

示例 2:三个 Y 轴

在这个例子中,我们要绘制三个数据图,第一个是条形图,另一个是散点图,最后一个是另一个条形图。使用字典和/或关键字参数更新图形布局的属性。我们将在上述每个轴的语法的帮助下定义连续的辅助 y 轴(即yaxis、yaxis2、yaxis3 ...)

Python3

import plotly.graph_objects as go
 
fig = go.Figure()
 
fig.add_trace(go.Bar(x=[1, 2, 3], y=[4, 5, 6],
                     name="yaxis1 data", yaxis='y'))
 
fig.add_trace(go.Scatter(x=[2, 3, 5], y=[40, 50, 60],
                         name="yaxis2 data", yaxis="y2"))
 
fig.add_trace(go.Bar(x=[4, 5, 6], y=[40000, 50000, 60000],
                     name="yaxis3 data", yaxis="y3"))
 
# Create axis objects
fig.update_layout(xaxis=dict(domain=[0.3, 0.7]),
 
                  # create 1st y axis
                  yaxis=dict(
    title="yaxis title",
    titlefont=dict(color="#1f77b4"),
    tickfont=dict(color="#1f77b4")),
 
    # create 2nd y axis
    yaxis2=dict(title="yaxis2 title", overlaying="y",
                side="left", position=0.15),
 
    # create 3rd y axis
    yaxis3=dict(
        title="yaxis3 title",
        anchor="x", overlaying="y", side="right"))
 
 
# title
fig.update_layout(
    title_text="Geeksforgeeks - Three y-axes",
    width=800,
)
 
fig.show()

输出:

示例 3:四个 Y 轴

在此示例中,我们将采用两个条形图的数据图和两个散点图的图。使用字典和/或关键字参数更新图形布局的属性。我们将在上述每个轴的语法的帮助下定义连续的辅助 y 轴(即yaxis, yaxis2, yaxis3, yaxis4

Python3

import plotly.graph_objects as go
 
fig = go.Figure()
 
fig.add_trace(go.Bar(x=[1, 2, 3], y=[4, 5, 6],
                     name="yaxis1 data"))
 
fig.add_trace(go.Scatter(x=[2, 3, 4], y=[40, 50, 60],
                         name="yaxis2 data", yaxis="y2"))
 
fig.add_trace(go.Scatter(x=[4, 5, 6],
                         y=[40000, 50000, 60000],
                         name="yaxis3 data", yaxis="y3"))
 
fig.add_trace(go.Bar(
    x=[5, 6, 7], y=[400000, 500000, 600000],
    name="yaxis4 data", yaxis="y4"))
 
 
# Create axis objects
fig.update_layout(
    xaxis=dict(
        domain=[0.3, 0.7]
    ),
    yaxis=dict(
        title="yaxis title", titlefont=dict(color="#1f77b4"),
        tickfont=dict(color="#1f77b4")),
 
    yaxis2=dict(
        title="yaxis2 title",
        titlefont=dict(color="#ff7f0e"),
        tickfont=dict(color="#ff7f0e"),
        anchor="free", overlaying="y",
        side="left", position=0.15),
 
    yaxis3=dict(
        title="yaxis3 title",
        titlefont=dict(color="#d62728"),
        tickfont=dict(color="#d62728"),
        anchor="x", overlaying="y", side="right"),
 
    yaxis4=dict(
        title="yaxis4 title",
        titlefont=dict(color="#9467bd"),
        tickfont=dict(color="#9467bd"),
        anchor="free", overlaying="y",
        side="right", position=0.85)
)
 
# Update layout properties
fig.update_layout(
    title_text="Four y-axes",
    width=800,
)
 
fig.show()

输出: