numpy 데이터를 csv로 pgfplots로 내보낼 때 실수

numpy 데이터를 csv로 pgfplots로 내보낼 때 실수

수치 시뮬레이션을 통해 데이터를 플롯해야 합니다. 나는 python및 을 사용하여 시뮬레이션을 수행합니다 numpy. 하지만 이제 LaTeX 파일에 결과를 표시하고 싶습니다. 시뮬레이션을 다시 실행하지 않고도 레이아웃을 수정할 수 있기를 원하므로 결과를 파일로 내보낸 csv다음 이 파일을 읽는 것이 pgfplots좋은 해결책이라고 생각합니다. 불행히도 이 작업을 수행하면 matplotlib예를 들어 플롯할 때와는 다른 결과를 얻게 됩니다.

내가 얻은 것은 다음과 같습니다 matplotlib. Matplotlib 결과

그리고 내가 얻은 것은 다음과 같습니다 pgfplots. pgfplots 결과

결과 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는 출력 필드 X이고 Y는 좌표이며 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값을 변경해 보았으나 결과가 최악이었습니다. 또한 형식 matplotlib으로 내보내기와 같은 작업을 시도 pgf하지만 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}

마침내 원하는 결과를 얻었습니다.여기에 이미지 설명을 입력하세요

관련 정보