当前位置: 代码网 > it编程>前端脚本>Python > Python绘图工具使用Matplotlib、Seaborn和Pyecharts绘制散点图详解

Python绘图工具使用Matplotlib、Seaborn和Pyecharts绘制散点图详解

2026年01月06日 Python 我要评论
数据可视化1.使用 matplotlib 库import matplotlib.pyplot as plt# 创建数据x = [1, 2, 3, 4, 5]y = [2, 3, 5, 7, 11]#

数据可视化

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('x-axis')plt.ylabel('y-axis')plt.title('scatter plot')
  • 添加图例和网格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 snsimport matplotlib.pyplot as plt
  • 创建数据x = [1, 2, 3, 4, 5]y = [2, 3, 5, 7, 11]
  • 绘制散点图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()

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 scatterfrom 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(),在普通python脚本中使用render("scatter_plot.html")

注意

如果你在jupyter notebook中运行这段代码,但是图表没有显示出来,可能是因为render_notebook()方法没有被正确执行,或者你的环境配置有问题。下面是一些可能的解决方案:

1. 确保安装了所有必要的库

首先,确保已经安装了pyecharts及其相关依赖。可以使用以下命令来安装:

pip install pyecharts

2. 检查jupyter notebook的版本

确保使用的jupyter notebook版本支持render_notebook()方法。通常情况下,较新版本的jupyter notebook应该没有问题。

3. 使用render()方法保存为html文件

如果render_notebook()方法不起作用,可以尝试将图表保存为html文件,然后手动打开这个文件查看图表。

# 渲染图表并保存为html文件
scatter.render("scatter_plot.html")

保存后,你可以在文件浏览器中找到scatter_plot.html文件并双击打开它,查看图表。

4. 使用iframe在notebook中显示html文件

如果你希望在jupyter notebook中直接显示html文件,可以使用ipython.display.iframe来实现。

from ipython.display import iframe

# 渲染图表并保存为html文件
scatter.render("scatter_plot.html")

# 在notebook中显示html文件
iframe('scatter_plot.html', width=800, height=600)

5. 检查是否有其他输出干扰

有时候,jupyter notebook中的其他输出可能会干扰图表的显示。确保在执行绘图代码之前没有其他输出。

6. 重启jupyter notebook

如果以上方法都不奏效,可以尝试重启jupyter notebook服务器,有时这可以解决一些临时性的问题。

比较三种库的特点

特点适用场景
matplotlib基础库,支持自定义,静态图表科研论文,数据分析报告
seaborn基于 matplotlib,样式美观统计分析,探索性数据分析
pyecharts交互性强,适合网页展示数据展示,交互式仪表板

选择建议

  • 如果需要在科研或数据分析中生成静态图表,matplotlib 是基础且可靠的选择。
  • 需要更多美观效果和便捷的统计分析时,seaborn 提供了友好的界面。
  • 若要在网页中展示交互式图表,pyecharts 能生成包含交互功能的 html 文件,非常适合网络发布。

目标

  • 学习和实践:通过实际操作,掌握使用 matplotlibseabornpyecharts 绘制散点图的方法。
  • 比较不同库的特点:了解每个库的优缺点,选择最适合具体需求的工具。
  • 数据可视化:通过散点图展示数据之间的关系,帮助更好地理解和解释数据。

总结

嘿,数据可视化这事儿暂时要告一段落啦,不过以后有机会的话,咱还能再写写关于数据可视化的东西。😎

到此这篇关于python绘图工具使用matplotlib、seaborn和pyecharts绘制散点图的文章就介绍到这了,更多相关matplotlib、seaborn和pyecharts绘制散点图内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com