无法使用 MATPLOTLIB 从 EXCEL 中绘制多个数据
Unable to PLOT multiple data from EXCEL using MATPLOTLIB
我有一个包含 1000 行和 300 列的 Excel 文件。
我想绘制(第 1 列)与(第 2 至 288 列);我的第一列是我的 X 轴,其余列在 Y 轴上。
我的代码如下;我没有显示。
没有这样的错误消息。
from openpyxl import load_workbook
import numpy as np
import matplotlib.pyplot as plt
wb = load_workbook('CombinedData1.xlsx')
sheet_1 = wb.get_sheet_by_name('CombinedData')
x = np.zeros(sheet_1.max_row)
y = np.zeros(sheet_1.max_row)
a = np.zeros(sheet_1.max_column)
b = np.zeros(sheet_1.max_column)
print (sheet_1.max_row)
print (sheet_1.max_column)
for i in range(0, sheet_1.max_row):
for j in range(1, 7):
x[i] = sheet_1.cell(row=i + 1, column=j).value
y[j] = sheet_1.cell(row=i + 1, column=j).value
# z[i] = sheet_1.cell(row=i + 1, column=3).value
print x[i]
# print y[i]
plt.plot(x[i], y[i], 'bo-', label='Values')
plt.grid(True)
plt.xlim(0,100)
plt.ylim(0,10)
plt.show()
尝试类似下面的嵌套循环:
for j in range(1, 7):
for i in range(0, sheet_1.max_row):
x[i] = sheet_1.cell(row=i + 1, column=j).value
y[i] = sheet_1.cell(row=i + 1, column=j).value
#z[i] = sheet_1.cell(row=i + 1, column=3).value
print x[i]
#print y[i]
plt.plot(x, y, label='Values %d' % j, hold=1)
保留选项会将 7 个单独的地块合并为一个地块。此外,您应该考虑使用 pandas 包来导入和处理 sheet 数据。
我会考虑使用 pandas
:
import pandas
df = pandas.read_excel('CombinedData1.xlsx', sheetname='CombinedData', header=None)
df.plot(x=0)
或
plt.plot(df[0], df[1])
我有一个包含 1000 行和 300 列的 Excel 文件。 我想绘制(第 1 列)与(第 2 至 288 列);我的第一列是我的 X 轴,其余列在 Y 轴上。 我的代码如下;我没有显示。 没有这样的错误消息。
from openpyxl import load_workbook
import numpy as np
import matplotlib.pyplot as plt
wb = load_workbook('CombinedData1.xlsx')
sheet_1 = wb.get_sheet_by_name('CombinedData')
x = np.zeros(sheet_1.max_row)
y = np.zeros(sheet_1.max_row)
a = np.zeros(sheet_1.max_column)
b = np.zeros(sheet_1.max_column)
print (sheet_1.max_row)
print (sheet_1.max_column)
for i in range(0, sheet_1.max_row):
for j in range(1, 7):
x[i] = sheet_1.cell(row=i + 1, column=j).value
y[j] = sheet_1.cell(row=i + 1, column=j).value
# z[i] = sheet_1.cell(row=i + 1, column=3).value
print x[i]
# print y[i]
plt.plot(x[i], y[i], 'bo-', label='Values')
plt.grid(True)
plt.xlim(0,100)
plt.ylim(0,10)
plt.show()
尝试类似下面的嵌套循环:
for j in range(1, 7): for i in range(0, sheet_1.max_row): x[i] = sheet_1.cell(row=i + 1, column=j).value y[i] = sheet_1.cell(row=i + 1, column=j).value #z[i] = sheet_1.cell(row=i + 1, column=3).value print x[i] #print y[i] plt.plot(x, y, label='Values %d' % j, hold=1)
保留选项会将 7 个单独的地块合并为一个地块。此外,您应该考虑使用 pandas 包来导入和处理 sheet 数据。
我会考虑使用 pandas
:
import pandas
df = pandas.read_excel('CombinedData1.xlsx', sheetname='CombinedData', header=None)
df.plot(x=0)
或
plt.plot(df[0], df[1])