Python 读取 Excel 数据并绘制图表 - 示例代码
以下是使用 Python 读取 Excel 文档数据并绘制图表的示例代码:
import pandas as pd
import matplotlib.pyplot as plt
# 读取 Excel 文件
df = pd.read_excel('data.xlsx')
# 绘制柱状图
plt.bar(df['Month'], df['Sales'])
# 设置图表标题和坐标轴标签
plt.title('Monthly Sales')
plt.xlabel('Month')
plt.ylabel('Sales')
# 显示图表
plt.show()
在此示例中,我们使用 pandas 库的 read_excel 函数读取名为 'data.xlsx' 的 Excel 文件中的数据。然后,我们使用 matplotlib 库的 bar 函数绘制柱状图,x 轴为 'Month' 列,y 轴为 'Sales' 列。最后,我们使用 title、xlabel 和 ylabel 函数设置图表标题和坐标轴标签,并使用 show 函数显示图表。
原文地址: https://www.cveoy.top/t/topic/fZZZ 著作权归作者所有。请勿转载和采集!