的网站开发工具,厦门企业建站系统模板,经典模板网站建设,中国宁波网首页Pandas 数据可视化指南#xff1a;从散点图到面积图的全面展示
本文介绍了使用 Pandas 进行数据可视化的多种方法#xff0c;包括散点图、折线图、条形图、直方图、饼图和面积图等#xff0c;涵盖了常见的图表类型及其实现方式。通过提供详细的代码示例#xff0c;展示了如…Pandas 数据可视化指南从散点图到面积图的全面展示
本文介绍了使用 Pandas 进行数据可视化的多种方法包括散点图、折线图、条形图、直方图、饼图和面积图等涵盖了常见的图表类型及其实现方式。通过提供详细的代码示例展示了如何使用 Pandas 和 Matplotlib 快速创建不同类型的图表帮助读者轻松掌握数据可视化技术。这篇指南既适合初学者也为有经验的开发者提供了一些实用技巧帮助在数据分析中更直观地展示结果。 文章目录 Pandas 数据可视化指南从散点图到面积图的全面展示一 散点图Scatter二 折线图Plot简单折线图多折线图 三 条形图Bar垂直条形图堆叠条形图水平条形图 四 直方图Hist简单直方图重叠直方图 五 饼图Pie简单饼图多个饼图 六 面积图Area堆叠面积图同起点面积图 七 完整代码示例八 源码地址 导入库
在开始绘制图表之前我们首先导入必要的库
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt一 散点图Scatter n 1024 # 数据量
# 创建数据框
df pd.DataFrame({x: np.random.normal(0, 1, n),y: np.random.normal(0, 1, n),
})
# 使用 arctan2 函数计算颜色
color np.arctan2(df[y], df[x])
# 绘制散点图
df.plot.scatter(xx, yy, ccolor, s60, alpha0.5, cmaprainbow)二 折线图Plot
简单折线图 n 20 # 数据量
x np.linspace(-1, 1, n)
y x * 2 0.4 np.random.normal(0, 0.3, n)
# 创建数据框
df pd.DataFrame({x: x,y: y,
})
# 绘制折线图
df.plot(xx, yy, alpha0.5, cr)多折线图 n 20 # 数据量
x np.linspace(-1, 1, n)
y1 x * -1 - 0.1 np.random.normal(0, 0.3, n)
y2 x * 2 0.4 np.random.normal(0, 0.3, n)
# 创建数据框
df pd.DataFrame({x: x,y1: y1,y2: y2,
})
# 绘制多折线图
df.plot(xx, y[y1, y2], alpha0.5)三 条形图Bar
垂直条形图 df pd.DataFrame(np.random.rand(5, 3), columns[a, b, c])df.plot.bar()堆叠条形图 df pd.DataFrame(np.random.rand(5, 3), columns[a, b, c])df.plot.bar(stackedTrue)水平条形图 df pd.DataFrame(np.random.rand(5, 3), columns[a, b, c])df.plot.barh()四 直方图Hist
简单直方图 df pd.DataFrame({a: np.random.randn(1000)})
df.plot.hist()重叠直方图 df pd.DataFrame({a: np.random.randn(1000) 1,b: np.random.randn(1000),c: np.random.randn(1000) - 4,}
)df.plot.hist(alpha0.5, bins30)
五 饼图Pie
简单饼图 df pd.DataFrame({boss: np.random.rand(4)},index[meeting, supervise, teaching, team building],)df.plot.pie(yboss, figsize(7, 7))多个饼图 df pd.DataFrame({bigBoss: np.random.rand(4),smallBoss: np.random.rand(4),},index[meeting, supervise, teaching, team building],
)
df.plot.pie(subplotsTrue, figsize(9, 9), legendFalse)六 面积图Area
堆叠面积图 #
df pd.DataFrame(np.random.rand(10, 4),columns[a, b, c, d]
)
df.plot.area()同起点面积图 #
df pd.DataFrame(np.random.rand(10, 4),columns[a, b, c, d]
)
df.plot.area(stackedFalse)详情见官方文档Pandas 可视化图表
七 完整代码示例
# This is a sample Python script.# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
import numpy as np
import pandas as pd
import matplotlib.pyplot as pltdef print_hi(name):# Use a breakpoint in the code line below to debug your script.print(fHi, {name}) # Press ⌘F8 to toggle the breakpoint.# 散点图Scattern 1024 # data sizedf pd.DataFrame({x: np.random.normal(0, 1, n),y: np.random.normal(0, 1, n),})color np.arctan2(df[y], df[x])df.plot.scatter(xx, yy, ccolor, s60, alpha.5, cmaprainbow)# 折线图Plotn 20 # data sizex np.linspace(-1, 1, n)y x * 2 0.4 np.random.normal(0, 0.3, n)df pd.DataFrame({x: x,y: y,})df.plot(xx, yy, alpha.5, cr)n 20 # data sizex np.linspace(-1, 1, n)y1 x * -1 - 0.1 np.random.normal(0, 0.3, n)y2 x * 2 0.4 np.random.normal(0, 0.3, n)df pd.DataFrame({x: x,y1: y1,y2: y2,})df.plot(xx, y[y1, y2], alpha.5)# 条形图Bardf pd.DataFrame(np.random.rand(5, 3), columns[a, b, c])df.plot.bar()df.plot.bar(stackedTrue)df.plot.barh()# 分布图Histdf pd.DataFrame({a: np.random.randn(1000)})df.plot.hist()df pd.DataFrame({a: np.random.randn(1000) 1,b: np.random.randn(1000),c: np.random.randn(1000) - 4,})df.plot.hist(alpha0.5, bins30)# 饼图Piedf pd.DataFrame({boss: np.random.rand(4)},index[meeting, supervise, teaching, team building],)df.plot.pie(yboss, figsize(7, 7))df pd.DataFrame({bigBoss: np.random.rand(4),smallBoss: np.random.rand(4),},index[meeting, supervise, teaching, team building],)df.plot.pie(subplotsTrue, figsize(9, 9), legendFalse)# 面积图Areadf pd.DataFrame(np.random.rand(10, 4),columns[a, b, c, d])df.plot.area()plt.show()df.plot.area(stackedFalse)plt.show()# https://pandas.pydata.org/pandas-docs/stable/user_guide/visualization.html# Press the green button in the gutter to run the script.
if __name__ __main__:print_hi(绘制图表)# See PyCharm help at https://www.jetbrains.com/help/pycharm/
复制粘贴并覆盖到你的 main.py 中运行运行结果如下。
Hi, 绘制图表八 源码地址
代码地址
国内看 Gitee 之 pandas/绘制图表.py
国外看 GitHub 之 pandas/绘制图表.py
引用 莫烦 Python