ToB企服应用市场:ToB评测及商务社交产业平台

标题: Python可视化 | 利用matplotlib绘制面积图示例 [打印本页]

作者: 徐锦洪    时间: 2024-6-11 08:59
标题: Python可视化 | 利用matplotlib绘制面积图示例
面积图是数据可视化中的一个有效工具,用于阐明时间上的关系和趋势。它们提供了一种全面的、视觉上迷人的方法,通过纯熟地将折线图的可读性与填充地域的吸引力相结合来呈现数值数据。
在本文中,我们将学习更多关于在Python中创建面积折线图的知识。面积图为数据可视化提供了一个有价值的工具,提供了一种清晰而引人入胜的方式来传达随着时间的推移的趋势和关系。
什么是面积图?

面积线图,也称为面积图或堆积面积图,是一种数据可视化技术,用于表现随时间或跨种别的数据。它是根本折线图的扩展,当您想要表现整体的组成、沿着单个组件以及它们如何随时间或跨种别变化时,它特殊有效。在本文中,我们将探索如何利用matplotlib库在Python中创建面积线图,并解释它们在可视化数据中的重要性。
以下是面积线图的关键组成部分和特性。
X轴:程度轴代表自变量,通常是时间或种别。它是一种连续或分类量表,为数据点提供背景。
Y轴:垂直轴表现因变量,通常是一个数值,用于度量您正在可视化的内容的数目或大小。
线:面积线图中的各条线表现不同的种别、组或构件。每一行从基线(通常是X轴)开始,向上表现该种别或组件在特定时间点或沿着种别轴的值。
地域填充:线条和基线之间的地域用颜色填充,使其在视觉上与众不同。该地域的颜色通常用于表现它所代表的种别或组件。
堆叠:在堆叠面积图中,每条线都堆叠在前一条线的顶部。这种叠加阐明了总体如何随时间或跨种别变化,以及每个种别对整体的贡献。
创建面积图

起首,让我们利用Python制作一个根本的面积线图。为了创建图并表现各种种别如何随时间变化,我们将利用Matplotlib。
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. # Sample data
  4. df = pd.DataFrame({
  5.         'x': list(range(1, 11)),
  6.         'y': [1, 3, 2, 4, 5, 7, 6, 8, 9, 10]
  7. })
  8. # Create the area line plot
  9. plt.fill_between(df['x'], df['y'], color='blue', alpha=0.2)
  10. plt.plot(df['x'], df['y'], color='red', alpha=0.5, linewidth=0.9)
  11. plt.title("Area Line Plot")
  12. plt.xlabel("X-axis")
  13. plt.ylabel("Y-axis")
  14. plt.show()
复制代码

带标记和标签的面积图

添加更多功能,使其更具吸引力

  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. # Sample data
  4. df = pd.DataFrame({
  5.         'x': list(range(1, 11)),
  6.         'y': [1, 3, 2, 4, 5, 7, 6, 8, 9, 10]
  7. })
  8. # Create the area line plot
  9. plt.fill_between(df['x'], df['y'], color='blue', alpha=0.5)
  10. plt.plot(df['x'], df['y'], color='red', alpha=0.1)
  11. # Add red markers at data points
  12. plt.scatter(df['x'], df['y'], color='red', s=30)
  13. # Add labels above data points
  14. for i, row in df.iterrows():
  15.         plt.text(row['x'], row['y'], str(row['y']), ha='center', va='bottom', color='black', size=10)
  16. plt.title("Area Line Plot with Markers and Labels")
  17. plt.xlabel("X-axis")
  18. plt.ylabel("Y-axis")
  19. plt.show()
复制代码

堆叠面积图


  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. # Sample data
  4. df = pd.DataFrame({
  5.         'x': list(range(1, 11)),
  6.         'Category A': [1, 3, 2, 4, 5, 7, 6, 8, 9, 10],
  7.         'Category B': [2, 4, 3, 5, 6, 8, 7, 9, 10, 11],
  8.         'Category C': [3, 5, 4, 6, 7, 9, 8, 10, 11, 12]
  9. })
  10. # Define custom colors for each category
  11. colors = ['yellow', 'purple', 'pink']
  12. # Create the stacked area line plot with custom colors
  13. plt.stackplot(df['x'], df['Category A'], df['Category B'], df['Category C'], colors=colors, alpha=0.7)
  14. # Plot lines for each category with custom colors
  15. plt.plot(df['x'], df['Category A'], color='blue', alpha=0.5, linewidth=0.9)
  16. plt.plot(df['x'], df['Category B'], color='green', alpha=0.5, linewidth=0.9)
  17. plt.plot(df['x'], df['Category C'], color='red', alpha=0.5, linewidth=0.9)
  18. plt.title("Stacked Area Line Plot with Custom Colors")
  19. plt.xlabel("X-axis")
  20. plt.ylabel("Y-axis")
  21. plt.legend()
  22. plt.show()
复制代码

线间填充


  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. # Sample data for demonstration
  4. x = np.linspace(0, 10, 100)
  5. y1 = np.sin(x)
  6. y2 = np.cos(x)
  7. # Create a figure and axis
  8. fig, ax = plt.subplots()
  9. # Plot the two lines
  10. ax.plot(x, y1, label='Line 1', color='blue')
  11. ax.plot(x, y2, label='Line 2', color='green')
  12. # Fill the area between the lines
  13. ax.fill_between(x, y1, y2, where=(y1 > y2), interpolate=True, alpha=0.5, color='yellow', label='Fill Area')
  14. # Customize the plot
  15. ax.set_xlabel('X-axis')
  16. ax.set_ylabel('Y-axis')
  17. ax.set_title('Filling Between Lines')
  18. ax.legend()
  19. # Display the plot
  20. plt.show()
复制代码

总结

总而言之,面积图可有效表现数据随时间或跨种别的趋势、比较和部分与整体的关系。它们提供了一种视觉上引人注目的方式来理解不同的组件如何对整体做出贡献,以及这些贡献如何在选定的轴(时间或种别)上发生变化。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4