我想创建一个仅包含一个表的Matplotlib图形,该表应类似于在Matlab中创建的图形:
我需要显示带有浮点数的列和带有“ Unidades”字符串的列。
我试图通过Matplotlib,Numpy,Pandas和Spyder在Python中实现这一目标。 注意:表格和数据与用Matlab制作的图片不同,但我需要该表格的结构相同。我仍然缺少表格的标题。
尝试了几次之后,在将浮点数和字符串合并到一个Numpy数组中进行绘图时遇到了问题,我想到了:
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()
但是这里的问题是它只显示每个元素的第一个字符
我遇到的另一个问题是,表已在具有行名的列中被切断,是否有一种格式化单元格的方式,以便它们适合几乎没有剩余空间的数据?
谢谢!
编辑1: 感谢@William Miller,我可以打印表格了。但是,当我使用这种方法制作相同的原始表(Matlab的一个表)时,我遇到了另一个问题,即原始问题中描述的是图形在具有rowLabels的列中被切割,这是完整原始表的代码: / p>
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()
Edit3:找到一个不太合适的解决方案。在图像框中,有一个按钮可以修改表格的位置(发布问题时我不知道该位置)。我在此对话框中修改值:
然后,我研究了Matplotlib Matplotlib adjust documentation,最后将这一行添加到了代码plt.subplots_adjust(top=0.97, bottom=0.08, left=0.205, right=0.98, hspace=0.20,wspace=0.20)
中,我不得不对此行fig.tight_layout()
进行注释以使更改生效。
这是最终结果:
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()
答案 0 :(得分:3)
我无法告诉您如何防止pandas
截断字符串,但是您可以通过将构造函数pd.DataFrame()
作为两个列表传递数据来解决它,如
pd.DataFrame({'Magnitud' : parametros.tolist(), 'Unidad' : unidades.tolist()})
这将截断数据框第二列中的字符串。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
fig, ax = plt.subplots(dpi=200)
# 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","$"])
df = pd.DataFrame({'Magnitud' : parametros.tolist(), 'Unidad' : unidades.tolist()})
ax.table(cellText=df.values, rowLabels=propiedades, colLabels=df.columns,
cellLoc='center', loc='center')
fig.tight_layout()
plt.show()