使用 Python 进行数据可视化

使用 Python 进行数据可视化

使用 Pandas 进行 Matplotlib 绘图

图表类型 描述 Pandas 函数 示例
折线图 显示随时间变化的趋势和变化 DataFrame.plot.line()DataFrame.plot(kind='line') df.plot(x='year', y='sales', kind='line')
面积图 显示数据系列之间的关系,以填充区域呈现 DataFrame.plot.area()DataFrame.plot(kind='area') df.plot(kind='area')
直方图 显示数据在各个区间(箱)内的频数分布 Series.plot.hist()Series.plot(kind='hist', bins=n) s.plot(kind='hist', bins=10)
条形图 通过矩形条形显示数据 DataFrame.plot.bar()DataFrame.plot(kind='bar') df.plot(kind='bar')
饼图 以圆形分片显示数据比例 Series.plot.pie()DataFrame.plot.pie(y, labels) df.plot(x='Category', y='Percentage', kind='pie')
箱型图 展示数据的分布及关键统计指标 DataFrame.plot.box()DataFrame.plot(kind='box') df_can.plot(kind='box')
散点图 通过笛卡尔坐标系展示两个变量的关系 DataFrame.plot.scatter()DataFrame.plot(x, y, kind='scatter') df.plot(x='Height', y='Weight', kind='scatter')

直接使用 Matplotlib 进行绘图

绘图类型 描述 Matplotlib 函数 示例
折线图 显示趋势和随时间变化的情况 plt.plot() plt.plot(x, y, color='red', linewidth=2)
区域图 显示填充区域的变化趋势 plt.fill_between() plt.fill_between(x, y1, y2, color='blue', alpha=0.5)
直方图 显示数据的频数分布 plt.hist() plt.hist(data, bins=10, color='orange', edgecolor='black')
条形图 通过矩形条展示数据 plt.bar() plt.bar(x, height, color='green', width=0.5)
饼图 显示数据的比例或百分比 plt.pie() plt.pie(sizes, labels=labels, colors=colors, explode=explode)
箱型图 显示数据的分布及统计特征 plt.boxplot() plt.boxplot(data, notch=True)
散点图 通过点显示两个变量之间的关系 plt.scatter() plt.scatter(x, y, color='purple', marker='o', s=50)
子图 在一个图形中创建多个子图 plt.subplots() fig, axes = plt.subplots(nrows=2, ncols=2)
自定义 自定义绘图(标题、标签、图例、网格) plt.title() plt.xlabel() plt.ylabel() plt.legend() plt.grid() plt.title('标题') plt.xlabel('X 轴') plt.ylabel('Y 轴') plt.legend() plt.grid(True)

这样整理后,内容更清晰,查找更方便!🚀

作者

Ethan Wilkins

发布于

2025-03-13

更新于

2025-03-13

许可协议

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

评论

You forgot to set the shortname for Disqus. Please set it in _config.yml.
You need to set client_id and slot_id to show this AD unit. Please set it in _config.yml.