首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有办法在make_subplot的规范中输入动态数量的"'secondary_y':True“?

有没有办法在make_subplot的规范中输入动态数量的"'secondary_y':True“?
EN

Stack Overflow用户
提问于 2020-06-17 19:36:53
回答 1查看 188关注 0票数 0

主要问题:我正在尝试使用一个"regions“数组在plotly-python中动态生成子图。

有没有办法使用我的区域数组来处理下面的代码部分?在代码片段中,我手动放入了4行{ 'secondary_y':True } ,)

代码语言:javascript
复制
fig = make_subplots(
    rows=1 ,
    cols=len( arr___regions ) ,
    subplot_titles=arr___regions ,
    shared_yaxes=True ,
    specs = [
        [
            { 'secondary_y':True } ,
            { 'secondary_y':True } ,
            { 'secondary_y':True } ,
            { 'secondary_y':True } ,
            ]
        ]
    )

尝试了几次搜索,但没有找到任何东西;不确定im是否使用了正确的搜索词。

或者也许我遗漏了一些关于python的基础知识?

2第二个问题:shared_yaxes=True共享主要Y轴,但第二个Y轴仍然显示在所有子图上。plotly有没有关于共享二次Y轴的任何东西?

两个问题的完整代码:

代码语言:javascript
复制
import pandas as pd
import plotly.io as pio # https://plotly.com/python/templates/
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from io import StringIO

data_source = StringIO('''region|period|sales|quantity
Central|2014-01|1,539.91|62
Central|2014-02|1,233.17|69
Central|2014-03|5,827.60|123
Central|2014-04|3,712.34|80
Central|2014-05|4,048.51|121
Central|2014-06|9,646.30|158
East|2014-01|436.17|33
East|2014-02|199.78|15
East|2014-03|5,943.39|140
East|2014-04|3,054.91|120
East|2014-05|7,250.10|135
East|2014-06|10,759.16|160
South|2014-01|9,322.09|125
South|2014-02|2,028.99|28
South|2014-03|32,911.12|172
South|2014-04|12,184.61|157
South|2014-05|5,779.24|69
South|2014-06|4,560.25|65
West|2014-01|2,938.72|64
West|2014-02|1,057.96|47
West|2014-03|11,008.90|150
West|2014-04|9,343.49|179
West|2014-05|6,570.44|141
West|2014-06|9,629.42|138
''')

odf = pd.read_csv( data_source , delimiter='|' , thousands=',' , )   # "original dataframe"

pio.templates.default = 'presentation'

arr___regions = pd.unique( odf['region'] )
arr___regions.sort()



fig = make_subplots(
    rows=1 ,
    cols=len( arr___regions ) ,
    subplot_titles=arr___regions ,
    shared_yaxes=True ,
    specs = [
        [
            { 'secondary_y':True } ,
            { 'secondary_y':True } ,
            { 'secondary_y':True } ,
            { 'secondary_y':True } ,
            ]
        ]
    )



sdf = {}   # "sub dataframe"

for idx , region in enumerate( arr___regions , start=0 ):
    sdf[region] = odf[ odf['region'] == region ]

    fig.add_trace(
        go.Bar(
            name='sales',
            x=sdf[region]['period'],
            y=sdf[region]['sales'],
            ) ,
        row=1,col=(idx+1),
        )

    fig.add_trace(
        go.Scatter(
            name='quantity',
            x=sdf[region]['period'],
            y=sdf[region]['quantity'],
            mode='lines+markers',
            line=dict( shape='spline', ) ,
            ) ,
        row=1,col=(idx+1),
        secondary_y=True,
        )



fig.update_xaxes( tickangle=270 , type='category' , )

y_11 = --000
y_12 = 40000
y_21 = y_11*0.010
y_22 = y_12*0.010

fig.update_yaxes(
    title_text='sales',
    range = [ y_11,y_12 ] ,
    )

fig.update_yaxes(
    title_text='quantity',
    secondary_y=True,
    range = [ y_21,y_22 ] ,
    )

fig.update_layout(
    legend_orientation='h' ,
    font = dict( color='#000000' , family='tahoma' , size=10 , ) ,
    margin = dict( l=140,r=140,b=140,t=140,pad=12, ) ,
    )

fig.show()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-17 21:22:25

关于你的主要问题,你可以使用:

代码语言:javascript
复制
specs = [list([{ 'secondary_y':True }]*len( arr___regions ))]

len( arr___regions )指定适当的大小等级库的步骤

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62428052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档