数据可视化
1. 使用 matplotlib 库
import matplotlib.pyplot as plt
# 创建数据
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
# 使用 matplotlib 绘制散点图
plt.scatter(x, y, label='Data Points', color='blue', marker='o')
# 添加标签和标题
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Scatter Plot')
# 添加图例和网格
plt.legend()
plt.grid(True)
# 显示图形
plt.show()

matplotlib 库说明:
- 导入库:
import matplotlib.pyplot as plt - 创建数据:
x = [1, 2, 3, 4, 5]和y = [2, 3, 5, 7, 11] - 绘制散点图:
plt.scatter(x, y, label='Data Points', color='blue', marker='o') - 添加标签和标题:
plt.xlabel(),plt.ylabel(),plt.title() - 添加图例和网格:
plt.legend(),plt.grid(True) - 显示图形:
plt.show()
2. 使用 seaborn 库
import seaborn as sns
import matplotlib.pyplot as plt
# 创建数据
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
# 使用 Seaborn 绘制散点图
sns.scatterplot(x=x, y=y, label='Data Points')
# 添加标签和标题
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Scatter Plot')
# 添加图例和网格
plt.legend()
plt.grid(True)
# 显示图形
plt.show()

seaborn 库说明:
- 导入库:
import seaborn as sns和import matplotlib.pyplot as plt - 创建数据:同上
- 绘制散点图:
sns.scatterplot(x=x, y=y, label='Data Points') - 其他设置:与 matplotlib 类似,通过 plt 接口完成
3. 使用 pyecharts 库
from pyecharts.charts import Scatter
from pyecharts import options as opts
# 创建数据
data = [(1, 2), (2, 3), (3, 5), (4, 7), (5, 11)]
# 创建散点图对象
scatter = (
Scatter()
.add_xaxis([x for x, y in data])
.add_yaxis("Data Points", [y for x, y in data])
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="Scatter Plot"),
xaxis_opts=opts.AxisOpts(name="X-axis"),
yaxis_opts=opts.AxisOpts(name="Y-axis"),
)
)
# 渲染图表
# 如果在 Jupyter Notebook 中运行,使用 render_notebook()
scatter.render_notebook()
# 如果在普通 Python 脚本中运行,使用 render() 保存为 HTML 文件
# scatter.render("scatter_plot.html")
pyecharts 库说明:
- 导入库:
from pyecharts.charts import Scatter和from pyecharts import options as opts - 创建数据:
data = [(1, 2), ...] - 创建散点图对象:链式调用
.add_xaxis(),.add_yaxis() - 设置选项:
.set_series_opts(),.set_global_opts() - 渲染图表:Notebook 用
render_notebook(),脚本用render()
注意
如果你在 Jupyter Notebook 中运行这段代码,但是图表没有显示出来,可能是因为 render_notebook() 方法没有被正确执行,或者你的环境配置有问题。下面是一些可能的解决方案:
-
确保安装了所有必要的库
pip install pyecharts -
检查 Jupyter Notebook 的版本 确保使用的 Jupyter Notebook 版本支持
render_notebook()方法。 -
使用
render()方法保存为 HTML 文件 如果render_notebook()方法不起作用,可以尝试将图表保存为 HTML 文件,然后手动打开这个文件查看图表。scatter.render("scatter_plot.html") -
使用
IFrame在 Notebook 中显示 HTML 文件from IPython.display import IFrame scatter.render("scatter_plot.html") IFrame('scatter_plot.html', width=800, height=600) -
检查是否有其他输出干扰 确保在执行绘图代码之前没有其他输出。
-
重启 Jupyter Notebook 如果以上方法都不奏效,可以尝试重启 Jupyter Notebook 服务器。
比较三种库的特点
| 库 | 特点 | 适用场景 |
|---|---|---|
matplotlib | 基础库,支持自定义,静态图表 | 科研论文,数据分析报告 |
seaborn | 基于 matplotlib,样式美观 | 统计分析,探索性数据分析 |
pyecharts | 交互性强,适合网页展示 | 数据展示,交互式仪表板 |
选择建议
- 如果需要在科研或数据分析中生成静态图表,
matplotlib是基础且可靠的选择。 - 需要更多美观效果和便捷的统计分析时,
seaborn提供了友好的界面。 - 若要在网页中展示交互式图表,
pyecharts能生成包含交互功能的 HTML 文件,非常适合网络发布。
总结
本文对比了 Matplotlib、Seaborn 和 Pyecharts 三种 Python 绘图库在绘制散点图时的用法与特点。Matplotlib 适合基础静态图表,Seaborn 适合统计美化,Pyecharts 适合交互式网页展示。开发者可根据具体需求选择合适的工具。


