带有两个y轴(每个)的子图 - plotly和python / pandas

时间:2016-11-07 17:27:20

标签: python pandas plotly subplot yaxis

是否有关于如何在python中设置辅助Y轴的指导方针? 我通过迭代循环来分配轴样式,如下所示:

all_plots = ['plot1','plot2'...'plot20']
fig = tools.make_subplots(rows=nrow, cols=ncol, shared_xaxes=False, shared_yaxes=False, subplot_titles=all_plots)
for i in all_plots:
    fig['layout']['yaxis'+str(j)].update()

y轴的分配如何工作?

如果我的子图包括4行5列,总共20个子图,我是否必须假设情节需要接收奇数和偶数,这意味着: yaxis1

yaxis2plot1

...

对于yaxis39

yaxis40plot20

3 个答案:

答案 0 :(得分:1)

不是一个确切的答案,但我认为这可能会有所帮助...

我喜欢用熊猫和袖扣。以下是如何使用辅助y轴在图形上从一个数据框(df)绘制两组数据的示例。在此示例中,每个轴的数据以不同的格式显示(散点图和条形图)。数据预先排列成列。

import pandas as pd
import cufflinks as cf
from plotly.offline import download_plotlyjs, init_notebook_mode,plot,iplot    

fig1 = df.iplot(kind='scatter', mode='lines+markers', x=['col1', 'col2'],
                y=['col3', 'col4',],
                asFigure=True)
fig2 = df.iplot(kind='bar', x=['col1', 'col2'],
                  y=['col3', 'col4', ],
                  secondary_y=['col5','col6'],asFigure=True)
fig2['data'].extend(fig1['data'])

答案 1 :(得分:1)

这样做有可能,但不是特别直观。以这个例子为例,我创建了一个2x2子图,并在第2,2位的图中添加了一个辅助y轴。

创建子图时,会为它们分配y轴:" y1"," y2"," y3"," y4"在每个子图的左侧。对于辅助y轴,您需要使用fig['layout'].update来创建新轴" y5"," y6"," y7"," Y8"对应于" y1"," y2"," y3"," y4"。因此,右下方的子图将具有轴y4(右)和y8(左)。在下面的示例中,我仅为最后一个绘图创建了辅助y轴,但将其扩展到更多/所有子绘图非常简单。

重要的是要注意,创建辅助轴并在trace5中分配它并不会自动将其放在正确的轴上。您仍然需要使用fig['data'][4].update(yaxis='y'+str(8))手动指定它以相对于左轴绘制它。

fig = tools.make_subplots(rows=2, cols=2,subplot_titles=('Air Temperature', 'Photon Flux Density',
                                                         'Ground Temps','Water Table & Precip'))


fig['layout']['xaxis1'].update( range=[174, 256])
fig['layout']['xaxis3'].update(title='Day of Year', range=[174, 256])
fig['layout']['yaxis1'].update(title='Degrees C',range=[-5,30])
fig['layout']['yaxis2'].update(title='mmol m<sup>-2</sup> m<sup>-d</sup>', range=[0, 35])
fig['layout']['yaxis3'].update(title='Ground Temps', range=[0, 11])
fig['layout']['yaxis4'].update(title='depth cm', range=[-20, 0])
fig['layout']['yaxis8'].update(title='rainfall cm', range=[0, 1.6])
fig['layout'].update(showlegend=False, title='Climate Conditions')



# In this example, I am only doing it for the last subplot, but if you wanted to do if for all, 
# Just change to range(1,5)

for k in range(4,5):  
    fig['layout'].update({'yaxis{}'.format(k+4): dict(anchor='x'+str(k),
                                                          overlaying='y'+str(k),
                                                          side='right',
                                                         )
                            })

trace1 = go.Scatter(
        y=Daily['AirTC_Avg'],
        x=Daily.index,
        marker = dict(
        size = 10,
        color = 'rgba(160, 0, 0, .8)',),
        error_y=dict(
            type='data',
            array=Daily_Max['AirTC_Avg']-Daily_Min['AirTC_Avg'],
            visible=True,
        color = 'rgba(100, 0, 0, .5)',
        ),
    name = 'Air Temp'
    )

trace2 = go.Bar(
        y=Daily['PPFD']/1000,
        x=Daily.index,
        name='Photon Flux',
        marker=dict(
            color='rgb(180, 180, 0)'
        ),

    yaxis='y2',
)

trace3 = go.Scatter(
        y=Daily['Temp_2_5_1'],
        x=Daily.index,
        name='Soil Temp',
        marker=dict(
            color='rgb(180, 0, 0)'
        ),

    yaxis='y3',
)


trace4 = go.Scatter(
        y=Daily['Table_1']*100,
        x=Daily.index,
        name='Water Table',
        marker=dict(
            color='rgb(0, 0, 180)'
        ),

    yaxis='y4',
)

trace5 = go.Bar(
        y=Daily['Rain']/10,
        x=Daily.index,
        name='Rain',
        marker=dict(
            color='rgb(0, 100, 180)'
        ),

    yaxis='y8',
)

fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 2)
fig.append_trace(trace3, 2, 1)
fig.append_trace(trace4, 2, 2)
fig.append_trace(trace5, 2, 2)


## This part is important!!! you have to manually assing the data to the axis even 
# though you do it when defining trace 5
fig['data'][4].update(yaxis='y'+str(8))
plot(fig, filename='FI_Climate')

答案 2 :(得分:0)

命名约定是y,y2,y3 ... y40,并且您在跟踪字典中引用了轴。

所以你的踪迹应该像......

trace0 = dict(
    x = xvals,
    y = yvals,
    yaxis = 'y'
)
trace1 = dict(
     x = x2vals,
     y = y2vals,
     yaxis = 'y2'
)
....
trace40 = dict(
     x = x40vals,
     y = y40vals,
     yaxis = 'y40'
)