使用plotlyjs绘制y=3x^2的曲线并保存图片为svg格式
以下是使用plotly.js绘制y=3x^2的曲线,并保存图片为svg格式的代码示例:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<div id="plot"></div>
<script>
// 定义x轴数据
var x = [];
for(var i = -10; i <= 10; i += 0.1) {
x.push(i);
}
// 定义y轴数据
var y = [];
for(var i = 0; i < x.length; i++) {
y.push(3 * Math.pow(x[i], 2));
}
// 定义图形布局
var layout = {
title: 'y = 3x^2',
xaxis: {
title: 'x'
},
yaxis: {
title: 'y'
}
};
// 定义图形数据
var data = [{
x: x,
y: y,
type: 'scatter'
}];
// 绘制图形
Plotly.newPlot('plot', data, layout).then(function(gd) {
// 保存图形为svg格式
Plotly.downloadImage(gd, {
format: 'svg',
width: 800,
height: 600,
filename: 'y=3x^2'
});
});
</script>
</body>
</html>
运行代码后,会在页面上绘制出y=3x^2的曲线图,并自动下载保存为svg格式的图片文件。
原文地址: https://www.cveoy.top/t/topic/bedA 著作权归作者所有。请勿转载和采集!