I clarify that I had never used openpyxel before (in case I screw up ... XD). Using Openpyxl
, NumPy and matplotlib:
from itertools import cycle
from openpyxl import load_workbook
import numpy as np
import matplotlib.pyplot as plt
#cargamos el xlsx en memoria
wb = load_workbook('datos.xlsx', data_only=True)
#cargamos la hoja
sheet1 = wb.get_sheet_by_name('Hoja1')
#creamos la matriz en numpy
matriz = np.zeros((sheet1.max_row, sheet1.max_column))
#leemos la hoja fila por fila y vamos guardando los valores 'x' e 'y'
for i in range(0,sheet1.max_row):
for j in range(0,sheet1.max_column):
matriz[i,j]=sheet1.cell(row=i+1, column=j+1).value
#creamos el grafico
colors = cycle(["aqua", "black", "blue", "fuchsia", "gray", "green", "lime", "maroon", "navy", "olive", "purple", "red", "silver", "teal", "yellow"])
plt.xlabel('tiempo')
plt.ylabel('intensidad')
for y in range(1, sheet1.max_column):
plt.plot(matriz[:,0],matriz[:,y], label="Data " + str(y), color=next(colors))
plt.legend(loc='upper left', fontsize='small')
plt.grid(True)
plt.xlim(0,70)
plt.ylim(0,70)
plt.title('Grafica tiempo/intensidad')
plt.show()
I have tried it with a document (which I have called data.xlsx) made in LibreOffice and saved with .xlsx Excel format because I do not have excel [^ _ ^] at hand. The document has 4 columns (1 with values of x and 3 with values of y) with invented data:
0 2 4 6
1 4 8 12
2 6 12 18
3 8 16 24
4 10 20 30
5 12 24 36
6 14 28 42
7 16 32 48
8 18 36 54
9 20 40 60
10 22 44 66
11 24 48 72
12 26 52 78
13 28 56 84
14 30 60 90
15 32 64 96
16 34 68 102
17 36 72 108
18 38 76 114
19 40 80 120
20 42 84 126
21 44 88 132
22 46 92 138
23 48 96 144
24 50 100 150
25 52 104 156
26 54 108 162
27 56 112 168
28 58 116 174
29 60 120 180
30 62 124 196
With this and using Python 3.5.2 I get this ugly graphic:
It's just an example that I hope will help you. I at least have discovered openpyxl, in case in the future I need something like that it never hurts:)
P.D: to load the corresponding sheet on the line
sheet1 = wb.get_sheet_by_name('Hoja1')
We need the name of the sheet (Sheet1 in my case). You can get the name of all xlsx sheets in the following way:
print(wb.get_sheet_names())
Modified to clarify doubt expressed in comments:
If in the first row we have labels that show that they contain each column and we want them to appear in the legend of the graph, it will be enough to start reading from the second row the data and read the sheet corresponding to the name of each sheet. column passing it as a 'label' attribute to
matplotlib.pyplot
For example for some data of the form:
t Datos 1 Datos 2 Datos 3
0 2 4 6
1 4 8 12
2 6 12 18
3 8 16 24
The code below makes the graph using as 'Data 1', 'Data 2' and 'Data 3' labels:
from itertools import cycle
from openpyxl import load_workbook
import numpy as np
import matplotlib.pyplot as plt
#cargamos el xlsx en memoria
wb = load_workbook('datos.xlsx', data_only=True)
#cargamos la hoja
sheet1 = wb.get_sheet_by_name('Hoja1')
#creamos dos arreglos en numpy para guardar las 'x' y las 'y'
matriz = np.zeros((sheet1.max_row, sheet1.max_column))
#leemos la hoja columna por columna y vamos guardando los valores 'x' e 'y'
for i in range(1,sheet1.max_row):
for j in range(0,sheet1.max_column):
matriz[i,j]=sheet1.cell(row=i+1, column=j+1).value
#creamos el grafico
colors = cycle(["aqua", "black", "blue", "fuchsia", "gray", "green", "lime", "maroon", "navy", "olive", "purple", "red", "silver", "teal", "yellow"])
plt.xlabel('tiempo')
plt.ylabel('intensidad')
for y in range(1, sheet1.max_column):
plt.plot(matriz[:,0],matriz[:,y], label=sheet1.cell(row=1, column=y+1).value,color=next(colors))
plt.legend(loc='upper left', fontsize='small')
plt.grid(True)
plt.xlim(0,70)
plt.ylim(0,70)
plt.title('Grafica tiempo/intensidad')
plt.show()