Python绘图工具使用Matplotlib、Seaborn和Pyecharts绘制散点图详解
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li><a href="#_label0">数据可视化</a></li><ul class="second_class_ul"><li><a href="#_lab2_0_0">1.使用 matplotlib 库</a></li><ul class="third_class_ul"><li><a href="#_label3_0_0_0">matplotlib 库</a></li></ul><li><a href="#_lab2_0_1">2 .使用 seaborn 库</a></li><ul class="third_class_ul"><li><a href="#_label3_0_1_1">seaborn 库</a></li></ul><li><a href="#_lab2_0_2">3 .使用 pyecharts库</a></li><ul class="third_class_ul"><li><a href="#_label3_0_2_2">pyecharts库</a></li><li><a href="#_label3_0_2_3">注意</a></li></ul></ul><li><a href="#_label1">比较三种库的特点</a></li><ul class="second_class_ul"><li><a href="#_lab2_1_3">选择建议</a></li><ul class="third_class_ul"><li><a href="#_label3_1_3_4">目标</a></li></ul></ul><li><a href="#_label2">总结</a></li><ul class="second_class_ul"></ul></ul></div><p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202601/2026010609263559.png" /></p><p class="maodian"><a name="_label0"></a></p><h2>数据可视化</h2>
<p class="maodian"><a name="_lab2_0_0"></a></p><h3>1.使用 matplotlib 库</h3>
<div class="jb51code"><pre class="brush:py;">import matplotlib.pyplot as plt
# 创建数据
x =
y =
# 使用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()
</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202601/2026010609263578.png" /></p>
<p class="maodian"><a name="_label3_0_0_0"></a></p><h4>matplotlib 库</h4>
<ul><li><strong>导入库</strong>:<code>import matplotlib.pyplot as plt</code></li><li><strong>创建数据</strong>:<code>x = </code> 和 <code>y = </code></li><li><strong>绘制散点图</strong>:<code>plt.scatter(x, y, label='Data Points', color='blue', marker='o')</code></li><li><strong>添加标签和标题</strong>:<code>plt.xlabel('X-axis')</code>,<code>plt.ylabel('Y-axis')</code>,<code>plt.title('Scatter Plot')</code></li><li><strong>添加图例和网格</strong>:<code>plt.legend()</code>,<code>plt.grid(True)</code></li><li><strong>显示图形</strong>:<code>plt.show()</code></li></ul>
<p class="maodian"><a name="_lab2_0_1"></a></p><h3>2 .使用 seaborn 库</h3>
<div class="jb51code"><pre class="brush:py;">import seaborn as sns
import matplotlib.pyplot as plt
# 创建数据
x =
y =
# 使用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()
</pre></div>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202601/2026010609263571.png" /></p>
<p class="maodian"><a name="_label3_0_1_1"></a></p><h4>seaborn 库</h4>
<ul><li><strong>导入库</strong>:<code>import seaborn as sns</code> 和 <code>import matplotlib.pyplot as plt</code></li><li><strong>创建数据</strong>:<code>x = </code> 和 <code>y = </code></li><li><strong>绘制散点图</strong>:<code>sns.scatterplot(x=x, y=y, label='Data Points')</code></li><li><strong>添加标签和标题</strong>:<code>plt.xlabel('X-axis')</code>,<code>plt.ylabel('Y-axis')</code>,<code>plt.title('Scatter Plot')</code></li><li><strong>添加图例和网格</strong>:<code>plt.legend()</code>,<code>plt.grid(True)</code></li><li><strong>显示图形</strong>:<code>plt.show()</code></li></ul>
<p class="maodian"><a name="_lab2_0_2"></a></p><h3>3 .使用 pyecharts库</h3>
<div class="jb51code"><pre class="brush:py;">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()
.add_yaxis("Data Points", )
.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")
</pre></div>
<p class="maodian"><a name="_label3_0_2_2"></a></p><h4>pyecharts库</h4>
<ul><li><strong>导入库</strong>:<code>from pyecharts.charts import Scatter</code> 和 <code>from pyecharts import options as opts</code></li><li><strong>创建数据</strong>:<code>data = [(1, 2), (2, 3), (3, 5), (4, 7), (5, 11)]</code></li><li><strong>创建散点图对象</strong>:<code>scatter = Scatter().add_xaxis().add_yaxis("Data Points", )</code></li><li><strong>设置系列选项</strong>:<code>set_series_opts(label_opts=opts.LabelOpts(is_show=False))</code></li><li><strong>设置全局选项</strong>:<code>set_global_opts(title_opts=opts.TitleOpts(title="Scatter Plot"), xaxis_opts=opts.AxisOpts(name="X-axis"), yaxis_opts=opts.AxisOpts(name="Y-axis"))</code></li><li><strong>渲染图表</strong>:在Jupyter Notebook中使用<code>render_notebook()</code>,在普通Python脚本中使用<code>render("scatter_plot.html")</code></li></ul>
<p class="maodian"><a name="_label3_0_2_3"></a></p><h4>注意</h4>
<p>如果你在Jupyter Notebook中运行这段代码,但是图表没有显示出来,可能是因为<code>render_notebook()</code>方法没有被正确执行,或者你的环境配置有问题。下面是一些可能的解决方案:</p>
<p><strong>1. 确保安装了所有必要的库</strong></p>
<p>首先,确保已经安装了<code>pyecharts</code>及其相关依赖。可以使用以下命令来安装:</p>
<div class="jb51code"><pre class="brush:py;">pip install pyecharts
</pre></div>
<p><strong>2. 检查Jupyter Notebook的版本</strong></p>
<p>确保使用的Jupyter Notebook版本支持<code>render_notebook()</code>方法。通常情况下,较新版本的Jupyter Notebook应该没有问题。</p>
<p><strong>3. 使用render()方法保存为HTML文件</strong></p>
<p>如果<code>render_notebook()</code>方法不起作用,可以尝试将图表保存为HTML文件,然后手动打开这个文件查看图表。</p>
<div class="jb51code"><pre class="brush:py;"># 渲染图表并保存为HTML文件
scatter.render("scatter_plot.html")
</pre></div>
<p>保存后,你可以在文件浏览器中找到<code>scatter_plot.html</code>文件并双击打开它,查看图表。</p>
<p><strong>4. 使用IFrame在Notebook中显示HTML文件</strong></p>
<p>如果你希望在Jupyter Notebook中直接显示HTML文件,可以使用<code>IPython.display.IFrame</code>来实现。</p>
<div class="jb51code"><pre class="brush:py;">from IPython.display import IFrame
# 渲染图表并保存为HTML文件
scatter.render("scatter_plot.html")
# 在Notebook中显示HTML文件
IFrame('scatter_plot.html', width=800, height=600)
</pre></div>
<p><strong>5. 检查是否有其他输出干扰</strong></p>
<p>有时候,Jupyter Notebook中的其他输出可能会干扰图表的显示。确保在执行绘图代码之前没有其他输出。</p>
<p><strong>6. 重启Jupyter Notebook</strong></p>
<p>如果以上方法都不奏效,可以尝试重启Jupyter Notebook服务器,有时这可以解决一些临时性的问题。</p>
<p class="maodian"><a name="_label1"></a></p><h2>比较三种库的特点</h2>
<table><thead><tr><th>库</th><th>特点</th><th>适用场景</th></tr></thead><tbody><tr><td><code>matplotlib</code></td><td>基础库,支持自定义,静态图表</td><td>科研论文,数据分析报告</td></tr><tr><td><code>seaborn</code></td><td>基于 <code>matplotlib</code>,样式美观</td><td>统计分析,探索性数据分析</td></tr><tr><td><code>pyecharts</code></td><td>交互性强,适合网页展示</td><td>数据展示,交互式仪表板</td></tr></tbody></table>
<p class="maodian"><a name="_lab2_1_3"></a></p><h3>选择建议</h3>
<ul><li>如果需要在科研或数据分析中生成静态图表,<code>matplotlib</code> 是基础且可靠的选择。</li><li>需要更多美观效果和便捷的统计分析时,<code>seaborn</code> 提供了友好的界面。</li><li>若要在网页中展示交互式图表,<code>pyecharts</code> 能生成包含交互功能的 HTML 文件,非常适合网络发布。</li></ul>
<p class="maodian"><a name="_label3_1_3_4"></a></p><h4>目标</h4>
<ul><li><strong>学习和实践</strong>:通过实际操作,掌握使用 <code>matplotlib</code>、<code>seaborn</code> 和 <code>pyecharts</code> 绘制散点图的方法。</li><li><strong>比较不同库的特点</strong>:了解每个库的优缺点,选择最适合具体需求的工具。</li><li><strong>数据可视化</strong>:通过散点图展示数据之间的关系,帮助更好地理解和解释数据。</li></ul>
<p class="maodian"><a name="_label2"></a></p><h2>总结</h2>
<p>嘿,数据可视化这事儿暂时要告一段落啦,不过以后有机会的话,咱还能再写写关于数据可视化的东西。😎</p>
頁:
[1]