I want to create a Matplotlib figure with only a table in it, this table should be something like this created in Matlab:
I need to display columns with floats, and a column with strings for "Unidades".
I tried to make that in Python with Matplotlib, Numpy, Pandas and Spyder. Note: the table and data are not the same as the picture made with Matlab, but I need the same structure of that table.I'm still missing the title for the table.
After trying several times, having problems to merge floats and string into a Numpy array for plotting, I came up with this:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
# hide axes
fig.patch.set_visible(False)
ax.axis('off')
ax.axis('tight')
mlamb= 10.03724236064883
d= 0.48815610164066126 #[m]
e= 4.8815610164066126 #[mm]
v= 1.0495771928993183 #[m/s]
f= 0.04161227375782485
p1= 5.976000000000005 #[MPa]
X= 4.8459009928884695
N= 1529.742063492065 #[kW]
k3= 4.851121433427204 #[$/kW]
k03= 7420964.511821515 #[$]
parametros=np.around(np.array([mlamb,d,e,v,f,p1,X,N,k3,k03]),3)
string_magnitudes=str(parametros)
propiedades=np.array(["-lambda","Diametro","Espesor","Velocidad",
"Factor de friccion","Presion p1","Plantas de bombeo (X)"
,"Potencia","k3","k03"])
unidades=np.array(["-","m","mm","m/s","-","MPa","unidades","kW","$/kW","$"])
datos=np.zeros((10,2),str)
for r in range(0,len(parametros)):
datos[r,0]=parametros[r]
datos[r,1]=parametros[r]
df = pd.DataFrame(datos, columns=['Magnitud','Unidad'])
ax.table(cellText=df.values, rowLabels=propiedades, colLabels=df.columns, cellLoc='center', loc='center')
fig.tight_layout()
plt.show()
But the problem here is that it only display the first character of each element
Another issue that I had was that the table was cut off in the column with the rows names, is there a method to format the cells so they fit the data with no/little spare space?
Thanks!
Edit1: Thanks to @William Miller I could print the table. But when I use this method to make the same original table (the Matlab one) I have the other issue described in the original question that the graph get cut in the column with the rowLabels, here is the code for the complete original table:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
fig, ax = plt.subplots()
# hide axes
fig.patch.set_visible(False)
ax.axis('off')
ax.axis('tight')
parametros=np.array(["Diámetro comercial (dcom)","Espesor comercial (ecom)",
"Plantas de bombeo (X)","Factor de fricción (f)",
"Presión inicial (p1)",
"Variación de presión total (Delta Ptot)",
"Variación de presión entre plantas de bombeo (DeltaPx)",
"Presión final (p2)"])
unidades=np.array(['m','mm','-','-','MPa','MPa','MPa','MPa'])
datos_caso1=np.array([0.457, 5.6, 4, 0.04080925, 5.976, 36.67886619,
9.16971655, -3.19371655])
datos_caso2=np.array([0.457, 5.6, 5, 0.04080925, 5.976, 36.67886619,
7.33577324, -1.35977324])
datos_caso3=np.array([0.508, 5.6, 4, 0.04216189, 5.976, 23.7697998 ,
5.94244995, 0.03355005])
datos_caso4=np.array([0.508, 5.6, 5, 0.04216189, 5.976, 23.7697998 ,
4.75395996, 1.22204004])
df = pd.DataFrame({'Caso 1' : datos_caso1.tolist(),
'Caso 2' : datos_caso2.tolist(),
'Caso 3' : datos_caso3.tolist(),
'Caso 4' : datos_caso4.tolist(),
'Unidad' : unidades.tolist()})
ax.table(cellText=df.values, rowLabels=parametros, colLabels=df.columns, cellLoc='center', loc='center')
fig.tight_layout()
plt.show()
Edit2: I forgot to add the cut table image, here it is:
Edit3: Find a no so fancy solution. In the image box there is a button that allows to modify the positioning of the table (I didn't know that when I posted the question). I modify the values in this dialog box:
Then I looked into Matplotlib Matplotlib adjust documentation and finally added this line in the code plt.subplots_adjust(top=0.97, bottom=0.08, left=0.205, right=0.98, hspace=0.20,wspace=0.20) I had to comment this line fig.tight_layout() to make the changes work.
This is the final result:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
fig, ax = plt.subplots()
# hide axes
fig.patch.set_visible(False)
ax.axis('off')
ax.axis('tight')
parametros=np.array(["Diametro comercial (dcom)","Espesor comercial (ecom)",
"Plantas de bombeo (X)","Factor de friccion (f)",
"Presion inicial (p1)",
"Variacion de presion total (Delta Ptot)",
"Variacion de presion entre plantas de bombeo (DeltaPx)",
"Presion final (p2)"])
unidades=np.array(['m','mm','-','-','MPa','MPa','MPa','MPa'])
datos_caso1=np.array([0.457, 5.6, 4, 0.04080925, 5.976, 36.67886619,
9.16971655, -3.19371655])
datos_caso2=np.array([0.457, 5.6, 5, 0.04080925, 5.976, 36.67886619,
7.33577324, -1.35977324])
datos_caso3=np.array([0.508, 5.6, 4, 0.04216189, 5.976, 23.7697998 ,
5.94244995, 0.03355005])
datos_caso4=np.array([0.508, 5.6, 5, 0.04216189, 5.976, 23.7697998 ,
4.75395996, 1.22204004])
df = pd.DataFrame({'Caso 1' : datos_caso1.tolist(),
'Caso 2' : datos_caso2.tolist(),
'Caso 3' : datos_caso3.tolist(),
'Caso 4' : datos_caso4.tolist(),
'Unidad' : unidades.tolist()})
ax.table(cellText=df.values, rowLabels=parametros, colLabels=df.columns, cellLoc='center', loc='center')
plt.subplots_adjust(top=0.97, bottom=0.08, left=0.205, right=0.98, hspace=0.20,
wspace=0.20)
#fig.tight_layout()
plt.show()


