尝试使用Matplotlib从Mathematica-Wolfram获取的文本文件绘制一些数据时遇到问题。 代码如下:
import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import matplotlib.artist as pltart
params = {'font.size': 11,
'axes.labelsize' : 18, 'axes.titlesize' : 18,
'xtick.labelsize' : 18, 'ytick.labelsize' : 18,
'figure.figsize' : [6.0,4.4],
'figure.subplot.left' : 0.13, 'figure.subplot.right' : 0.999,
'figure.subplot.bottom' : 0.16, 'figure.subplot.top' : 0.999}
plt.rcParams.update(params)
x=np.arange(7,24,0.1)
M3=0.47e-3*x**3
plt.figure()
RhoS,MS,RS,=np.loadtxt('Static.pnr',usecols=(0,1,2),unpack=True)
# MtotVsRtot plot
plt.plot(RS,MS,'k-',color='red',linewidth=2.0)
plt.plot(x,M3,'k-.',color='grey',linewidth=1.5)
plt.xlabel(r'$R_{\rm eq}\,(\rm km)$',fontsize=20)
plt.ylabel(r'$M/M_{\odot}$', fontsize=20)
#plt.show()
plt.savefig('MvsR_L&P_GM1.eps')
#exit()
使用以下命令从mathematica获取文本文件:
Static = Table[{KepSeq[[i, 1]],
KepSeq[[i, 2]], KepSeq[[i, 3]]}, {i, 1,
Length[KepSeq]}] // TableForm
Export["Static", Static, "Table"]
其中KepSeq[[i,j]]
只是一个值矩阵。
看来,即使Mathematica正确生成三列文本文件,Matplotlib也无法识别这些列,实际上,使用手工构建的文本文件进行测试,我没有遇到任何问题。
当然,使用.txt文件无法解决错误。
错误,从标题开始是:IndexError: list index out of range.
编辑:
除了做静态= KepSeq [[All,1 ;; 3]]。你研究过这个文件以确定它是全数字的吗?
我注意到mathematica创建的表只由数字组成。但是,在一个空的.txt文件中“复制并粘贴”它会显示很多隐藏的字符,如paranthesis,逗号和其他:
\!\(\*
TagBox[GridBox[{
{"19.30736436824425`", "0.49704178850444225`"},
...
{"11.327870685578393`", "2.199907624212163`"}
},
GridBoxAlignment->{
"Columns" -> {{Left}}, "ColumnsIndexed" -> {},
"Rows" -> {{Baseline}}, "RowsIndexed" -> {}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[2.0999999999999996`]},
Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> {
Offset[0.2], {
Offset[0.4]},
Offset[0.2]}, "RowsIndexed" -> {}}],
Function[BoxForm`e$,
TableForm[BoxForm`e$]]]\)
关于如何摆脱它的任何想法?同时,(明显不满意)的解决方案已被复制并过去,然后手工删除多余的字符。