Python 读取 Excel 数据并绘制动态图表 - 使用 Pandas 和 Matplotlib
以下是一个示例代码,使用 Python 的 pandas 和 matplotlib 库来读取 Excel 数据和绘制图表:
import pandas as pd
import matplotlib.pyplot as plt
# 读取 Excel 数据
df = pd.read_excel('data.xlsx')
# 绘制图表
plt.plot(df['x'], df['y'])
plt.title('Data')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
# 更换另外一组数据
df2 = pd.read_excel('data2.xlsx')
# 刷新图表
plt.clf()
plt.plot(df2['x'], df2['y'])
plt.title('New Data')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
其中,'data.xlsx' 和 'data2.xlsx' 是两个 Excel 文件,包含两组数据,每组数据有两列:'x' 和 'y'。首先使用 pandas 的 read_excel 函数读取第一组数据,然后使用 matplotlib 的 plot 函数绘制图表。接着,读取第二组数据,并使用 matplotlib 的 clf 函数清除之前的图表,然后再次使用 plot 函数绘制新的图表。最后调用 show 函数显示图表。
原文地址: https://www.cveoy.top/t/topic/oy0v 著作权归作者所有。请勿转载和采集!