我有一个自动缩放 y 轴的函数。它接收图形和重新布局数据对象,然后输出符合我预先确定的某些标准的 y 轴。我想在 python 上的 dash 中添加一个新的 modbar 按钮,该按钮会在我点击它时触发该函数并更新图形。那可能吗?
一个简单的示例是能够调整图表大小,但仅重置 y 轴。 modbar 中开箱即用的调整大小按钮可重置 y 轴并设置 autosize = true。用于此问题的示例自定义 modbar 按钮将执行第一个功能,但不会执行第二个功能。
添加/删除/自定义模式栏按钮非常简单,只需定义图形即可config相应地使用modeBarButtonsToAdd
and/or modeBarButtonsToRemove
选项(已记录here).
请注意,这些配置选项在 dash、plotly.py 和plotly.js 中是一致的。
The .show()
您用来显示数字的方法也接受config
范围。
您可以通过传递一个来设置您的图形的配置选项
此参数的字典,其中包含您想要的选项
放。
使用 Dash,可以将相同的配置字典传递给dcc.Graph成分 :
dcc.Graph(figure=go.Figure(), config=config)
这是一个 Plotly.js 实时示例,它删除了预定义的“autoscale2d”按钮,并添加了一个带有触发的特定处理程序的自定义按钮Plotly.relayout() :
const data = [{
mode: 'lines',
x: [-2, 0, 2],
y: [2, -2, 3],
line: {shape: 'spline'}
}]
const layout = { title: 'Mode Bar Custom Button' }
const config = {
displayModeBar: true,
modeBarButtonsToRemove: ['autoscale2d'],
modeBarButtonsToAdd: [{
name: 'autoscale2d-custom',
title: 'Autoscale (custom)',
icon: Plotly.Icons.autoscale,
click: function(gd) {
Plotly.relayout(gd, {
'xaxis.range': [-3, 3],
'yaxis.range': [-3, 3]
});
}
}]
}
Plotly.newPlot('graph', data, layout, config)
<script src="https://cdn.plot.ly/plotly-2.16.2.min.js"></script>
<div id="graph"></div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)