简体中文 繁體中文 English Deutsch 한국 사람 بالعربية TÜRKÇE português คนไทย Français Japanese

站内搜索

搜索

活动公告

通知:本站资源由网友上传分享,如有违规等问题请到版务模块进行投诉,将及时处理!
10-23 09:31

Matplotlib与Pandas集成应用实战指南利用Python两大神器轻松实现数据分析和可视化无缝对接

SunJu_FaceMall

3万

主题

166

科技点

3万

积分

大区版主

碾压王

积分
32106
发表于 2025-8-24 16:00:00 | 显示全部楼层 |阅读模式 [标记阅至此楼]

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
引言

在数据科学领域,Python已经成为最受欢迎的编程语言之一。而在Python的数据科学生态系统中,Matplotlib和Pandas无疑是两个最重要的库。Pandas提供了强大的数据处理和分析能力,而Matplotlib则是Python中最基础、最广泛使用的数据可视化库。将这两个库结合使用,可以实现从数据处理到可视化展示的无缝对接,大大提高数据分析的效率和效果。

本文将深入探讨如何将Matplotlib与Pandas进行有效集成,通过实战案例展示它们如何协同工作,帮助读者掌握利用这两大工具进行数据分析和可视化的技巧。

基础知识回顾

Pandas简介

Pandas是一个开源的数据分析和操作库,提供了高性能、易于使用的数据结构和数据分析工具。Pandas的核心数据结构是DataFrame,它是一个二维的、大小可变的、 potentially heterogeneous的表格型数据结构,带有标记的轴(行和列)。
  1. import pandas as pd
  2. # 创建一个简单的DataFrame
  3. data = {'Name': ['John', 'Anna', 'Peter', 'Linda'],
  4.         'Age': [28, 34, 29, 32],
  5.         'City': ['New York', 'Paris', 'Berlin', 'London']}
  6. df = pd.DataFrame(data)
  7. print(df)
复制代码

Matplotlib简介

Matplotlib是Python中最基础的可视化库,它提供了一个类似于MATLAB的绘图接口。使用Matplotlib,可以创建各种静态、动态和交互式的可视化图表。
  1. import matplotlib.pyplot as plt
  2. # 创建一个简单的折线图
  3. x = [1, 2, 3, 4]
  4. y = [10, 20, 25, 30]
  5. plt.plot(x, y)
  6. plt.xlabel('X轴')
  7. plt.ylabel('Y轴')
  8. plt.title('简单折线图')
  9. plt.show()
复制代码

集成方法

基本集成方式

Matplotlib与Pandas的集成非常简单,因为Pandas内置了对Matplotlib的支持。最基本的方式是使用DataFrame或Series的plot()方法,这实际上是调用了Matplotlib的绘图函数。
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. # 创建一个DataFrame
  4. data = {'Year': [2010, 2011, 2012, 2013, 2014],
  5.         'Sales': [100, 150, 200, 250, 300]}
  6. df = pd.DataFrame(data)
  7. # 使用Pandas的plot方法绘图
  8. df.plot(x='Year', y='Sales', kind='line')
  9. plt.title('年度销售额')
  10. plt.ylabel('销售额(万元)')
  11. plt.show()
复制代码

使用Matplotlib直接绘制Pandas数据

除了使用Pandas内置的plot方法外,我们还可以直接使用Matplotlib来绘制Pandas数据。这种方式提供了更多的自定义选项。
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. # 创建一个DataFrame
  4. data = {'Year': [2010, 2011, 2012, 2013, 2014],
  5.         'Sales': [100, 150, 200, 250, 300],
  6.         'Expenses': [80, 120, 150, 180, 200]}
  7. df = pd.DataFrame(data)
  8. # 使用Matplotlib直接绘制
  9. plt.figure(figsize=(10, 6))
  10. plt.plot(df['Year'], df['Sales'], label='销售额', marker='o')
  11. plt.plot(df['Year'], df['Expenses'], label='支出', marker='s')
  12. plt.xlabel('年份')
  13. plt.ylabel('金额(万元)')
  14. plt.title('年度销售额与支出')
  15. plt.legend()
  16. plt.grid(True)
  17. plt.show()
复制代码

使用Pandas的内置绘图功能

Pandas提供了多种内置的绘图类型,可以通过kind参数指定:

• ‘line’:折线图(默认)
• ‘bar’:垂直条形图
• ‘barh’:水平条形图
• ‘hist’:直方图
• ‘box’:箱线图
• ‘kde’:核密度估计图
• ‘density’:同’kde’
• ‘area’:面积图
• ‘pie’:饼图
• ‘scatter’:散点图
• ‘hexbin’:六边形分箱图
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. # 创建一个DataFrame
  4. data = {'Product': ['A', 'B', 'C', 'D'],
  5.         'Sales': [100, 150, 200, 250],
  6.         'Profit': [30, 45, 60, 75]}
  7. df = pd.DataFrame(data)
  8. # 创建一个2x2的子图布局
  9. fig, axes = plt.subplots(2, 2, figsize=(12, 10))
  10. # 绘制条形图
  11. df.plot(x='Product', y='Sales', kind='bar', ax=axes[0, 0])
  12. axes[0, 0].set_title('产品销售额')
  13. # 绘制水平条形图
  14. df.plot(x='Product', y='Sales', kind='barh', ax=axes[0, 1])
  15. axes[0, 1].set_title('产品销售额(水平)')
  16. # 绘制饼图
  17. df.plot(y='Sales', kind='pie', labels=df['Product'], autopct='%1.1f%%', ax=axes[1, 0])
  18. axes[1, 0].set_title('销售额占比')
  19. axes[1, 0].legend().set_visible(False)
  20. # 绘制散点图
  21. df.plot(x='Sales', y='Profit', kind='scatter', ax=axes[1, 1])
  22. axes[1, 1].set_title('销售额与利润关系')
  23. plt.tight_layout()
  24. plt.show()
复制代码

实战案例

案例1:销售数据分析与可视化

假设我们有一份销售数据,包含日期、产品类别、销售额等信息。我们将使用Pandas进行数据处理,然后使用Matplotlib进行可视化。
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. from datetime import datetime, timedelta
  5. # 创建模拟数据
  6. np.random.seed(42)
  7. date_range = pd.date_range(start='2022-01-01', end='2022-12-31', freq='D')
  8. categories = ['电子产品', '服装', '食品', '家居', '图书']
  9. n = len(date_range)
  10. data = {
  11.     'Date': np.random.choice(date_range, n),
  12.     'Category': np.random.choice(categories, n),
  13.     'Sales': np.random.randint(100, 1000, n),
  14.     'Profit': np.random.randint(10, 100, n)
  15. }
  16. df = pd.DataFrame(data)
  17. # 按月份和类别汇总销售额
  18. df['Month'] = df['Date'].dt.to_period('M')
  19. monthly_sales = df.groupby(['Month', 'Category'])['Sales'].sum().unstack()
  20. # 绘制月度销售额趋势
  21. plt.figure(figsize=(12, 6))
  22. monthly_sales.plot(kind='line', marker='o')
  23. plt.title('2022年各类产品月度销售额趋势')
  24. plt.xlabel('月份')
  25. plt.ylabel('销售额(元)')
  26. plt.grid(True)
  27. plt.legend(title='产品类别')
  28. plt.tight_layout()
  29. plt.show()
  30. # 计算各类别销售额占比
  31. category_sales = df.groupby('Category')['Sales'].sum()
  32. plt.figure(figsize=(8, 8))
  33. category_sales.plot(kind='pie', autopct='%1.1f%%', startangle=90)
  34. plt.title('2022年各类产品销售额占比')
  35. plt.ylabel('')  # 移除y标签
  36. plt.tight_layout()
  37. plt.show()
  38. # 分析销售额与利润的关系
  39. plt.figure(figsize=(10, 6))
  40. plt.scatter(df['Sales'], df['Profit'], alpha=0.5)
  41. plt.title('销售额与利润关系')
  42. plt.xlabel('销售额(元)')
  43. plt.ylabel('利润(元)')
  44. plt.grid(True)
  45. plt.tight_layout()
  46. plt.show()
复制代码

案例2:股票数据分析与可视化

在这个案例中,我们将分析股票数据,包括价格走势、交易量、移动平均线等。
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. from datetime import datetime, timedelta
  5. # 创建模拟股票数据
  6. np.random.seed(42)
  7. date_range = pd.date_range(start='2022-01-01', end='2022-12-31', freq='D')
  8. n = len(date_range)
  9. # 生成随机价格走势
  10. initial_price = 100
  11. price_changes = np.random.normal(0, 1, n)
  12. prices = [initial_price]
  13. for change in price_changes:
  14.     prices.append(prices[-1] * (1 + change/100))
  15. prices = prices[1:]  # 移除初始价格
  16. # 生成交易量
  17. volumes = np.random.randint(100000, 500000, n)
  18. # 创建DataFrame
  19. stock_data = pd.DataFrame({
  20.     'Date': date_range,
  21.     'Open': prices * np.random.uniform(0.98, 1.02, n),
  22.     'High': prices * np.random.uniform(1.01, 1.05, n),
  23.     'Low': prices * np.random.uniform(0.95, 0.99, n),
  24.     'Close': prices,
  25.     'Volume': volumes
  26. })
  27. # 计算移动平均线
  28. stock_data['MA5'] = stock_data['Close'].rolling(window=5).mean()
  29. stock_data['MA20'] = stock_data['Close'].rolling(window=20).mean()
  30. stock_data['MA60'] = stock_data['Close'].rolling(window=60).mean()
  31. # 绘制价格走势和移动平均线
  32. plt.figure(figsize=(12, 8))
  33. plt.plot(stock_data['Date'], stock_data['Close'], label='收盘价', alpha=0.7)
  34. plt.plot(stock_data['Date'], stock_data['MA5'], label='5日均线', alpha=0.7)
  35. plt.plot(stock_data['Date'], stock_data['MA20'], label='20日均线', alpha=0.7)
  36. plt.plot(stock_data['Date'], stock_data['MA60'], label='60日均线', alpha=0.7)
  37. plt.title('股票价格走势与移动平均线')
  38. plt.xlabel('日期')
  39. plt.ylabel('价格(元)')
  40. plt.legend()
  41. plt.grid(True)
  42. plt.tight_layout()
  43. plt.show()
  44. # 绘制交易量
  45. plt.figure(figsize=(12, 6))
  46. plt.bar(stock_data['Date'], stock_data['Volume'], color=np.where(stock_data['Close'] >= stock_data['Open'], 'g', 'r'))
  47. plt.title('股票交易量')
  48. plt.xlabel('日期')
  49. plt.ylabel('交易量')
  50. plt.grid(True)
  51. plt.tight_layout()
  52. plt.show()
  53. # 绘制K线图(简化版)
  54. fig, ax = plt.subplots(figsize=(12, 8))
  55. # 定义上涨和下跌的颜色
  56. up = stock_data['Close'] >= stock_data['Open']
  57. down = ~up
  58. # 绘制K线
  59. for idx, row in stock_data.iterrows():
  60.     if up[idx]:
  61.         color = 'red'
  62.         lower = row['Open']
  63.         height = row['Close'] - row['Open']
  64.     else:
  65.         color = 'green'
  66.         lower = row['Close']
  67.         height = row['Open'] - row['Close']
  68.    
  69.     # 绘制影线
  70.     ax.plot([row['Date'], row['Date']], [row['Low'], row['High']], color=color)
  71.    
  72.     # 绘制实体
  73.     ax.bar(row['Date'], height, bottom=lower, width=0.6, color=color)
  74. plt.title('股票K线图')
  75. plt.xlabel('日期')
  76. plt.ylabel('价格(元)')
  77. plt.grid(True)
  78. plt.tight_layout()
  79. plt.show()
复制代码

案例3:人口统计分析与可视化

在这个案例中,我们将分析人口统计数据,包括年龄分布、性别比例、教育水平等。
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. # 创建模拟人口数据
  5. np.random.seed(42)
  6. n = 1000
  7. data = {
  8.     'Age': np.random.randint(18, 80, n),
  9.     'Gender': np.random.choice(['男', '女'], n),
  10.     'Education': np.random.choice(['小学', '初中', '高中', '大专', '本科', '硕士', '博士'], n, p=[0.05, 0.1, 0.2, 0.2, 0.3, 0.1, 0.05]),
  11.     'Income': np.random.randint(2000, 20000, n),
  12.     'City': np.random.choice(['北京', '上海', '广州', '深圳', '杭州', '成都'], n)
  13. }
  14. df = pd.DataFrame(data)
  15. # 年龄分布分析
  16. plt.figure(figsize=(10, 6))
  17. plt.hist(df['Age'], bins=15, edgecolor='black', alpha=0.7)
  18. plt.title('人口年龄分布')
  19. plt.xlabel('年龄')
  20. plt.ylabel('人数')
  21. plt.grid(True, alpha=0.3)
  22. plt.tight_layout()
  23. plt.show()
  24. # 性别比例
  25. gender_counts = df['Gender'].value_counts()
  26. plt.figure(figsize=(8, 8))
  27. plt.pie(gender_counts, labels=gender_counts.index, autopct='%1.1f%%', startangle=90)
  28. plt.title('性别比例')
  29. plt.tight_layout()
  30. plt.show()
  31. # 教育水平分布
  32. education_counts = df['Education'].value_counts().sort_index()
  33. plt.figure(figsize=(10, 6))
  34. education_counts.plot(kind='bar', color='skyblue')
  35. plt.title('教育水平分布')
  36. plt.xlabel('教育水平')
  37. plt.ylabel('人数')
  38. plt.grid(True, alpha=0.3)
  39. plt.tight_layout()
  40. plt.show()
  41. # 城市与收入关系
  42. city_income = df.groupby('City')['Income'].mean().sort_values(ascending=False)
  43. plt.figure(figsize=(10, 6))
  44. city_income.plot(kind='bar', color='green')
  45. plt.title('各城市平均收入')
  46. plt.xlabel('城市')
  47. plt.ylabel('平均收入(元)')
  48. plt.grid(True, alpha=0.3)
  49. plt.tight_layout()
  50. plt.show()
  51. # 年龄与收入关系(按性别分组)
  52. plt.figure(figsize=(10, 6))
  53. for gender in ['男', '女']:
  54.     subset = df[df['Gender'] == gender]
  55.     plt.scatter(subset['Age'], subset['Income'], alpha=0.5, label=gender)
  56. plt.title('年龄与收入关系(按性别分组)')
  57. plt.xlabel('年龄')
  58. plt.ylabel('收入(元)')
  59. plt.legend()
  60. plt.grid(True, alpha=0.3)
  61. plt.tight_layout()
  62. plt.show()
  63. # 教育水平与收入关系
  64. education_income = df.groupby('Education')['Income'].mean().sort_values()
  65. plt.figure(figsize=(10, 6))
  66. education_income.plot(kind='barh', color='purple')
  67. plt.title('教育水平与平均收入关系')
  68. plt.xlabel('平均收入(元)')
  69. plt.ylabel('教育水平')
  70. plt.grid(True, alpha=0.3)
  71. plt.tight_layout()
  72. plt.show()
复制代码

高级技巧

自定义图表样式

Matplotlib允许我们自定义图表的各种样式,包括颜色、线型、标记、字体等。
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. # 创建模拟数据
  5. np.random.seed(42)
  6. years = np.arange(2010, 2021)
  7. sales_A = np.random.randint(100, 300, len(years))
  8. sales_B = np.random.randint(150, 350, len(years))
  9. df = pd.DataFrame({
  10.     'Year': years,
  11.     'Product_A': sales_A,
  12.     'Product_B': sales_B
  13. })
  14. # 设置图表样式
  15. plt.style.use('seaborn')  # 使用seaborn风格
  16. plt.figure(figsize=(12, 6))
  17. # 绘制图表并自定义样式
  18. plt.plot(df['Year'], df['Product_A'],
  19.          color='#1f77b4',  # 自定义颜色
  20.          linestyle='--',   # 虚线
  21.          marker='o',       # 圆形标记
  22.          markersize=8,     # 标记大小
  23.          markerfacecolor='white',  # 标记面颜色
  24.          markeredgewidth=2,        # 标记边宽度
  25.          label='产品A')
  26. plt.plot(df['Year'], df['Product_B'],
  27.          color='#ff7f0e',  # 自定义颜色
  28.          linestyle='-',    # 实线
  29.          marker='s',       # 方形标记
  30.          markersize=8,     # 标记大小
  31.          markerfacecolor='white',  # 标记面颜色
  32.          markeredgewidth=2,        # 标记边宽度
  33.          label='产品B')
  34. # 自定义标题和标签
  35. plt.title('产品销售趋势对比', fontsize=16, fontweight='bold', pad=20)
  36. plt.xlabel('年份', fontsize=12, labelpad=10)
  37. plt.ylabel('销售额(万元)', fontsize=12, labelpad=10)
  38. # 自定义刻度
  39. plt.xticks(df['Year'], rotation=45)  # 旋转x轴标签
  40. plt.yticks(np.arange(0, max(max(sales_A), max(sales_B)) + 50, 50))
  41. # 自定义网格
  42. plt.grid(True, linestyle=':', alpha=0.6)  # 点线网格,半透明
  43. # 自定义图例
  44. plt.legend(fontsize=12, frameon=True, fancybox=True, framealpha=0.8,
  45.            loc='upper left', bbox_to_anchor=(1, 1))  # 图例放在图表外
  46. # 调整边距
  47. plt.tight_layout()
  48. plt.show()
复制代码

多子图布局

Matplotlib提供了多种方式来创建多子图布局,包括subplots()、subplot2grid()和GridSpec等。
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. from matplotlib.gridspec import GridSpec
  5. # 创建模拟数据
  6. np.random.seed(42)
  7. categories = ['电子产品', '服装', '食品', '家居', '图书']
  8. n = len(categories)
  9. data = {
  10.     'Category': categories,
  11.     'Sales': np.random.randint(100, 500, n),
  12.     'Profit': np.random.randint(20, 100, n),
  13.     'Growth': np.random.uniform(-5, 15, n)
  14. }
  15. df = pd.DataFrame(data)
  16. # 使用GridSpec创建复杂布局
  17. fig = plt.figure(figsize=(14, 10))
  18. gs = GridSpec(3, 2, figure=fig)
  19. # 第一个子图:销售额条形图
  20. ax1 = fig.add_subplot(gs[0, 0])
  21. bars = ax1.bar(df['Category'], df['Sales'], color='skyblue')
  22. ax1.set_title('产品销售额', fontsize=14, fontweight='bold')
  23. ax1.set_ylabel('销售额(万元)')
  24. ax1.grid(True, alpha=0.3)
  25. # 在条形上添加数值标签
  26. for bar in bars:
  27.     height = bar.get_height()
  28.     ax1.text(bar.get_x() + bar.get_width()/2., height,
  29.              f'{height}',
  30.              ha='center', va='bottom')
  31. # 第二个子图:利润条形图
  32. ax2 = fig.add_subplot(gs[0, 1])
  33. bars = ax2.bar(df['Category'], df['Profit'], color='lightgreen')
  34. ax2.set_title('产品利润', fontsize=14, fontweight='bold')
  35. ax2.set_ylabel('利润(万元)')
  36. ax2.grid(True, alpha=0.3)
  37. # 在条形上添加数值标签
  38. for bar in bars:
  39.     height = bar.get_height()
  40.     ax2.text(bar.get_x() + bar.get_width()/2., height,
  41.              f'{height}',
  42.              ha='center', va='bottom')
  43. # 第三个子图:增长率折线图
  44. ax3 = fig.add_subplot(gs[1, :])
  45. ax3.plot(df['Category'], df['Growth'], marker='o', linestyle='-', color='red', markersize=8)
  46. ax3.set_title('产品增长率', fontsize=14, fontweight='bold')
  47. ax3.set_ylabel('增长率(%)')
  48. ax3.grid(True, alpha=0.3)
  49. # 添加零线
  50. ax3.axhline(y=0, color='black', linestyle='-', alpha=0.3)
  51. # 在数据点上添加数值标签
  52. for i, v in enumerate(df['Growth']):
  53.     ax3.text(i, v + (0.5 if v >= 0 else -1.5), f'{v:.1f}%',
  54.              ha='center', va='bottom' if v >= 0 else 'top')
  55. # 第四个子图:销售额与利润散点图
  56. ax4 = fig.add_subplot(gs[2, :])
  57. scatter = ax4.scatter(df['Sales'], df['Profit'], s=100, c=df['Growth'],
  58.                      cmap='coolwarm', alpha=0.7, edgecolors='black')
  59. ax4.set_title('销售额与利润关系(颜色表示增长率)', fontsize=14, fontweight='bold')
  60. ax4.set_xlabel('销售额(万元)')
  61. ax4.set_ylabel('利润(万元)')
  62. ax4.grid(True, alpha=0.3)
  63. # 添加颜色条
  64. cbar = plt.colorbar(scatter, ax=ax4)
  65. cbar.set_label('增长率(%)')
  66. # 为每个点添加产品标签
  67. for i, row in df.iterrows():
  68.     ax4.text(row['Sales'], row['Profit'], row['Category'],
  69.              ha='center', va='bottom', fontsize=10)
  70. # 调整子图间距
  71. plt.tight_layout()
  72. plt.show()
复制代码

交互式可视化

Matplotlib虽然主要用于静态图表,但也支持一些基本的交互功能。
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. from matplotlib.widgets import Slider, Button
  5. # 创建模拟数据
  6. np.random.seed(42)
  7. years = np.arange(2000, 2021)
  8. categories = ['产品A', '产品B', '产品C', '产品D', '产品E']
  9. n = len(years)
  10. # 为每个产品创建随机销售额数据
  11. data = {'Year': years}
  12. for category in categories:
  13.     # 每个产品有不同的基础销售额和增长趋势
  14.     base_sales = np.random.randint(100, 300)
  15.     growth_rate = np.random.uniform(0.01, 0.05)
  16.     sales = [base_sales * (1 + growth_rate) ** i for i in range(n)]
  17.     # 添加一些随机波动
  18.     sales = sales * np.random.uniform(0.9, 1.1, n)
  19.     data[category] = sales
  20. df = pd.DataFrame(data)
  21. # 创建图表和轴
  22. fig, ax = plt.subplots(figsize=(12, 6))
  23. plt.subplots_adjust(bottom=0.25)  # 为滑块留出空间
  24. # 初始绘制所有产品
  25. lines = []
  26. for category in categories:
  27.     line, = ax.plot(df['Year'], df[category], label=category, marker='o', markersize=4)
  28.     lines.append(line)
  29. # 设置图表属性
  30. ax.set_title('产品销售趋势(可交互)', fontsize=16, fontweight='bold')
  31. ax.set_xlabel('年份')
  32. ax.set_ylabel('销售额(万元)')
  33. ax.grid(True, alpha=0.3)
  34. ax.legend(loc='upper left')
  35. # 添加滑块用于调整年份范围
  36. axcolor = 'lightgoldenrodyellow'
  37. ax_year = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
  38. year_slider = Slider(
  39.     ax=ax_year,
  40.     label='显示年份范围',
  41.     valmin=0,
  42.     valmax=len(years)-1,
  43.     valinit=len(years)-1,
  44.     valstep=1
  45. )
  46. # 定义滑块更新函数
  47. def update(val):
  48.     year_idx = int(year_slider.val)
  49.     ax.set_xlim(years[0], years[year_idx])
  50.     fig.canvas.draw_idle()
  51. year_slider.on_changed(update)
  52. # 添加按钮用于切换显示/隐藏产品线
  53. ax_button = []
  54. buttons = []
  55. for i, category in enumerate(categories):
  56.     # 为每个按钮创建轴
  57.     ax_btn = plt.axes([0.1 + i*0.15, 0.01, 0.1, 0.04])
  58.     ax_button.append(ax_btn)
  59.    
  60.     # 创建按钮
  61.     btn = Button(ax_btn, category, color=axcolor, hovercolor='0.975')
  62.     buttons.append(btn)
  63.    
  64.     # 定义按钮点击函数
  65.     def make_click_func(line, idx):
  66.         def click(event):
  67.             line.set_visible(not line.get_visible())
  68.             fig.canvas.draw_idle()
  69.         return click
  70.    
  71.     btn.on_clicked(make_click_func(lines[i], i))
  72. plt.show()
复制代码

动画可视化

Matplotlib还支持创建动画,可以用来展示数据随时间的变化。
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. from matplotlib.animation import FuncAnimation
  5. from matplotlib.colors import Normalize
  6. from matplotlib.cm import ScalarMappable
  7. # 创建模拟数据
  8. np.random.seed(42)
  9. years = np.arange(2000, 2021)
  10. categories = ['电子产品', '服装', '食品', '家居', '图书']
  11. n = len(years)
  12. # 为每个类别创建随机销售额数据
  13. data = {'Year': years}
  14. for category in categories:
  15.     # 每个类别有不同的基础销售额和增长趋势
  16.     base_sales = np.random.randint(100, 300)
  17.     growth_rate = np.random.uniform(0.01, 0.05)
  18.     sales = [base_sales * (1 + growth_rate) ** i for i in range(n)]
  19.     # 添加一些随机波动
  20.     sales = sales * np.random.uniform(0.9, 1.1, n)
  21.     data[category] = sales
  22. df = pd.DataFrame(data)
  23. # 创建图表和轴
  24. fig, ax = plt.subplots(figsize=(12, 6))
  25. # 初始化条形图
  26. bars = ax.bar(categories, [0] * len(categories), color='skyblue')
  27. # 设置图表属性
  28. ax.set_title('产品销售额变化(2000-2020)', fontsize=16, fontweight='bold')
  29. ax.set_xlabel('产品类别')
  30. ax.set_ylabel('销售额(万元)')
  31. ax.set_ylim(0, df[categories].max().max() * 1.1)  # 设置y轴范围
  32. ax.grid(True, alpha=0.3)
  33. # 添加年份文本
  34. year_text = ax.text(0.02, 0.95, '', transform=ax.transAxes, fontsize=12,
  35.                    bbox=dict(facecolor='white', alpha=0.7))
  36. # 创建颜色映射
  37. norm = Normalize(vmin=0, vmax=len(years)-1)
  38. cmap = plt.cm.viridis
  39. sm = ScalarMappable(norm=norm, cmap=cmap)
  40. sm.set_array([])
  41. # 添加颜色条
  42. cbar = plt.colorbar(sm, ax=ax, ticks=np.arange(0, len(years), 5))
  43. cbar.set_ticklabels(years[::5])
  44. cbar.set_label('年份')
  45. # 定义动画更新函数
  46. def update(frame):
  47.     # 更新条形图高度
  48.     for bar, category in zip(bars, categories):
  49.         bar.set_height(df[category].iloc[frame])
  50.         # 根据年份设置颜色
  51.         bar.set_color(cmap(norm(frame)))
  52.    
  53.     # 更新年份文本
  54.     year_text.set_text(f'年份: {years[frame]}')
  55.    
  56.     return bars + [year_text]
  57. # 创建动画
  58. ani = FuncAnimation(fig, update, frames=len(years), interval=200, blit=True)
  59. plt.tight_layout()
  60. plt.show()
  61. # 如果需要保存动画为GIF,可以使用以下代码
  62. # ani.save('sales_animation.gif', writer='pillow', fps=5)
复制代码

最佳实践

数据准备与清洗

在使用Matplotlib和Pandas进行数据可视化之前,良好的数据准备和清洗是非常重要的。
  1. import pandas as pd
  2. import numpy as np
  3. # 创建包含缺失值和异常值的模拟数据
  4. np.random.seed(42)
  5. dates = pd.date_range(start='2022-01-01', end='2022-12-31', freq='D')
  6. n = len(dates)
  7. # 创建基础数据
  8. data = {
  9.     'Date': dates,
  10.     'Product_A': np.random.normal(100, 20, n),
  11.     'Product_B': np.random.normal(150, 30, n),
  12.     'Product_C': np.random.normal(200, 40, n)
  13. }
  14. df = pd.DataFrame(data)
  15. # 添加一些缺失值
  16. for _ in range(20):
  17.     df.loc[np.random.randint(0, n), np.random.choice(['Product_A', 'Product_B', 'Product_C'])] = np.nan
  18. # 添加一些异常值
  19. for _ in range(5):
  20.     df.loc[np.random.randint(0, n), np.random.choice(['Product_A', 'Product_B', 'Product_C'])] *= 5
  21. # 检查缺失值
  22. print("缺失值统计:")
  23. print(df.isnull().sum())
  24. # 处理缺失值 - 使用前向填充
  25. df_filled = df.fillna(method='ffill')
  26. # 如果还有缺失值(比如开头的数据),使用后向填充
  27. df_filled = df_filled.fillna(method='bfill')
  28. # 检查异常值 - 使用Z-score方法
  29. from scipy import stats
  30. z_scores = stats.zscore(df_filled[['Product_A', 'Product_B', 'Product_C']])
  31. abs_z_scores = np.abs(z_scores)
  32. filtered_entries = (abs_z_scores < 3).all(axis=1)
  33. df_cleaned = df_filled[filtered_entries]
  34. print(f"\n原始数据行数: {len(df)}")
  35. print(f"清洗后数据行数: {len(df_cleaned)}")
  36. print(f"移除的异常值行数: {len(df) - len(df_cleaned)}")
  37. # 添加一些有用的衍生列
  38. df_cleaned['Month'] = df_cleaned['Date'].dt.month
  39. df_cleaned['DayOfWeek'] = df_cleaned['Date'].dt.dayofweek
  40. df_cleaned['Total_Sales'] = df_cleaned['Product_A'] + df_cleaned['Product_B'] + df_cleaned['Product_C']
  41. # 按月份汇总数据
  42. monthly_data = df_cleaned.groupby('Month').agg({
  43.     'Product_A': 'mean',
  44.     'Product_B': 'mean',
  45.     'Product_C': 'mean',
  46.     'Total_Sales': 'mean'
  47. }).reset_index()
  48. print("\n月度平均销售额:")
  49. print(monthly_data)
复制代码

选择合适的图表类型

不同的数据类型和分析目的需要不同的图表类型。以下是一些常见的数据类型和适合的图表类型:

1. 时间序列数据:折线图、面积图
2. 分类数据比较:条形图、饼图
3. 分布数据:直方图、箱线图、密度图
4. 关系数据:散点图、气泡图
5. 组成数据:堆叠条形图、饼图
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. # 创建模拟数据
  5. np.random.seed(42)
  6. n = 1000
  7. data = {
  8.     'Age': np.random.randint(18, 80, n),
  9.     'Income': np.random.randint(2000, 20000, n),
  10.     'Spending': np.random.randint(1000, 15000, n),
  11.     'Category': np.random.choice(['A', 'B', 'C', 'D'], n),
  12.     'Satisfaction': np.random.randint(1, 6, n)
  13. }
  14. df = pd.DataFrame(data)
  15. # 1. 时间序列数据 - 折线图
  16. # 创建时间序列数据
  17. dates = pd.date_range(start='2022-01-01', end='2022-12-31', freq='D')
  18. ts_data = pd.DataFrame({
  19.     'Date': dates,
  20.     'Value': np.cumsum(np.random.normal(0, 1, len(dates))) + 100
  21. })
  22. plt.figure(figsize=(12, 6))
  23. plt.plot(ts_data['Date'], ts_data['Value'])
  24. plt.title('时间序列数据 - 折线图')
  25. plt.xlabel('日期')
  26. plt.ylabel('值')
  27. plt.grid(True, alpha=0.3)
  28. plt.tight_layout()
  29. plt.show()
  30. # 2. 分类数据比较 - 条形图
  31. category_counts = df['Category'].value_counts()
  32. plt.figure(figsize=(8, 6))
  33. category_counts.plot(kind='bar')
  34. plt.title('分类数据 - 条形图')
  35. plt.xlabel('类别')
  36. plt.ylabel('计数')
  37. plt.grid(True, alpha=0.3)
  38. plt.tight_layout()
  39. plt.show()
  40. # 3. 分布数据 - 直方图和箱线图
  41. fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
  42. # 直方图
  43. ax1.hist(df['Age'], bins=15, edgecolor='black', alpha=0.7)
  44. ax1.set_title('年龄分布 - 直方图')
  45. ax1.set_xlabel('年龄')
  46. ax1.set_ylabel('频数')
  47. ax1.grid(True, alpha=0.3)
  48. # 箱线图
  49. ax2.boxplot([df[df['Category'] == cat]['Income'] for cat in ['A', 'B', 'C', 'D']],
  50.             labels=['A', 'B', 'C', 'D'])
  51. ax2.set_title('各类别收入分布 - 箱线图')
  52. ax2.set_xlabel('类别')
  53. ax2.set_ylabel('收入')
  54. ax2.grid(True, alpha=0.3)
  55. plt.tight_layout()
  56. plt.show()
  57. # 4. 关系数据 - 散点图
  58. plt.figure(figsize=(8, 6))
  59. plt.scatter(df['Age'], df['Income'], alpha=0.5)
  60. plt.title('年龄与收入关系 - 散点图')
  61. plt.xlabel('年龄')
  62. plt.ylabel('收入')
  63. plt.grid(True, alpha=0.3)
  64. plt.tight_layout()
  65. plt.show()
  66. # 5. 组成数据 - 堆叠条形图
  67. # 创建组成数据
  68. composition_data = pd.DataFrame({
  69.     'Quarter': ['Q1', 'Q2', 'Q3', 'Q4'],
  70.     'Product_A': np.random.randint(100, 200, 4),
  71.     'Product_B': np.random.randint(150, 250, 4),
  72.     'Product_C': np.random.randint(50, 150, 4)
  73. })
  74. # 设置Quarter为索引
  75. composition_data.set_index('Quarter', inplace=True)
  76. plt.figure(figsize=(10, 6))
  77. composition_data.plot(kind='bar', stacked=True)
  78. plt.title('季度产品组成 - 堆叠条形图')
  79. plt.xlabel('季度')
  80. plt.ylabel('销售额')
  81. plt.legend(title='产品')
  82. plt.grid(True, alpha=0.3)
  83. plt.tight_layout()
  84. plt.show()
复制代码

图表美化与标注

良好的图表美化和标注可以使数据可视化更加清晰、专业。
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. from matplotlib.ticker import FuncFormatter
  5. # 创建模拟数据
  6. np.random.seed(42)
  7. years = np.arange(2015, 2023)
  8. categories = ['产品A', '产品B', '产品C', '产品D']
  9. n = len(years)
  10. # 创建数据
  11. data = {'Year': years}
  12. for category in categories:
  13.     base_sales = np.random.randint(100, 300)
  14.     growth_rate = np.random.uniform(0.05, 0.15)
  15.     sales = [base_sales * (1 + growth_rate) ** i for i in range(n)]
  16.     data[category] = sales
  17. df = pd.DataFrame(data)
  18. # 设置图表样式
  19. plt.style.use('seaborn')
  20. fig, ax = plt.subplots(figsize=(12, 8))
  21. # 绘制折线图
  22. for category in categories:
  23.     ax.plot(df['Year'], df[category], marker='o', markersize=8,
  24.             linewidth=2, label=category)
  25. # 设置标题和标签
  26. ax.set_title('产品销售趋势(2015-2022)', fontsize=16, fontweight='bold', pad=20)
  27. ax.set_xlabel('年份', fontsize=12, labelpad=10)
  28. ax.set_ylabel('销售额(万元)', fontsize=12, labelpad=10)
  29. # 设置刻度
  30. ax.set_xticks(df['Year'])
  31. ax.tick_params(axis='both', which='major', labelsize=10)
  32. # 格式化y轴刻度
  33. def millions_formatter(x, pos):
  34.     return f'{x/100:.1f}百'
  35. ax.yaxis.set_major_formatter(FuncFormatter(millions_formatter))
  36. # 添加网格
  37. ax.grid(True, linestyle='--', alpha=0.6)
  38. # 添加图例
  39. ax.legend(fontsize=12, frameon=True, fancybox=True, framealpha=0.8,
  40.           loc='upper left', bbox_to_anchor=(1, 1))
  41. # 添加数据标签
  42. for category in categories:
  43.     for i, value in enumerate(df[category]):
  44.         ax.annotate(f'{value:.0f}',
  45.                    xy=(df['Year'][i], value),
  46.                    xytext=(0, 5),  # 5 points vertical offset
  47.                    textcoords='offset points',
  48.                    ha='center', va='bottom',
  49.                    fontsize=9,
  50.                    bbox=dict(boxstyle='round,pad=0.3', fc='white', alpha=0.7))
  51. # 添加注释
  52. max_value = df[categories].max().max()
  53. max_year = df['Year'][df[categories].values.argmax() // len(categories)]
  54. max_category = categories[df[categories].values.argmax() % len(categories)]
  55. ax.annotate(f'最高销售额: {max_value:.0f}万元\n{max_year}年 {max_category}',
  56.             xy=(max_year, max_value),
  57.             xytext=(max_year-1, max_value-500),
  58.             arrowprops=dict(facecolor='black', shrink=0.05, width=1, headwidth=8),
  59.             bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.7),
  60.             fontsize=10, ha='center')
  61. # 添加平均线
  62. avg_values = df[categories].mean(axis=1)
  63. ax.plot(df['Year'], avg_values, linestyle='--', color='gray', alpha=0.7, label='平均值')
  64. ax.legend(fontsize=12, frameon=True, fancybox=True, framealpha=0.8,
  65.           loc='upper left', bbox_to_anchor=(1, 1))
  66. # 添加趋势线
  67. z = np.polyfit(df['Year'], df['Product_A'], 1)
  68. p = np.poly1d(z)
  69. ax.plot(df['Year'], p(df['Year']), linestyle=':', color='red', alpha=0.7, label='产品A趋势')
  70. ax.legend(fontsize=12, frameon=True, fancybox=True, framealpha=0.8,
  71.           loc='upper left', bbox_to_anchor=(1, 1))
  72. # 调整边距
  73. plt.tight_layout()
  74. plt.show()
复制代码

性能优化

当处理大量数据时,可视化可能会变得缓慢。以下是一些优化性能的技巧:
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. import time
  5. # 创建大量数据
  6. np.random.seed(42)
  7. n = 1000000  # 100万数据点
  8. x = np.random.randn(n)
  9. y = x * 0.5 + np.random.randn(n) * 0.5
  10. # 创建DataFrame
  11. df = pd.DataFrame({'X': x, 'Y': y})
  12. # 方法1:直接绘制所有数据(慢)
  13. start_time = time.time()
  14. plt.figure(figsize=(10, 6))
  15. plt.scatter(df['X'], df['Y'], alpha=0.1)
  16. plt.title('直接绘制所有数据点')
  17. print(f"直接绘制所有数据点耗时: {time.time() - start_time:.2f}秒")
  18. plt.show()
  19. # 方法2:使用数据采样(快)
  20. sample_size = 10000  # 采样1万个点
  21. df_sampled = df.sample(sample_size)
  22. start_time = time.time()
  23. plt.figure(figsize=(10, 6))
  24. plt.scatter(df_sampled['X'], df_sampled['Y'], alpha=0.5)
  25. plt.title('使用数据采样')
  26. print(f"使用数据采样耗时: {time.time() - start_time:.2f}秒")
  27. plt.show()
  28. # 方法3:使用2D直方图(快)
  29. start_time = time.time()
  30. plt.figure(figsize=(10, 6))
  31. plt.hist2d(df['X'], df['Y'], bins=50, cmap='viridis')
  32. plt.colorbar()
  33. plt.title('使用2D直方图')
  34. print(f"使用2D直方图耗时: {time.time() - start_time:.2f}秒")
  35. plt.show()
  36. # 方法4:使用六边形分箱图(快)
  37. start_time = time.time()
  38. plt.figure(figsize=(10, 6))
  39. plt.hexbin(df['X'], df['Y'], gridsize=50, cmap='viridis')
  40. plt.colorbar()
  41. plt.title('使用六边形分箱图')
  42. print(f"使用六边形分箱图耗时: {time.time() - start_time:.2f}秒")
  43. plt.show()
  44. # 方法5:使用数据聚合(快)
  45. # 创建网格
  46. x_bins = np.linspace(df['X'].min(), df['X'].max(), 50)
  47. y_bins = np.linspace(df['Y'].min(), df['Y'].max(), 50)
  48. # 计算每个网格中的点数
  49. x_idx = np.digitize(df['X'], x_bins)
  50. y_idx = np.digitize(df['Y'], y_bins)
  51. # 创建聚合数据
  52. grid_data = pd.DataFrame({'x_idx': x_idx, 'y_idx': y_idx})
  53. grid_counts = grid_data.groupby(['x_idx', 'y_idx']).size().reset_index(name='count')
  54. # 创建网格坐标
  55. grid_data['x_center'] = x_bins[grid_data['x_idx']-1] + (x_bins[1] - x_bins[0]) / 2
  56. grid_data['y_center'] = y_bins[grid_data['y_idx']-1] + (y_bins[1] - y_bins[0]) / 2
  57. # 合并计数
  58. grid_data = grid_data.merge(grid_counts, on=['x_idx', 'y_idx'], how='left').fillna(0)
  59. start_time = time.time()
  60. plt.figure(figsize=(10, 6))
  61. plt.scatter(grid_data['x_center'], grid_data['y_center'],
  62.             s=grid_data['count']/100, alpha=0.5, c=grid_data['count'], cmap='viridis')
  63. plt.colorbar()
  64. plt.title('使用数据聚合')
  65. print(f"使用数据聚合耗时: {time.time() - start_time:.2f}秒")
  66. plt.show()
复制代码

总结

Matplotlib与Pandas的集成为Python数据分析和可视化提供了强大的工具链。通过本文的介绍,我们了解了如何将这两个库有效地结合使用,从基础的数据处理到高级的可视化技巧。

主要要点包括:

1. 基础集成:Pandas内置了对Matplotlib的支持,可以直接使用DataFrame和Series的plot()方法进行基本绘图。
2. 灵活绘图:除了使用Pandas的内置绘图功能外,我们还可以直接使用Matplotlib来绘制Pandas数据,这提供了更多的自定义选项。
3. 实战应用:通过销售数据、股票数据和人口统计数据的分析案例,我们展示了如何在实际项目中应用这些工具。
4. 高级技巧:包括自定义图表样式、多子图布局、交互式可视化和动画可视化等高级技巧,可以帮助我们创建更加专业和吸引人的可视化效果。
5. 最佳实践:良好的数据准备与清洗、选择合适的图表类型、图表美化与标注以及性能优化等最佳实践,可以帮助我们提高数据可视化的质量和效率。

基础集成:Pandas内置了对Matplotlib的支持,可以直接使用DataFrame和Series的plot()方法进行基本绘图。

灵活绘图:除了使用Pandas的内置绘图功能外,我们还可以直接使用Matplotlib来绘制Pandas数据,这提供了更多的自定义选项。

实战应用:通过销售数据、股票数据和人口统计数据的分析案例,我们展示了如何在实际项目中应用这些工具。

高级技巧:包括自定义图表样式、多子图布局、交互式可视化和动画可视化等高级技巧,可以帮助我们创建更加专业和吸引人的可视化效果。

最佳实践:良好的数据准备与清洗、选择合适的图表类型、图表美化与标注以及性能优化等最佳实践,可以帮助我们提高数据可视化的质量和效率。

通过掌握Matplotlib与Pandas的集成应用,我们可以实现从数据处理到可视化展示的无缝对接,大大提高数据分析的效率和效果。希望本文能够帮助读者更好地利用这两大工具进行数据分析和可视化工作。
「七転び八起き(ななころびやおき)」
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

加入Discord频道

加入Discord频道

加入QQ社群

加入QQ社群

联系我们|小黑屋|TG频道|RSS |网站地图

Powered by Pixtech

© 2025-2026 Pixtech Team.