填充仅填充我的绘图图表的一部分,而不是全部

问题描述 投票:0回答:1

我在 JS 上使用plotly 创建线性回归及其置信区间的交互式图。数据是正确的,除了间隔中的填充函数之外,一切都显示正常。尽管线条已完全绘制,但仅填充了间隔的一部分,而不是它们之间的所有空间。这是我的间隔代码:

let lower_trace = [];
let top_trace = [];

data.forEach(obj => { 
    lower_trace.push(obj['lower'])
    top_trace.push(obj['upper'])
});

let trace_low = {
    x: x_values,
    y: lower_trace,
    mode: 'lines',
    fill: 'tonexty',
    type: 'scatter',
    name : "low",
    line: {
        color:  '#d4e6f1'
    }
    };

let trace_upper = {
    x: x_values,
    y: top_trace,
    mode: 'lines',
    fill: '',
    type: 'scatter',
    name : "high",
    line: {
        color:  '#d4e6f1'
        }
};


let traceArray = [];
traceArray.push(trace_upper);
traceArray.push(trace_low);

traceArray.push(fit); //Plots the trending line
dots = Object.values(traces);
    dots.forEach(dot =>{
    traceArray.push(dot);   
}); //Plots the data as scatter

let layout = { 
    font: {size: 12},
    xaxis: {
    showgrid: true,
    title: 'PDI',
    },
    yaxis: {
    showgrid: true,
    title: 'Ro',
    },
    legend:{
    x: 1,
    y: 0.5
    }             
};
    
let config = {responsive: true}  
 
Plotly.newPlot('mySidebare', traceArray, layout, config);

This is the plot I'm currently getting,正如您所看到的,绘制的痕迹比实际填充的空间更长。

我试图得到一个 plot like this,它一直填充(我使用 matplotlib 制作了这个图作为测试,但我需要在我的项目中使用plotly)。

我错过了什么???我快疯了,两个小时前还好好的,我什至不知道我移动或删除了什么导致它停止工作。

javascript plotly
1个回答
0
投票

更新!因此,如果有人遇到这个问题,我在这里找到了解决方案:https://community.plotly.com/t/filled-plot-not-extending-fill-to-y-axis/20749。事实证明,如果数据未排序,填充时绘图会变得混乱,所以我所要做的就是在绘制数据之前对数据进行排序:

x_values.sort();
lower_trace.sort();
top_trace.sort();

现在my graph看起来像我想要的。

© www.soinside.com 2019 - 2024. All rights reserved.