3

I have to plot data from a numerical simulation. I perform my simulations with python and numpy. But now I want to plot the result in a LaTeX file. Since I want to be able to modify the layout without rerun the simulation, I think that exporting my results in a csv file then read this file in pgfplots is a good solution. Unfortunately, when I do this I obtain different results than when I plot with matplotlib for example.

Here is what I obtain from matplotlib: Matplotlib result

And here is what I obtain with pgfplots: pgfplots result

The pgfplots result has been generated with the following LaTeX code:

\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}

And the file hydrostat.csv can be obtained through this link: https://transfernow.net/103fg0a2hl44

This file has been generated from the numpy as np command (where T1 is output field, X and Y are the coordinates, ly and ly are the dimensions of the mesh):

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' )

I think it is not a big deal, just a wrong configuration of pgfplots, but I can not find it.

It looks like there a miss number of rows and columns, so I tried to change mesh/cols values but the results where worst. I also try stuff like export from matplotlib in pgf format but it creates a png image so there no way to change to layout without rerun the simulation. The same is happening with matplotlib2tikz.

Thank you in advance for helping to find my mistake.

  • 1
    Does adding colormap/viridis to the options of the axis help? – user121799 Mar 16 at 16:37
  • My problem is not about the colormap, but about the organization of the results. Look around the line 5 for example you go from yellowish to blue and line 6 from blue to yellowish. This is not comparable with the line or column 5 of matplotlib results. I thinks that line 5 and 6 from pgfplots should in fact be only one. – R. N Mar 16 at 16:48
  • I had a quick look at the data file. The y values run all the way up to 20. This is consistent with the plot produced by pgfplots but not at all with your first plot unless you exchange the roles of x and y there. Is that what's going on? – user121799 Mar 16 at 16:59
  • I think this is part of the problem. But I switch the value of x and y in the reading of the table with the command : table[x=y, y=x,... with or without change of the mesh/rows value, and the problem mention remains. – R. N Mar 16 at 17:16
  • 1
    To me it looks as if you have transposed the data for some reason. That is, I fail to see that pgfplots is the culprit. BTW, the code formatting got destroyed in your edit. – user121799 Mar 16 at 17:28
1

Short version

Thanks to the comments of marmot, we could determine that part of the problem comes from the original data in the csv file.

The problem can be solved by using the numpy/python command to export the data 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' )

Please note the presence of the data transposed with the T1.T.

The last point is to use y dir=reverse to obtain the wanted output with pgfplots.

Long version

If I use the numpy/python code to export the data (without transpose):

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' )

I obtain the following file hydrostat.csv present in the question link or in the answer of Ulrich.

Then by using the following LaTeX code:

\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}

I will obtain the resulting picture:enter image description here

I added a red circle to highlight the problem. Even if the apsect ration is the inverse of the one wanted (this can be solve by using the solution of Ulrich), in this area data are different from the matplotlib picture.

Then if I use the numpy/python code (with the transpose):

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' )

I will obtain the file 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

Then by using the LaTeX code :

\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}

I finally obtain the wanted result:enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.