Python数据可视化之散点图(实战篇---从入门到精通)

张开发
2026/4/18 22:52:08 15 分钟阅读

分享文章

Python数据可视化之散点图(实战篇---从入门到精通)
1. 为什么选择散点图散点图是数据分析中最基础也最实用的可视化工具之一。我第一次接触散点图是在分析电商用户行为时当时需要找出用户年龄和消费金额之间的关系。散点图就像是一个放大镜能让你一眼看出数据中隐藏的模式。举个例子假设你手上有1000条用户数据包含年龄和月消费金额。用Excel表格看这些数字你可能会头晕眼花。但把这些数据点画在散点图上立刻就能发现25-35岁的用户消费最活跃形成明显的密集区域而50岁以上用户的点则分散在较低位置。这种直观的洞察是表格数据永远给不了的。2. 环境准备与数据生成2.1 三件套安装指南工欲善其事必先利其器我们先搞定三个必备工具NumPy数据生成的瑞士军刀Pandas数据处理的全能选手Matplotlib可视化界的扛把子安装命令很简单pip install numpy pandas matplotlib如果你遇到安装慢的问题可以试试国内镜像源pip install numpy pandas matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple2.2 创建模拟数据集让我们生成一份更接近真实场景的数据。假设我们要分析某电商平台的用户数据import numpy as np import pandas as pd # 设置随机种子保证结果可复现 np.random.seed(42) # 生成1000个18-70岁的用户年龄 ages np.random.randint(18, 70, size1000) # 生成消费金额年龄越大消费越低但存在随机波动 consumption 3000 - ages * 20 np.random.normal(0, 500, size1000) # 创建DataFrame df pd.DataFrame({Age: ages, Consumption: consumption}) # 查看前5行 print(df.head())这份数据的特点是年龄范围18-70岁消费金额与年龄呈负相关加入了随机噪声模拟真实情况3. 基础散点图绘制3.1 你的第一个散点图让我们用最简单的代码画出散点图import matplotlib.pyplot as plt plt.figure(figsize(10, 6)) plt.scatter(df[Age], df[Consumption]) plt.title(Age vs Consumption) plt.xlabel(Age) plt.ylabel(Monthly Consumption (CNY)) plt.grid(True) plt.show()这段代码虽然简单但已经包含了散点图的核心要素figsize控制图形大小10×6是比较适合报告的尺寸scatter核心绘图函数title/xlabel/ylabel让图表更易读grid添加网格线方便观察数据分布3.2 第一次优化解决重叠问题当数据点很多时你会发现点都挤在一起。这时候可以调整点的大小(s参数)设置透明度(alpha参数)使用边缘色(edgecolor)改进后的代码plt.figure(figsize(10, 6)) plt.scatter(df[Age], df[Consumption], s20, alpha0.6, edgecolorwhite) plt.title(Age vs Consumption (Optimized)) plt.xlabel(Age) plt.ylabel(Monthly Consumption (CNY)) plt.grid(True, alpha0.3) plt.show()4. 进阶技巧添加分类维度4.1 按性别分类着色假设我们新增了性别数据# 随机生成性别数据 genders np.random.choice([Male, Female], size1000) df[Gender] genders现在可以用不同颜色区分性别colors {Male: blue, Female: red} plt.figure(figsize(10, 6)) for gender in [Male, Female]: subset df[df[Gender] gender] plt.scatter(subset[Age], subset[Consumption], colorcolors[gender], labelgender, s20, alpha0.6) plt.title(Age vs Consumption by Gender) plt.xlabel(Age) plt.ylabel(Monthly Consumption (CNY)) plt.legend() plt.grid(True, alpha0.3) plt.show()4.2 添加趋势线要更清楚地看到年龄和消费的关系可以添加趋势线from scipy.stats import linregress # 计算线性回归 slope, intercept, r_value, p_value, std_err linregress(df[Age], df[Consumption]) plt.figure(figsize(10, 6)) plt.scatter(df[Age], df[Consumption], s20, alpha0.6) plt.plot(df[Age], intercept slope * df[Age], r, labelfY {intercept:.1f} {slope:.1f}X) plt.title(Age vs Consumption with Trendline) plt.xlabel(Age) plt.ylabel(Monthly Consumption (CNY)) plt.legend() plt.grid(True, alpha0.3) plt.show()5. 专业级美化技巧5.1 使用Seaborn风格Matplotlib的默认样式比较简陋可以改用Seaborn风格import seaborn as sns sns.set_style(whitegrid) plt.figure(figsize(10, 6)) sns.scatterplot(datadf, xAge, yConsumption, hueGender, palette[blue, red], alpha0.6, s50) plt.title(Professional Style with Seaborn) plt.xlabel(Age) plt.ylabel(Monthly Consumption (CNY)) plt.show()5.2 添加边际分布图想要同时看到单变量分布试试边际分布图sns.set_style(whitegrid) g sns.JointGrid(datadf, xAge, yConsumption, height8) g.plot_joint(sns.scatterplot, huedf[Gender], palette[blue, red], alpha0.6) g.plot_marginals(sns.histplot, kdeTrue) plt.suptitle(Age vs Consumption with Marginal Distributions) plt.tight_layout() plt.show()6. 实战案例用户分群分析6.1 识别高价值用户假设我们定义高价值用户消费 2500元普通用户消费 ≤ 2500元df[User_Type] np.where(df[Consumption] 2500, High Value, Normal) plt.figure(figsize(10, 6)) for user_type in [High Value, Normal]: subset df[df[User_Type] user_type] plt.scatter(subset[Age], subset[Consumption], labeluser_type, alpha0.6) plt.axhline(y2500, colorgray, linestyle--, alpha0.5) plt.title(Identifying High Value Users) plt.xlabel(Age) plt.ylabel(Monthly Consumption (CNY)) plt.legend() plt.grid(True, alpha0.3) plt.show()6.2 添加注释标记想要突出显示特定数据点highlight df.nlargest(3, Consumption) plt.figure(figsize(10, 6)) plt.scatter(df[Age], df[Consumption], alpha0.4) plt.scatter(highlight[Age], highlight[Consumption], colorred, s100, labelTop 3) for i, row in highlight.iterrows(): plt.annotate(f{row[Consumption]:.0f}元, (row[Age], row[Consumption]), textcoordsoffset points, xytext(0,10), hacenter) plt.title(Highlighting Top Spenders) plt.xlabel(Age) plt.ylabel(Monthly Consumption (CNY)) plt.legend() plt.show()7. 导出与分享7.1 保存高清图片plt.figure(figsize(10, 6)) plt.scatter(df[Age], df[Consumption], alpha0.6) plt.title(Final Version for Report) plt.xlabel(Age) plt.ylabel(Monthly Consumption (CNY)) # 保存为高清PNG plt.savefig(age_consumption.png, dpi300, bbox_inchestight)7.2 交互式探索如果你需要交互功能可以试试Plotlyimport plotly.express as px fig px.scatter(df, xAge, yConsumption, colorGender, hover_data[User_Type], titleInteractive Scatter Plot) fig.show()

更多文章