python读取excel并绘图的代码
import openpyxl
import matplotlib.pyplot as plt
wb = openpyxl.load_workbook('渗透记录表(1).xlsx') # 载入excel工作簿
a = wb.get_sheet_names() # 获取工作簿中所有工作表的名字
st = wb.get_sheet_by_name('Sheet1') # 获取某张工作表的内容
d = st['B1'].value # 获取某个单元格的值
rows = st.max_row # 获取行数
cols = st.max_column # 获取列数
e = st['C1'].column
# print(e)
# # 对行进行遍历,输出A1,B1,C1
# for row in st.rows:
# for cell in row:
# print(cell.value)
#
# # 对列进行遍历,输出A1,A2,A3
# for column in st.columns:
# for cell in column:
# print(cell.value)
# 对某一特定的行进行遍历
# for cell in list(st.rows)[1]:
# print(cell.value)
# 对某一特定的列进行遍历
# for cell in list(st.columns)[1]:
# print(cell.value)
# 对某一单元格范围进行遍历
# for spaces in st['B2':'B11']:
# for cell in spaces:
# print(cell.value)
y = list()
x = list()
for spaces in st['B2':'B11']:
for cell in spaces:
y.append(int(cell.value))
for spaces in st['A2':'A11']:
for cell in spaces:
x.append(cell.value)
plt.plot(x, y)
plt.xticks(x, color='green', rotation=60) # 改变x轴标签的颜色和倾斜角度
plt.show()