Plotly Python:在具有多个Y轴的分组条形图中对齐X轴

时间:2018-06-15 18:29:37

标签: python bar-chart plotly

我有一个带有两个y轴的分组条形图,每个轴都有不同的比例。我试图对齐两组的x轴(y = 0)。我发现很少有链接link1link2,其中设置 rangemode ='0'应该有效,但是我的数据由负值组成,因为我猜想将rangemode设置为零isn工作。

这是我的代码:

import plotly.offline as plt
import plotly.graph_objs as go
traces = [go.Bar(x=[1,2,3,4], y=[-1,2,-3,4], name='y actual'), 
          go.Bar(x=[1], y=[0], name='y dummy', hoverinfo='none', showlegend=False), 
          go.Bar(x=[1],y=[0],yaxis='y2', name='y2 dummy', hoverinfo='none', showlegend=False),
          go.Bar(x=[1,2,3,4], y=[22, 2, 13, 25], yaxis='y2', name='y2 actual')]
layout = go.Layout(barmode='group',
                   yaxis=dict(title='y actual', rangemode="tozero", anchor='x', overlaying='y2'),
                   yaxis2=dict(title='y2 actual', side='right', rangemode = "tozero", anchor='x'))
fig = go.Figure(data=traces, layout=layout)
plt.iplot(fig)

上面代码生成的图: enter image description here

我该如何解决这个问题?

注意:您可以在代码中看到两条虚拟迹线。我介绍了它们,以便两条“y actual”和“y2 actual”的曲线不会叠加在一起。有关我为什么要结帐此link

的详细信息

1 个答案:

答案 0 :(得分:2)

可能的解决方法:

将两个图形的range元素设置为彼此成比例,然后轴将对齐。基本上,你的问题是一个轴必须显示负数而另一个不显示。通过告诉y2显示负数,我们可以获得目标。

from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go

init_notebook_mode(connected=True)

traces = [
    go.Bar(
        x=[1, 2, 3, 4], 
        y=[-1, 2, -3, 4], 
        name='y actual'
    ), 
    go.Bar(
        x=[1], 
        y=[0], 
        name='y dummy', 
        hoverinfo='none', 
        showlegend=False
    ), 
    go.Bar(
        x=[1],
        y=[0],
        yaxis='y2', 
        name='y2 dummy', 
        hoverinfo='none', 
        showlegend=False
    ),
   go.Bar(
       x=[1, 2, 3, 4], 
       y=[22, 2, 13, 25], 
       yaxis='y2', 
       name='y2 actual'
   )
]

# layout
layout = go.Layout(
    barmode='group',
    yaxis=dict(
        title='y actual', 
        rangemode="tozero", 
        #anchor='x', 
        overlaying='y2',
        side="left",
        range = [-4, 10]
    ),
    yaxis2=dict(
        title='y2 actual', 
        side='right', 
        rangemode = "tozero",
        #anchor='x',
        range = [-12, 30]
    )
)

# make fig
fig = go.Figure(data=traces, layout=layout)
iplot(fig)

enter image description here

这可能是令人讨厌的必须保持它们的比例,但它将确保它们的对齐。

为了帮助自动化该过程,您可以使用以下函数生成两个彼此成比例的范围。

def make_proportional_intervals(a, b):
    """
    Given two list like objects, compute two proprotionally sized ranges.
    This function assumes the max value in both lists is positive and non-zero
    """
    min_a, min_b = min(a), min(b)
    max_a, max_b = max(a), max(b)
    if (min_a >=0) & (min_b >= 0):
        # provide a 20% cushion to the scale
        return [0, round(1.2*max_a)], [0, round(1.2*max_b)]
    else:
        if (min_a < min_b) & (max_a < max_b):
            n = -(-max_b // max_a)
            # n = math.ceil(max_b / max_a), if you cannot assume ints.
            return [min_a, max_a], [n*min_a, n*max_a]

        elif (min_b < min_a) & (max_b < max_a):
            n = -(-max_a // max_b)
            # n = math.ceil(max_b / max_a), if you cannot assume ints.
            return [n*min_b, n*max_b], [min_b, max_b]

        elif (min_b < min_a) & (max_a < max_b):
            n = max( -(-max_b // max_a), -(min_b // min_a) )
            return [min_b / n, max_b / n], [min_b, max_b]

        elif (min_a < min_b) & (max_b < max_a):
            n = max( -(-max_a // max_b), -(min_a // min_b) )
            return [min_a, max_a], [min_a / n, max_a / n]
        elif (min_a == min_b):
            m = max(max_a, max_b)
            return [min_a, m], [min_b,  m]
        elif max_a == max_b:
            m = min(min_a, min_b)
            return [m, max_a], [m, max_b]

此函数假设您的值为整数,但如果不是,您可以import math并使用math.ceil()而不是整数除法。我避免添加任何更多的导入。如果你想看到这个代码在运行中我在jupyter笔记本中创建了一个例子,你可以多次运行以查看它如何处理不同的数组。

from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
import numpy as np

def make_proportional_intervals(a, b):
    """
    Given two list like objects, compute two proprotionally sized ranges.
    This function assumes the max value in both lists is positive and non-zero
    """
    min_a, min_b = min(a), min(b)
    max_a, max_b = max(a), max(b)
    if (min_a >=0) & (min_b >= 0):
        # provide a 20% cushion to the scale
        return [0, round(1.2*max_a)], [0, round(1.2*max_b)]
    else:
        if (min_a < min_b) & (max_a < max_b):
            n = -(-max_b // max_a)
            # n = math.ceil(max_b / max_a), if you cannot assume ints.
            return [min_a, max_a], [n*min_a, n*max_a]

        elif (min_b < min_a) & (max_b < max_a):
            n = -(-max_a // max_b)
            # n = math.ceil(max_b / max_a), if you cannot assume ints.
            return [n*min_b, n*max_b], [min_b, max_b]

        elif (min_b < min_a) & (max_a < max_b):
            n = max( -(-max_b // max_a), -(min_b // min_a) )
            return [min_b / n, max_b / n], [min_b, max_b]

        elif (min_a < min_b) & (max_b < max_a):
            n = max( -(-max_a // max_b), -(min_a // min_b) )
            return [min_a, max_a], [min_a / n, max_a / n]
        elif (min_a == min_b):
            m = max(max_a, max_b)
            return [min_a, m], [min_b,  m]
        elif max_a == max_b:
            m = min(min_a, min_b)
            return [m, max_a], [m, max_b]

init_notebook_mode(connected=True)

y0 = np.random.randint(-5, 35, 6)
y1 = np.random.randint(-7, 28, 6)

print(y0, y1)
range0, range1 = make_proportional_intervals(y0, y1)

traces = [
    go.Bar(
        x=[1, 2, 3, 4, 5, 6], 
        y=y0, 
        name='y actual'
    ), 
    go.Bar(
        x=[1], 
        y=[0], 
        name='y dummy', 
        hoverinfo='none', 
        showlegend=False
    ), 
    go.Bar(
        x=[1],
        y=[0],
        yaxis='y2', 
        name='y2 dummy', 
        hoverinfo='none', 
        showlegend=False
    ),
   go.Bar(
       x=[1, 2, 3, 4, 5, 6], 
       y=y1, 
       yaxis='y2', 
       name='y2 actual'
   )
]

# layout
layout = go.Layout(
    barmode='group',
    yaxis=dict(
        title='y actual', 
        rangemode="tozero", 
        #anchor='x', 
        overlaying='y2',
        side="left",
        range = range0
    ),
    yaxis2=dict(
        title='y2 actual', 
        side='right', 
        rangemode = "tozero",
        #anchor='x',
        range = range1
    )
)

fig = go.Figure(data=traces, layout=layout)
iplot(fig)

同样,这只是一个解决方案,因为你有负数并且不能将rangemode = "tozero"用作场景here。也许开发人员将来会向rangemode添加一些内容来纠正这个问题。