import pandas as pdimport matplotlibpyplot as plt# 读取Excel表数据data = pdread_excelEpythonProject5深度学习新建 XLS 工作表xls# 数据集统计statistics = datadescribe# 绘制数据分布图像dataplotkind=hist bins=10 alpha=05pltxlabelVal
这个错误是由于数据中包含日期类型的列,而Matplotlib对日期类型的处理有限制,日期必须在0001年至9999年之间。解决这个问题的方法是将日期列转换为字符串类型,然后再进行绘图。
以下是修改后的代码:
import pandas as pd
import matplotlib.pyplot as plt
# 读取Excel表数据
data = pd.read_excel('E:\pythonProject5\深度学习\新建 XLS 工作表.xls')
# 将日期列转换为字符串类型
data['Date'] = data['Date'].astype(str)
# 数据集统计
statistics = data.describe()
# 绘制数据分布图像
data.plot(kind='hist', bins=10, alpha=0.5)
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Data Distribution')
# 显示统计结果和图像
print(statistics)
plt.show()
在这个代码中,我们使用astype()函数将日期列转换为字符串类型,然后再进行绘图。这样就可以避免日期类型的限制问题。
原文地址: https://www.cveoy.top/t/topic/hDgu 著作权归作者所有。请勿转载和采集!