[英]Plotly.js - Plot histogram with frequency
如何使用Plotly.js绘制频率直方图?
Plotly.js提供API来绘制具有一组值但不具有频率值的直方图。
你可以找到更多关于Plotly直方图这里- https://plot.ly/javascript/histograms/
这是一个例子:
如果样本集为{2、3、4、5、2、2、2、3、5}。 Plotly绘制这样说- https://codepen.io/sgsvenkatesh/pen/eEyyMJ
var x = [2, 3, 4, 5, 2, 2, 2, 3, 5];
var trace = {
x: x,
type: 'histogram',
};
var data = [trace];
Plotly.newPlot('myDiv', data);
但这是我的数据
x = [2, 3, 4, 5];
y = [4, 2, 1, 1];
这表示将值0-2重复4次,将值2-3重复2次,依此类推。
如何使用Plotly.js进行绘制?
检查此表示方式。
x = [2, 3, 4, 5]; y = [4, 2, 1, 1]; traces = []; for (var i = 0; i <= x.length - 1; i++) { var yarray = new Array(y[i]).fill(y[i]); var trace = {}; if (i === 0) { trace = { x: yarray, type: 'histogram', name: 0 + " to " + x[i] }; } else if (i === x.length - 1) { trace = { x: yarray, type: 'histogram', name: x[i] + " and above" }; } else { trace = { x: yarray, type: 'histogram', name: x[i - 1] + " to " + x[i] }; } traces.push(trace); } var data = traces; var layout = {barmode: "group"}; // there are three modes that can be set, "overlay", "stack", "group" Plotly.newPlot('myDiv', data, layout);
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script> <div id="myDiv"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.