【问题标题】:Plotly addTraces/relayout giving different output than newPlotPlotly addTraces/relayout 提供与 newPlot 不同的输出
【发布时间】:2016-05-02 02:30:50
【问题描述】:

我有一个带有两条迹线和两个 y 轴规格的简单图。当我使用newPlot 同时绘制它们时,我得到了想要的结果。当我一次绘制一个轨迹(和布局)时,我得到了不同的结果,这不是我想要的。

newPlot

addTraces/relayout

https://jsfiddle.net/abalter/eatszatj/

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});

【问题讨论】:

    标签: javascript plotly


    【解决方案1】:

    结果证明你需要在addTraces之前调用relayout。 Ettiene (etpinard) 为我找到了解决不同问题的解决方案。

    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});
    

    【讨论】:

      【解决方案2】:

      问题在于您的第一个Plotly.newPlot 电话。您正在传递一个空的跟踪对象,该对象默认为不可见的散点跟踪。

      这意味着您的第一个Plotly.addTraces 调用中的跟踪实际上是图形的第二个跟踪(默认以橙色绘制),而您的第二个Plotly.addTraces 调用中的跟踪实际上是图形的第三个跟踪(以绿色绘制默认)。

      因此,通过将第一行更改为

      Plotly.newPlot("graph",[],{title: "Plot"});
      

      你会得到想要的结果图。

      【讨论】:

      • 哦,很有趣……如此微妙的一点。然而,我用那个调整创造了一个新的小提琴,问题仍然存在。jsfiddle.net/abalter/1y4chsu7
      • 我在您更新的小提琴中没有发现问题。你能详细说明一下吗?
      • 抱歉,在我关闭标签之前一定忘记点击更新了。现在看...jsfiddle.net/abalter/eatszatj
      猜你喜欢
      • 2021-02-03
      • 2013-12-05
      • 2016-04-05
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多