javascript - 在plotly中创建散点图,无需为数据声明多个变量

标签 javascript plot plotly scatter-plot plotly.js

以下记录是我想使用plotly库的散点图绘制的数据集的一小部分样本。

id  value   condition
1   0.00167736464652281 CM
1   0.00409236292494868 ECM
1   0.00402114732563961 SAX
2   0.0136247916329259  SAX
2   0.0151036287262202  ECM
2   0.0115020440436599  CM
3   0.0115571286240125  CM
3   0.0155058764871028  ECM
3   0.0162593141280405  SAX
4   0.0162953858863326  SAX
4   0.0172050279098291  ECM
4   0.0140566233578565  CM
5   0.0141510897863713  CM
5   0.0177908403313223  ECM
5   0.0181831372346949  SAX

根据标准方式(使用此 example )我应该将值拆分为 3 个变量 - 每个条件一个变量(例如 var CM、var ECM、var SAX),如下所示:

var CM = {
  x: [1, 2, 3, 4, 5],
  y: [0.00167736464652281, 0.0115020440436599, 0.0115571286240125, 0.0140566233578565, 0.0141510897863713],
  mode: 'markers',
  type: 'scatter'
};

有没有更智能的方法来使用样本数据的结构来创建散点图?

最佳答案

假设您的数据位于对象数组中,您可以使用以下方法。

  • 迭代一组条件

    var conditions = new Set(data.map(a => a.condition));
    conditions.forEach(function(condition) {
        var newArray = data.filter(function(el) {
            return el.condition == condition;
        });
    })
    
  • id映射到x,将value映射到y

    traces.push({
        x: newArray.map(a => a.id),
        y: newArray.map(a => a.value),
        name: condition,
        mode: 'markers',
        type: 'scatter'
    })
    

data = [];
data.push({
  'id': 1,
  'value': 0.00167736464652281,
  'condition': 'CM'
});
data.push({
  'id': 1,
  'value': 0.00409236292494868,
  'condition': 'ECM'
});
data.push({
  'id': 1,
  'value': 0.00402114732563961,
  'condition': 'SAX'
});
data.push({
  'id': 2,
  'value': 0.0136247916329259,
  'condition': 'SAX'
});
data.push({
  'id': 2,
  'value': 0.0151036287262202,
  'condition': 'ECM'
});
data.push({
  'id': 2,
  'value': 0.0115020440436599,
  'condition': 'CM'
});
data.push({
  'id': 3,
  'value': 0.0115571286240125,
  'condition': 'CM'
});
data.push({
  'id': 3,
  'value': 0.0155058764871028,
  'condition': 'ECM'
});
data.push({
  'id': 3,
  'value': 0.0162593141280405,
  'condition': 'SAX'
});
data.push({
  'id': 4,
  'value': 0.0162953858863326,
  'condition': 'SAX'
});
data.push({
  'id': 4,
  'value': 0.0172050279098291,
  'condition': 'ECM'
});
data.push({
  'id': 4,
  'value': 0.0140566233578565,
  'condition': 'CM'
});
data.push({
  'id': 5,
  'value': 0.0141510897863713,
  'condition': 'CM'
});
data.push({
  'id': 5,
  'value': 0.0177908403313223,
  'condition': 'ECM'
});
data.push({
  'id': 5,
  'value': 0.0181831372346949,
  'condition': 'SAX'
});

var conditions = new Set(data.map(a => a.condition));
traces = [];
conditions.forEach(function(condition) {
  var newArray = data.filter(function(el) {
    return el.condition == condition;
  });
  traces.push({
    x: newArray.map(a => a.id),
    y: newArray.map(a => a.value),
    name: condition,
    mode: 'markers',
    type: 'scatter'
  })
})
Plotly.plot('myPlot', traces);
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id="myPlot"></div>

关于javascript - 在plotly中创建散点图,无需为数据声明多个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50469718/

相关文章:

javascript - 从文本字段获取值并将其发布到 JavaScript 变量中

python - 删除 matplotlib 文本绘图边框

plot - 如何绘制高斯函数图?

javascript - 是否可以使用plotly.js 绘制饼图?

python - Plotly express 不在 jupyter lab 中渲染

python - Plotly-Dash:如何在不更改图形布局的情况下仅更新图形数据?

javascript - 如何使用 jQuery 引用我自己的自定义属性

javascript - 如何使用 Selenium 检查 javascript 应用程序中的内存泄漏?

javascript - 如何不允许最后选中1个复选框

r - 在ggplot中绘制经验和拟合半变异函数