Plotly:如何更改跟踪顺序,或在 plotly 中切换轴的边? [英] Plotly: How to change the trace order, or switch the sides of the axes in plotly?

查看:70
本文介绍了Plotly:如何更改跟踪顺序,或在 plotly 中切换轴的边?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让线条显示在栏上.似乎任何具有 secondary_y=True 的跟踪都将绘制在 secondary_y=False 的上面.

I am trying to get the line to display over the bar. It seems that whatever trace has secondary_y=True will be drawn on top of the one where secondary_y=False.

那很好,但是对于这个特定的数据集,条形轴应该在右边,否则这个图会让人困惑.线是在 1-3 范围内的那条线,而条是在 0-35k 范围内的那条线.

That's fine, but for this particular data set, the bar axis should be on the right, because otherwise this graph is confusing. The line is the one that is in the 1-3 range, and the bar is the one that is in the 0-35k range.

换句话说,它应该看起来像这样,但 y 轴已切换.有什么方法可以切换轴,或控制绘制轨迹的顺序,以便我可以强制线条位于条形图的顶部?

In other words, it should look just like this, but with y-axes switched. Is there some way to switch the axes, or control the order in which it draws the traces, so that I can force the line to be on top of the bars?

import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from plotly.offline import init_notebook_mode,  plot
init_notebook_mode()

rdf = pd.read_csv('us_covid_data_latest.csv', dtype={'fips':str})
incidence = pd.pivot_table(rdf, values='cases', index = 'date', aggfunc=np.sum)
incidence['actual_inc'] = incidence['cases'].diff()

def tail_plot_plotly(tail):

    fig = make_subplots(specs=[[{"secondary_y": True}]])

    fig.add_trace(
        go.Bar(
            x= incidence['date'].tail(tail),
            y= incidence['actual_inc'].tail(tail)
            ),
        secondary_y = False
        ) 


    fig.add_trace(
        go.Scatter(
            x= incidence['date'].tail(tail),
            y= incidence['R_t'].tail(tail)
            ),
        secondary_y = True
        )

    plot(fig)

tail_plot_plotly(50)

推荐答案

在没有数据集样本的情况下提供完整的解决方案并不容易,但我仍然认为我已经弄清楚了.我现在有点赶时间,所以我会简短:

It's not easy providing a complete solution without a sample of your dataset, but I still think I've figured it out. I'm in a bit of a hurry right now, so I'll make it short:

条是大数字,线是小数字.开箱即用,fig = make_subplots(specs=[[{secondary_y": True}]]) 将提供:

Bars are big numbers, lines are small numbers. Out of the box, a fig = make_subplots(specs=[[{"secondary_y": True}]]) would provide this:

顶部的线迹 = 好.左侧的条形编号 = 差.

更改 yoy 将不同轨迹应用于图形的顺序无济于事.但是您可以自由指定您希望主 y 轴和次要 y 轴显示在图的哪一侧:

Changing the order of which yoy apply the different traces to the figure won't help. But you can freely specify on which side of the plot you'd like your primary and secondary y axis to appear like this:

fig.update_layout(dict(yaxis2={'anchor': 'x', 'overlaying': 'y', 'side': 'left'},
                  yaxis={'anchor': 'x', 'domain': [0.0, 1.0], 'side':'right'}))

将其添加到组合中,您将得到:

Add that to the mix, and you'll get:

顶部的线迹 = 好.右侧的条形数字 = 好.

带有数据样本的完整代码:

# imports
import plotly.graph_objects as go
import numpy as np
from plotly.subplots import make_subplots

# set figure twith multiple y axes
fig = make_subplots(specs=[[{"secondary_y": True}]])

# blue line with numbers from 1 to 3
fig.add_trace(
    go.Scatter(x=[0, 1, 2, 3, 4, 5],
               y=[1.5, 1.0, 1.3, 2.7, 1.8, 2.9]),secondary_y=True)

# red bars with big numbers
fig.add_trace(
    go.Bar(x=[0, 1, 2, 3, 4, 5],
           y=[np.nan, np.nan, np.nan, 100000, 20000, 250000]))

# update layout to put axes and values in the desired positions
fig.update_layout(dict(yaxis2={'anchor': 'x', 'overlaying': 'y', 'side': 'left'},
                  yaxis={'anchor': 'x', 'domain': [0.0, 1.0], 'side':'right'}))

fig.show()

这篇关于Plotly:如何更改跟踪顺序,或在 plotly 中切换轴的边?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆