python绘制曲线y=2x+5_有两个y轴(每个)的子图 - plotly和python/pandas

可以这样做,但它不是特别直观。以此示例为例,我创建了一个绘图2x2子绘图,并将第二个y轴添加到位置2,2的绘图。

当您创建子图时,它们在每个子图的左侧分配y轴:“y1”,“y2”,“y3”,“y4”。对于第二个y轴,需要使用fig['layout'].update创建对应于“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-2 m-d', 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')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值