如何使用Plotly.js绘制频率直方图?
Plotly.js提供了用一组值绘制直方图而不是用频率值绘制直方图的API。
您可以在这里找到更多关于实际直方图的信息- https://plot.ly/javascript/histograms/
这里有一个例子:
如果样本集是{ 2,3,4,5,2,2,2,3,5 }。实际上是这样画的- 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绘制此图?
发布于 2017-08-17 16:36:08
检查这种表示方式。
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>
https://stackoverflow.com/questions/45732601
复制相似问题