将 numpy 数据导出到 csv 到 pgfplots 时出错

将 numpy 数据导出到 csv 到 pgfplots 时出错

我必须绘制数值模拟的数据。我使用和进行模拟pythonnumpy但现在我想将结果绘制在 LaTeX 文件中。由于我希望能够在不重新运行模拟的情况下修改布局,我认为将结果导出到文件中csv然后读取此文件pgfplots是一个很好的解决方案。不幸的是,当我这样做时,我得到的结果与使用matplotlib例如绘制时不同。

以下是我从中获得的信息matplotlibMatplotlib 结果

以下是我获得的结果pgfplotspgfplots 结果

pgfplots使用以下 LaTeX 代码生成结果:

\documentclass[12pt]{standalone} 
\usepackage[T1]{fontenc}
\usepackage{tikz}                       % 
\usepackage{pgfplots}                   %
    \pgfplotsset{compat=newest}         %


\begin{document}
\begin{tikzpicture} 
    \begin{axis}[colormap/viridis,
                enlarge y limits=false,
                axis equal image,
                axis on top,
                colorbar,
            ]           

        \addplot[ 
                matrix plot*, point meta=explicit, mesh/cols=10,
            ]
            table[x=y, y=x, 
                meta=T, ignore chars={\#}, col sep=comma,
                ] 
            {hydrostat.csv};

    \end{axis}  
\end{tikzpicture}
\end{document}

该文件hydrostat.csv可以通过这个链接获取: https://transfernow.net/103fg0a2hl44

numpy该文件是由as命令生成的np(其中T1是输出字段,XY是坐标,lyly是网格的尺寸):

np.savetxt('./hydrostat.csv', np.column_stack(( np.reshape(X, (lx*ly,1)), np.reshape(Y, (lx*ly,1)), np.reshape(T1, (lx*ly,1))  )), header='x, y, T', comments='# ',delimiter=',', newline='\n' )

我认为这不是什么大问题,只是一个错误的配置pgfplots,但我找不到它。

看起来行数和列数有点不对,所以我尝试更改mesh/cols值,但结果却很糟糕。我还尝试了从格式导出之类的操作matplotlibpgf但它会创建一个png图像,因此无法更改布局,除非重新运行模拟。 也发生了同样的情况matplotlib2tikz

提前感谢您帮助我找出错误。

答案1

简洁版本

感谢marmot的评论,我们可以确定部分问题来自文件中的原始数据csv

numpy/python可以使用以下命令导出数据来解决该问题T1

np.savetxt('./hydrostat.csv', np.column_stack(( np.reshape(X, (lx*ly,1)), np.reshape(Y, (lx*ly,1)), np.reshape(T1.T, (lx*ly,1))  )), header='x, y, T', comments='# ',delimiter=',', newline='\n' )

请注意 转置的数据的存在T1.T

最后一点是使用y dir=reverse获取想要的输出pgfplots

长版本

如果我使用numpy/python代码导出数据(不转置):

np.savetxt('./hydrostat.csv', np.column_stack(( np.reshape(X, (lx*ly,1)), np.reshape(Y, (lx*ly,1)),  np.reshape(T1, (lx*ly,1))  )), header='x, y, T', comments='# ',delimiter=',', newline='\n' )

hydrostat.csv我在问题链接中或 Ulrich 的回答中获得了以下文件。

然后使用以下 LaTeX 代码:

\documentclass[12pt]{standalone} 
\usepackage[T1]{fontenc}
\usepackage{tikz}                       % 
\usepackage{pgfplots}                   %
    \pgfplotsset{compat=newest}         %


\begin{document}
\begin{tikzpicture} 
    \begin{axis}[colormap/viridis,
                enlarge y limits=false,
                axis equal image,
                axis on top,
                colorbar,
                y dir=reverse,
            ]           

        \addplot[ 
                matrix plot*, point meta=explicit, mesh/cols=10,
            ]
            table[x expr=\thisrow{x}, y expr=\thisrow{y}, 
                            meta=T, ignore chars={\#}, col sep=comma,
                 ] 
            {hydrostat.csv};

    \end{axis}  
\end{tikzpicture}
\end{document}

我将获得结果图片:在此处输入图片描述

我加了一个红圈来突出这个问题。即使外径比是想要的倒数(这可以通过使用 Ulrich 的解决方案来解决),这个区域的数据也与图片不同matplotlib

然后如果我使用numpy/python代码(带有转置):

np.savetxt('./hydrostat.csv', np.column_stack(( np.reshape(X, (lx*ly,1)), np.reshape(Y, (lx*ly,1)),  np.reshape(T1.T, (lx*ly,1))  )), header='x, y, T', comments='# ',delimiter=',', newline='\n' )

我将获得该文件hydrostat.csv

# x, y, T
1.000000000000000000e+00,1.000000000000000000e+00,6.665274928193408721e-01
2.000000000000000000e+00,1.000000000000000000e+00,6.663971023015621276e-01
3.000000000000000000e+00,1.000000000000000000e+00,6.664437620670988771e-01
4.000000000000000000e+00,1.000000000000000000e+00,6.665301707977002721e-01
5.000000000000000000e+00,1.000000000000000000e+00,6.666191243753081253e-01
6.000000000000000000e+00,1.000000000000000000e+00,6.667140894072094426e-01
7.000000000000000000e+00,1.000000000000000000e+00,6.668030901761194951e-01
8.000000000000000000e+00,1.000000000000000000e+00,6.668895764592672748e-01
9.000000000000000000e+00,1.000000000000000000e+00,6.669363119300035780e-01
1.000000000000000000e+01,1.000000000000000000e+00,6.668059134476984617e-01
1.000000000000000000e+00,2.000000000000000000e+00,6.664984000034479550e-01
2.000000000000000000e+00,2.000000000000000000e+00,6.663563233698642785e-01
3.000000000000000000e+00,2.000000000000000000e+00,6.664214191117951991e-01
4.000000000000000000e+00,2.000000000000000000e+00,6.665131018225954884e-01
5.000000000000000000e+00,2.000000000000000000e+00,6.666140120616599329e-01
6.000000000000000000e+00,2.000000000000000000e+00,6.667192060591138336e-01
7.000000000000000000e+00,2.000000000000000000e+00,6.668201621659066713e-01
8.000000000000000000e+00,2.000000000000000000e+00,6.669119206097180452e-01
9.000000000000000000e+00,2.000000000000000000e+00,6.669770941226786931e-01
1.000000000000000000e+01,2.000000000000000000e+00,6.668350157237450393e-01
1.000000000000000000e+00,3.000000000000000000e+00,6.664854791213135066e-01
2.000000000000000000e+00,3.000000000000000000e+00,6.663415913719938910e-01
3.000000000000000000e+00,3.000000000000000000e+00,6.664222063383633543e-01
4.000000000000000000e+00,3.000000000000000000e+00,6.665140801477354993e-01
5.000000000000000000e+00,3.000000000000000000e+00,6.666148761882257912e-01
6.000000000000000000e+00,3.000000000000000000e+00,6.667183640856011451e-01
7.000000000000000000e+00,3.000000000000000000e+00,6.668191978934161490e-01
8.000000000000000000e+00,3.000000000000000000e+00,6.669111359852449850e-01
9.000000000000000000e+00,3.000000000000000000e+00,6.669918243907704269e-01
1.000000000000000000e+01,3.000000000000000000e+00,6.668479455078759610e-01
1.000000000000000000e+00,4.000000000000000000e+00,6.664815006196560532e-01
2.000000000000000000e+00,4.000000000000000000e+00,6.663330228538590916e-01
3.000000000000000000e+00,4.000000000000000000e+00,6.664169300798881146e-01
4.000000000000000000e+00,4.000000000000000000e+00,6.665119589458776694e-01
5.000000000000000000e+00,4.000000000000000000e+00,6.666143422671383378e-01
6.000000000000000000e+00,4.000000000000000000e+00,6.667189113521675425e-01
7.000000000000000000e+00,4.000000000000000000e+00,6.668213283281555492e-01
8.000000000000000000e+00,4.000000000000000000e+00,6.669164154775901743e-01
9.000000000000000000e+00,4.000000000000000000e+00,6.670003945605174067e-01
1.000000000000000000e+01,4.000000000000000000e+00,6.668519314279444110e-01
1.000000000000000000e+00,5.000000000000000000e+00,6.664770000644588688e-01
2.000000000000000000e+00,5.000000000000000000e+00,6.663251033169389492e-01
3.000000000000000000e+00,5.000000000000000000e+00,6.664111506008371100e-01
4.000000000000000000e+00,5.000000000000000000e+00,6.665088727699410853e-01
5.000000000000000000e+00,5.000000000000000000e+00,6.666133511753929985e-01
6.000000000000000000e+00,5.000000000000000000e+00,6.667199070068153821e-01
7.000000000000000000e+00,5.000000000000000000e+00,6.668244175028308351e-01
8.000000000000000000e+00,5.000000000000000000e+00,6.669221964566964811e-01
9.000000000000000000e+00,5.000000000000000000e+00,6.670083158660570222e-01
1.000000000000000000e+01,5.000000000000000000e+00,6.668564335409302712e-01
1.000000000000000000e+00,6.000000000000000000e+00,6.664730997869784401e-01
2.000000000000000000e+00,6.000000000000000000e+00,6.663180845314861100e-01
3.000000000000000000e+00,6.000000000000000000e+00,6.664054966081143228e-01
4.000000000000000000e+00,6.000000000000000000e+00,6.665054530423857315e-01
5.000000000000000000e+00,6.000000000000000000e+00,6.666122244948706754e-01
6.000000000000000000e+00,6.000000000000000000e+00,6.667210326937413889e-01
7.000000000000000000e+00,6.000000000000000000e+00,6.668278363313814294e-01
8.000000000000000000e+00,6.000000000000000000e+00,6.669278502659713448e-01
9.000000000000000000e+00,6.000000000000000000e+00,6.670153355264032413e-01
1.000000000000000000e+01,6.000000000000000000e+00,6.668603341518074545e-01
1.000000000000000000e+00,7.000000000000000000e+00,6.664697463550552925e-01
2.000000000000000000e+00,7.000000000000000000e+00,6.663120196443859111e-01
3.000000000000000000e+00,7.000000000000000000e+00,6.664004262050575722e-01
4.000000000000000000e+00,7.000000000000000000e+00,6.665021952648533254e-01
5.000000000000000000e+00,7.000000000000000000e+00,6.666111123890067214e-01
6.000000000000000000e+00,7.000000000000000000e+00,6.667221413104018612e-01
7.000000000000000000e+00,7.000000000000000000e+00,6.668310914276506240e-01
8.000000000000000000e+00,7.000000000000000000e+00,6.669329195149436007e-01
9.000000000000000000e+00,7.000000000000000000e+00,6.670214007442626380e-01
1.000000000000000000e+01,7.000000000000000000e+00,6.668636878941709423e-01
1.000000000000000000e+00,8.000000000000000000e+00,6.664671553565268969e-01
2.000000000000000000e+00,8.000000000000000000e+00,6.663072476998228577e-01
3.000000000000000000e+00,8.000000000000000000e+00,6.663962705305201961e-01
4.000000000000000000e+00,8.000000000000000000e+00,6.664994244361608366e-01
5.000000000000000000e+00,8.000000000000000000e+00,6.666101447363657062e-01
6.000000000000000000e+00,8.000000000000000000e+00,6.667231048960070572e-01
7.000000000000000000e+00,8.000000000000000000e+00,6.668338591735474274e-01
8.000000000000000000e+00,8.000000000000000000e+00,6.669370737822977180e-01
9.000000000000000000e+00,8.000000000000000000e+00,6.670261728290619585e-01
1.000000000000000000e+01,8.000000000000000000e+00,6.668662792153110530e-01
1.000000000000000000e+00,9.000000000000000000e+00,6.664654603829183177e-01
2.000000000000000000e+00,9.000000000000000000e+00,6.663040583623929258e-01
3.000000000000000000e+00,9.000000000000000000e+00,6.663933804084154477e-01
4.000000000000000000e+00,9.000000000000000000e+00,6.664974447369693689e-01
5.000000000000000000e+00,9.000000000000000000e+00,6.666094420860504410e-01
6.000000000000000000e+00,9.000000000000000000e+00,6.667238041750613853e-01
7.000000000000000000e+00,9.000000000000000000e+00,6.668358363404600642e-01
8.000000000000000000e+00,9.000000000000000000e+00,6.669399627501244598e-01
9.000000000000000000e+00,9.000000000000000000e+00,6.670293622727493377e-01
1.000000000000000000e+01,9.000000000000000000e+00,6.668679744632736162e-01
1.000000000000000000e+00,1.000000000000000000e+01,6.664646443897686012e-01
2.000000000000000000e+00,1.000000000000000000e+01,6.663024890447618587e-01
3.000000000000000000e+00,1.000000000000000000e+01,6.663919082297593555e-01
4.000000000000000000e+00,1.000000000000000000e+01,6.664964158667143757e-01
5.000000000000000000e+00,1.000000000000000000e+01,6.666090728974014556e-01
6.000000000000000000e+00,1.000000000000000000e+01,6.667241714870277836e-01
7.000000000000000000e+00,1.000000000000000000e+01,6.668368638159816175e-01
8.000000000000000000e+00,1.000000000000000000e+01,6.669414343107787913e-01
9.000000000000000000e+00,1.000000000000000000e+01,6.670309316686268142e-01
1.000000000000000000e+01,1.000000000000000000e+01,6.668687906109909136e-01
1.000000000000000000e+00,1.100000000000000000e+01,6.664646443897686012e-01
2.000000000000000000e+00,1.100000000000000000e+01,6.663024890447619697e-01
3.000000000000000000e+00,1.100000000000000000e+01,6.663919082297594665e-01
4.000000000000000000e+00,1.100000000000000000e+01,6.664964158667143757e-01
5.000000000000000000e+00,1.100000000000000000e+01,6.666090728974012336e-01
6.000000000000000000e+00,1.100000000000000000e+01,6.667241714870280056e-01
7.000000000000000000e+00,1.100000000000000000e+01,6.668368638159817285e-01
8.000000000000000000e+00,1.100000000000000000e+01,6.669414343107786802e-01
9.000000000000000000e+00,1.100000000000000000e+01,6.670309316686269252e-01
1.000000000000000000e+01,1.100000000000000000e+01,6.668687906109908026e-01
1.000000000000000000e+00,1.200000000000000000e+01,6.664654603829183177e-01
2.000000000000000000e+00,1.200000000000000000e+01,6.663040583623929258e-01
3.000000000000000000e+00,1.200000000000000000e+01,6.663933804084154477e-01
4.000000000000000000e+00,1.200000000000000000e+01,6.664974447369693689e-01
5.000000000000000000e+00,1.200000000000000000e+01,6.666094420860504410e-01
6.000000000000000000e+00,1.200000000000000000e+01,6.667238041750613853e-01
7.000000000000000000e+00,1.200000000000000000e+01,6.668358363404600642e-01
8.000000000000000000e+00,1.200000000000000000e+01,6.669399627501244598e-01
9.000000000000000000e+00,1.200000000000000000e+01,6.670293622727493377e-01
1.000000000000000000e+01,1.200000000000000000e+01,6.668679744632737272e-01
1.000000000000000000e+00,1.300000000000000000e+01,6.664671553565266748e-01
2.000000000000000000e+00,1.300000000000000000e+01,6.663072476998227467e-01
3.000000000000000000e+00,1.300000000000000000e+01,6.663962705305200851e-01
4.000000000000000000e+00,1.300000000000000000e+01,6.664994244361608366e-01
5.000000000000000000e+00,1.300000000000000000e+01,6.666101447363657062e-01
6.000000000000000000e+00,1.300000000000000000e+01,6.667231048960070572e-01
7.000000000000000000e+00,1.300000000000000000e+01,6.668338591735474274e-01
8.000000000000000000e+00,1.300000000000000000e+01,6.669370737822977180e-01
9.000000000000000000e+00,1.300000000000000000e+01,6.670261728290620695e-01
1.000000000000000000e+01,1.300000000000000000e+01,6.668662792153110530e-01
1.000000000000000000e+00,1.400000000000000000e+01,6.664697463550550705e-01
2.000000000000000000e+00,1.400000000000000000e+01,6.663120196443860221e-01
3.000000000000000000e+00,1.400000000000000000e+01,6.664004262050573502e-01
4.000000000000000000e+00,1.400000000000000000e+01,6.665021952648533254e-01
5.000000000000000000e+00,1.400000000000000000e+01,6.666111123890067214e-01
6.000000000000000000e+00,1.400000000000000000e+01,6.667221413104018612e-01
7.000000000000000000e+00,1.400000000000000000e+01,6.668310914276506240e-01
8.000000000000000000e+00,1.400000000000000000e+01,6.669329195149436007e-01
9.000000000000000000e+00,1.400000000000000000e+01,6.670214007442625270e-01
1.000000000000000000e+01,1.400000000000000000e+01,6.668636878941707202e-01
1.000000000000000000e+00,1.500000000000000000e+01,6.664730997869783291e-01
2.000000000000000000e+00,1.500000000000000000e+01,6.663180845314862211e-01
3.000000000000000000e+00,1.500000000000000000e+01,6.664054966081143228e-01
4.000000000000000000e+00,1.500000000000000000e+01,6.665054530423855095e-01
5.000000000000000000e+00,1.500000000000000000e+01,6.666122244948704534e-01
6.000000000000000000e+00,1.500000000000000000e+01,6.667210326937413889e-01
7.000000000000000000e+00,1.500000000000000000e+01,6.668278363313813184e-01
8.000000000000000000e+00,1.500000000000000000e+01,6.669278502659714558e-01
9.000000000000000000e+00,1.500000000000000000e+01,6.670153355264032413e-01
1.000000000000000000e+01,1.500000000000000000e+01,6.668603341518071215e-01
1.000000000000000000e+00,1.600000000000000000e+01,6.664770000644588688e-01
2.000000000000000000e+00,1.600000000000000000e+01,6.663251033169390602e-01
3.000000000000000000e+00,1.600000000000000000e+01,6.664111506008372210e-01
4.000000000000000000e+00,1.600000000000000000e+01,6.665088727699408633e-01
5.000000000000000000e+00,1.600000000000000000e+01,6.666133511753928875e-01
6.000000000000000000e+00,1.600000000000000000e+01,6.667199070068152711e-01
7.000000000000000000e+00,1.600000000000000000e+01,6.668244175028308351e-01
8.000000000000000000e+00,1.600000000000000000e+01,6.669221964566965921e-01
9.000000000000000000e+00,1.600000000000000000e+01,6.670083158660571332e-01
1.000000000000000000e+01,1.600000000000000000e+01,6.668564335409303823e-01
1.000000000000000000e+00,1.700000000000000000e+01,6.664815006196560532e-01
2.000000000000000000e+00,1.700000000000000000e+01,6.663330228538593136e-01
3.000000000000000000e+00,1.700000000000000000e+01,6.664169300798881146e-01
4.000000000000000000e+00,1.700000000000000000e+01,6.665119589458776694e-01
5.000000000000000000e+00,1.700000000000000000e+01,6.666143422671384489e-01
6.000000000000000000e+00,1.700000000000000000e+01,6.667189113521674315e-01
7.000000000000000000e+00,1.700000000000000000e+01,6.668213283281555492e-01
8.000000000000000000e+00,1.700000000000000000e+01,6.669164154775902853e-01
9.000000000000000000e+00,1.700000000000000000e+01,6.670003945605175177e-01
1.000000000000000000e+01,1.700000000000000000e+01,6.668519314279444110e-01
1.000000000000000000e+00,1.800000000000000000e+01,6.664854791213135066e-01
2.000000000000000000e+00,1.800000000000000000e+01,6.663415913719941130e-01
3.000000000000000000e+00,1.800000000000000000e+01,6.664222063383633543e-01
4.000000000000000000e+00,1.800000000000000000e+01,6.665140801477353882e-01
5.000000000000000000e+00,1.800000000000000000e+01,6.666148761882257912e-01
6.000000000000000000e+00,1.800000000000000000e+01,6.667183640856010340e-01
7.000000000000000000e+00,1.800000000000000000e+01,6.668191978934161490e-01
8.000000000000000000e+00,1.800000000000000000e+01,6.669111359852450960e-01
9.000000000000000000e+00,1.800000000000000000e+01,6.669918243907705380e-01
1.000000000000000000e+01,1.800000000000000000e+01,6.668479455078759610e-01
1.000000000000000000e+00,1.900000000000000000e+01,6.664984000034481770e-01
2.000000000000000000e+00,1.900000000000000000e+01,6.663563233698643895e-01
3.000000000000000000e+00,1.900000000000000000e+01,6.664214191117953101e-01
4.000000000000000000e+00,1.900000000000000000e+01,6.665131018225954884e-01
5.000000000000000000e+00,1.900000000000000000e+01,6.666140120616598219e-01
6.000000000000000000e+00,1.900000000000000000e+01,6.667192060591137226e-01
7.000000000000000000e+00,1.900000000000000000e+01,6.668201621659067824e-01
8.000000000000000000e+00,1.900000000000000000e+01,6.669119206097181562e-01
9.000000000000000000e+00,1.900000000000000000e+01,6.669770941226791372e-01
1.000000000000000000e+01,1.900000000000000000e+01,6.668350157237451503e-01
1.000000000000000000e+00,2.000000000000000000e+01,6.665274928193409831e-01
2.000000000000000000e+00,2.000000000000000000e+01,6.663971023015621276e-01
3.000000000000000000e+00,2.000000000000000000e+01,6.664437620670989881e-01
4.000000000000000000e+00,2.000000000000000000e+01,6.665301707977003831e-01
5.000000000000000000e+00,2.000000000000000000e+01,6.666191243753081253e-01
6.000000000000000000e+00,2.000000000000000000e+01,6.667140894072093316e-01
7.000000000000000000e+00,2.000000000000000000e+01,6.668030901761193840e-01
8.000000000000000000e+00,2.000000000000000000e+01,6.668895764592674968e-01
9.000000000000000000e+00,2.000000000000000000e+01,6.669363119300038001e-01
1.000000000000000000e+01,2.000000000000000000e+01,6.668059134476987948e-01

然后使用 LaTeX 代码:

\documentclass[12pt]{standalone} 
\usepackage[T1]{fontenc}
\usepackage{tikz}                       % 
\usepackage{pgfplots}                   %
    \pgfplotsset{compat=newest}         %


\begin{document}
\begin{tikzpicture} 
    \begin{axis}[colormap/viridis,
                enlarge y limits=false,
                axis equal image,
                axis on top,
                colorbar,
                y dir=reverse,
            ]           

        \addplot[ 
                matrix plot*, point meta=explicit, mesh/cols=10,
            ]
            table[x expr=\thisrow{y}, y expr=\thisrow{x}, 
                            meta=T, ignore chars={\#}, col sep=comma,
                 ] 
            {hydrostat.csv};

    \end{axis}  
\end{tikzpicture}
\end{document}

我终于得到了想要的结果:在此处输入图片描述

相关内容