问题描述
我有一个简单的绘图,其中包含两个迹线和两个yaxis规范.当我使用newPlot
同时绘制它们时,得到了期望的结果.一次绘制迹线(和布局)时,会得到不同的结果,这不是我想要的.
I have a simple plot with two traces and two yaxis specs. When I plot them at the same time using newPlot
I get the desired result. When I plot the traces (and layouts) one at a time, I get a different result, which is not what I want.
使用newPlot
使用addTraces
/relayout
https://jsfiddle.net/abalter/eatszatj/
JavaScript:
Javascript:
Plotly.newPlot("graph",[{}],{title: "Plot"});
trace1 = {y: [1,2,1]};
trace2 = {
y: [0,0,0],
yaxis: "y2",
mode: "markers",
marker: {symbol: 6, size: 12}
};
yaxis1 = {domain: [0, 0.8], title: "one"};
yaxis2 = {
domain: [0.85, 1.0],
showgrid: false,
showticklabels: false,
title: "two",
zeroline: false
};
Plotly.addTraces('graph', [trace1]);
Plotly.relayout('graph', {yaxis1});
Plotly.addTraces('graph', [trace2]);
Plotly.relayout('graph', {yaxis2});
//Plotly.newPlot('graph',[trace1, trace2],{yaxis1, yaxis2});
推荐答案
原来,解决方案是您需要在addTraces
之前调用relayout
.埃蒂安(etpinard)为另一个问题找到了这个解决方案.
The solution turned out to be that you need to call relayout
before addTraces
. Ettiene (etpinard) found me this solution for a different issue.
http://community. plot.ly/t/no-2nd-y-axis-with-addtraces-and-relayout/963
它也解决了这个问题.
Plotly.newPlot("graph",[],{title: "Plot"});
trace1 = {y: [1,2,1]};
trace2 = {
y: [0,0,0],
yaxis: "y2",
mode: "markers",
marker: {symbol: 6, size: 12}
};
yaxis1 = {domain: [0, 0.8], title: "one"};
yaxis2 = {
domain: [0.85, 1.0],
showgrid: false,
showticklabels: false,
title: "two",
zeroline: false
};
Plotly.relayout('graph', {yaxis1});
Plotly.addTraces('graph', [trace1]);
Plotly.relayout('graph', {yaxis2});
Plotly.addTraces('graph', [trace2]);
//Plotly.newPlot('graph',[trace1, trace2],{yaxis1, yaxis2});
这篇关于绘图addTraces/relayout给出与newPlot不同的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!