pyecharts如何绘制二维散点图
时间: 2023-07-09 22:35:05 浏览: 54
要使用 pyecharts 绘制二维散点图,需要安装 pyecharts 和 numpy 两个库。以下是一个简单的例子,展示如何使用 pyecharts 绘制二维散点图:
```python
import numpy as np
from pyecharts.charts import Scatter
from pyecharts import options as opts
# 生成随机数据
x_data = np.random.rand(100)
y_data = np.random.rand(100)
# 创建散点图对象
scatter = Scatter()
# 添加数据
scatter.add_xaxis(x_data)
scatter.add_yaxis("y", y_data)
# 设置图表标题和坐标轴标签
scatter.set_global_opts(title_opts=opts.TitleOpts(title="二维散点图"),
xaxis_opts=opts.AxisOpts(name="x"),
yaxis_opts=opts.AxisOpts(name="y"))
# 显示图表
scatter.render("scatter.html")
```
在这个例子中,我们首先使用 numpy 库生成了两个长度为 100 的随机数组,用作散点图的 x 和 y 数据。然后创建了一个 Scatter 对象,添加了 x 和 y 数据。最后,设置了图表标题和坐标轴标签,并将图表保存到 scatter.html 文件中。
运行这段代码后,会生成一个 scatter.html 文件,打开文件即可看到生成的二维散点图。