2016-11-07 9 views
2

プロットのためにセカンダリY軸をPythonでセットアップする方法に関するガイドラインはありますか?2つのy軸を持つサブプロット(それぞれ) - plotlyとpython/pandas

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軸の割り当ては動作しません:次のように 私は、反復ループを通る軸のスタイルをassingingのですか?

私のサブプロットは、たとえば、20個のサブプロットの合計4行5列が含まれている場合、私は意味、plotly奇数と偶数番号を受信する必要があることを前提としなければならないのです: yaxis1yaxis2plot1

用.... plot20

+0

これ以外の方法はありますか? – Andreuccio

答えて

0

ため

yaxis39yaxis40を命名規則は、Y、Y2、Y3 ... Y40で、あなたは、トレースのdictで軸を参照すること。

だからあなたの痕跡が

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

私の質問ははっきりしていません。私がお勧めしたことをすれば、各サブプロットごとに1つのy軸が得られ、定義された2つのy軸(1つは[0,5]、もう1つは[0,20]各プロットの同じ時間 https://plot.ly/~andrea.botti/627.embed – Andreuccio

+0

私はサブプロット – Axis

0

ない正確な答え...のようにする必要がありますが、私はそれは私がパンダやカフスボタンを使用したい...

を助けるかもしれないと思いました。セカンダリのy軸を使用してグラフ上の1つのデータフレーム(df)から2組のデータをプロットする方法の例を次に示します。この例では、各軸からのデータがさまざまな形式で表示されます(散布図と棒グラフ)。データはあらかじめ列に配置されています。

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']) 
+0

でこのコードを動作させることはできないと思います。そのためには 本当に便利です。 残念ながら、私のメインそれぞれ2つのy軸を持つサブプロットを生成する方法に関する質問 – Andreuccio

1

これは可能ですが、特に直感的ではありません。この例では、プロットの2x2サブプロットを作成し、2番目のy軸をプロットの2,2ポジションに追加します。

サブプロットを作成すると、各サブプロットの左側にy軸、「y1」、「y2」、「y3」、「y4」が割り当てられます。セカンダリのy軸には、0122,を使用して、 "y1"、 "y2"、 "y3"、 "y4"に対応する新しい軸 "y5"、 "y6"、 "y7"、 "y8"を作成する必要があります。したがって、右下のサブプロットは軸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') 
関連する問題