aa.html 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>3D Scatter Plot</title>
  7. <!-- 使用备用 CDN 加载 Plotly.js -->
  8. <script src="https://cdn.bootcdn.net/ajax/libs/plotly.js/2.35.3/plotly.min.js"></script>
  9. </head>
  10. <body>
  11. <div id="plotly-3d-chart" style="width: 100%; height: 600px"></div>
  12. <script>
  13. // 原始数据
  14. const xData = [5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9]; // sepal_length
  15. const yData = [3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1]; // sepal_width
  16. const zData = [0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.2]; // petal_width
  17. const species = [
  18. "setosa",
  19. "setosa",
  20. "setosa",
  21. "setosa",
  22. "setosa",
  23. "versicolor",
  24. "versicolor",
  25. "versicolor",
  26. "virginica",
  27. "virginica",
  28. ];
  29. // 创建 trace 数组
  30. const data = [
  31. {
  32. type: "scatter3d",
  33. mode: "markers",
  34. x: xData.slice(0, 5), // Setosa
  35. y: yData.slice(0, 5),
  36. z: zData.slice(0, 5),
  37. marker: {
  38. color: ["red", "red", "red", "red", "red"], // 为 Setosa 设置颜色
  39. size: 10,
  40. },
  41. name: "Setosa", // 图例中的名称
  42. },
  43. {
  44. type: "scatter3d",
  45. mode: "markers",
  46. x: xData.slice(5, 8), // Versicolor
  47. y: yData.slice(5, 8),
  48. z: zData.slice(5, 8),
  49. marker: {
  50. color: ["green", "green", "green"], // 为 Versicolor 设置颜色
  51. size: 10,
  52. },
  53. name: "Versicolor", // 图例中的名称
  54. },
  55. {
  56. type: "scatter3d",
  57. mode: "markers",
  58. x: xData.slice(8), // Virginica
  59. y: yData.slice(8),
  60. z: zData.slice(8),
  61. marker: {
  62. color: ["blue", "blue"], // 为 Virginica 设置颜色
  63. size: 10,
  64. },
  65. name: "Virginica", // 图例中的名称
  66. },
  67. ];
  68. const layout = {
  69. title: "3D Scatter Plot of Iris Dataset",
  70. scene: {
  71. xaxis: { title: "Sepal Length" },
  72. yaxis: { title: "Sepal Width" },
  73. zaxis: { title: "Petal Width" },
  74. },
  75. showlegend: true, // 显示图例
  76. };
  77. // 使用 Plotly 绘制图表
  78. Plotly.newPlot("plotly-3d-chart", data, layout);
  79. </script>
  80. </body>
  81. </html>