Skip to content

Commit

Permalink
Add matlab2tikz improved alternative showcase using pgfplots
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Mar 26, 2021
1 parent 5f8dc71 commit 0910bca
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 5 deletions.
4 changes: 4 additions & 0 deletions bib/glossaries/index/names.bib
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ @index{richard_mollier
name={Mollier},
firstname={Richard},
}
@index{osborne_reynolds,
name={Reynolds},
firstname={Osborne},
}
8 changes: 8 additions & 0 deletions bib/glossaries/symbols/greek.bib
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ @symbol{relative_humidity
unit={\si{\degreeCelsius}},
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fluid Mechanics
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@symbol{drag_coefficient,
name={\ensuremath{\zeta}},
description={Drag Coefficient},
unit={\si{-}},
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Other
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@symbol{mass_fraction,
Expand Down
5 changes: 5 additions & 0 deletions bib/glossaries/symbols/roman.bib
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ @symbol{mach_number
description={\glsfmtname{name.ernst_mach} number},
unit={\si{-}},
}
@symbol{reynolds_number,
name={\ensuremath{\operatorname{Re}}},
description={\glsfmtname{name.osborne_reynolds} number},
unit={\si{-}},
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Other
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down
98 changes: 93 additions & 5 deletions chapters/mainmatter/floats.tex
Original file line number Diff line number Diff line change
Expand Up @@ -1330,11 +1330,11 @@ \subsubsection{Importing CSV}
\subsubsection{Using MATLAB2Ti\textit{k}Z}

\href{https://github.com/matlab2tikz/matlab2tikz}{MATLAB2Ti\textit{k}Z} is a tool to
convert MATLAB figures to \LaTeX{}, see \cref{fig:matlab2tikz}.
convert MATLAB figures to \LaTeX{}, see \cref{fig:matlab2tikz_vanilla}.
Notice that by default, it exports using whitespace (tabs) as column separators,
which might differ from what you set as a global option in \verb|\pgfplotstableset|.
which might differ from what is set as a global option in \verb|\pgfplotstableset|.

\begin{figure}
\begin{figure}\ContinuedFloat*
\ffigbox[\FBwidth]{%
\caption[%
A vanilla \href{https://github.com/matlab2tikz/matlab2tikz}{MATLAB2Ti\textit{k}Z}
Expand All @@ -1347,15 +1347,103 @@ \subsubsection{Using MATLAB2Ti\textit{k}Z}
and you miss out on many useful features, like \ctanpackage{siunitx} or
\ctanpackage{glossaries-extra}.
A more frictionless approach is to export plain-text (CSV) data from MATLAB,
and import it into \LaTeX{}, see \cref{fig:diffuser}%
and import it into \LaTeX{}, see \cref{fig:diffuser,fig:matlab2tikz_pgfplots}%
}%
\label{fig:matlab2tikz}%
\label{fig:matlab2tikz_vanilla}%
}{%
% Can't use `subimport', since import is not from any child directory.
% Note that changing plot scaling/size for matlab2tikz is apparently very
% awkward: https://tex.stackexchange.com/a/173490/120853
\import{data/}{matlab2tikz_table_example}
}
\end{figure}

\Cref{fig:matlab2tikz_pgfplots} is the same plot, but generated directly in \LaTeX{}, without
relying on MATLAB2Ti\textit{k}Z---using the latter, a plethora of things break since it
hard\-/codes a bunch of stuff.
\Cref{fig:matlab2tikz_pgfplots} is only around 30 lines of code (arrived at after only a couple of hours\dots{})!

\begin{figure}
\ffigbox[\FBwidth]{
\caption[%
Same data as \cref{fig:matlab2tikz_vanilla} but plotted in \LaTeX{} directly, using only the provided raw data%
]{%
Same data as \cref{fig:matlab2tikz_vanilla} but plotted in \LaTeX{} directly, using only the provided raw data.
Note how this allows for a consistent style and unlocks all other, usual features found in this template.
Also take a look at the source data to see what \emph{tidy data} looks like, a data format most suitable for data processing%
}
\label{fig:matlab2tikz_pgfplots}
}{%
\pgfplotstableread{data/matlab2tikz_table_example_data.csv}{\matlabtikztable}%
\begin{tikzpicture}
\begin{axis}[%
regularplot,%
width=0.9\linewidth,
%
localstyle/.style={% Common, local plot style options
only marks,% Scatter plot, do not connect marks
error bars/y dir=both,% Y errors in both directions, positive and negative
error bars/y explicit,% Absolute, not relative, values
},
% Modify x labels, drop scientific notation, see also
% https://tex.stackexchange.com/a/119888/120853
xticklabel style={
/pgf/number format/fixed,
/pgf/number format/precision=5
},
scaled x ticks=false,
%
xlabel={\glsdesc{sym.reynolds_number} \sym{reynolds_number}},%
x unit={\glsunit{sym.reynolds_number}},
ylabel={\glsdesc{sym.drag_coefficient} \sym{drag_coefficient}},%
y unit={\glsunit{sym.drag_coefficient}},
% Table namespace, options for all tables:
table/y=Drag_Coefficient,
% Data sources for the requested error bars:
table/y error plus=Drag_Coefficient_Error_Pos,
table/y error minus=Drag_Coefficient_Error_Neg,
% We're going to filter using NaN a lot, don't spam logs:
filter discard warning=false,
]%
% We essentially have this same issue:
% https://tex.stackexchange.com/questions/264330/grouping-data-in-pgfplots
%
% Trying to group well-structured data by some other columns.
% It's not currently possible, but the below is a decent enough
% surrogate:
% Generate plots in a loop; pretty neat and automated with one
% core downside: we can't just say `groupby(pressure, temperature)`
% as we could in e.g. pandas, Excel PowerPivot and many others.
% That's inconvenient but it's understandable pgfplots/tex (whose
% job is to *plot*, not to mangle and process data) can't
% deliver this.
% Note how grouping data this way is AMAZING and dearly recommended
% for any serious data work:
% https://vita.had.co.nz/papers/tidy-data.pdf
\foreach\pressure/\temperature in {
9/30,%
13.2/45,%
16.6/55%
}{
\addplot+ [
localstyle,
table/x expr={
% Ternary filter: https://tex.stackexchange.com/a/412235/120853
\thisrow{Pressure}==\pressure?\thisrow{Reynolds}:nan
},
] table {\matlabtikztable};
% Expansion in `foreach` is problematic with legend printing;
% use this special command version, see also
% https://tex.stackexchange.com/a/317015/120853
\addlegendentryexpanded{
\SI{\pressure}{\bar}/\SI{\temperature}{\degreeCelsius}
}
}
\end{axis}%
\end{tikzpicture}
}
\end{figure}

\subsection{TikZ and Text}

\idx{tikz} content can also be intertwined with text using \verb|\tikzmark|.
Expand Down
86 changes: 86 additions & 0 deletions data/matlab2tikz_table_example_data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
Pressure, Temperature, Reynolds, Drag_Coefficient, Drag_Coefficient_Error_Pos, Drag_Coefficient_Error_Neg
9, 30, 91450.4005348642, 18.8206494302446, 0.22784420833039, 0.22784420833039
9, 30, 9600.2287009917, 17.0940855567971, 18.8769483727188, 18.8769483727188
9, 30, 98040.7874556726, 18.6728583096816, 0.202036909770865, 0.202036909770865
9, 30, 65157.2349669312, 19.3095953696594, 0.418204806276939, 0.418204806276939
9, 30, 49337.9741089259, 19.8795961310237, 0.722764642682136, 0.722764642682136
9, 30, 16355.6858822063, 21.6588058718839, 6.48504129977849, 6.48504129977849
9, 30, 68662.1156767816, 19.2826158073702, 0.379906394244728, 0.379906394244728
9, 30, 88193.8884388812, 18.8512612583498, 0.241587510229639, 0.241587510229639
9, 30, 52897.2052503459, 19.7556610691345, 0.63682805169426, 0.63682805169426
9, 30, 45361.2310463829, 19.9283864286216, 0.857322986465614, 0.857322986465614
9, 30, 35629.1264772871, 20.5353610928944, 1.37414627818398, 1.37414627818398
9, 30, 95043.2938482082, 18.7591297797428, 0.213416328531058, 0.213416328531058
9, 30, 61640.0699375336, 19.465583058119, 0.466703246588861, 0.466703246588861
9, 30, 58751.4523099524, 19.4914842217088, 0.508777380354382, 0.508777380354382
9, 30, 41886.8624922781, 20.2213805881069, 0.988945957346638, 0.988945957346638
9, 30, 71787.1923690876, 19.183171510498, 0.349958881481435, 0.349958881481435
9, 30, 55566.6189222481, 19.6884300185046, 0.57492765151384, 0.57492765151384
9, 30, 22414.4905749956, 21.4563894028588, 3.46477796770842, 3.46477796770842
9, 30, 29891.5116045859, 20.8072061681644, 1.95317002720734, 1.95317002720734
9, 30, 81648.5561586976, 18.9338775855205, 0.276308105964505, 0.276308105964505
9, 30, 84915.3205825358, 18.9018965006065, 0.25775055811768, 0.25775055811768
9, 30, 12748.6992906755, 20.956252098279, 10.6964468685791, 10.6964468685791
9, 30, 19702.7655801537, 21.3720690437585, 4.47489311485873, 4.47489311485873
9, 30, 78363.5688516276, 19.0751370068433, 0.297546198952902, 0.297546198952902
9, 30, 32704.8269868436, 20.8291059476584, 1.63441062956786, 1.63441062956786
9, 30, 39454.0447791576, 20.3956969160794, 1.11850790152941, 1.11850790152941
9, 30, 25852.5857440217, 21.5131319639883, 2.60715319679087, 2.60715319679087
9, 30, 75047.302268985, 19.1129078534408, 0.322186799342343, 0.322186799342343
13.2, 45, 90364.8194858743, 18.851972315629, 0.305217342375055, 0.305217342375055
13.2, 45, 8076.2640578434, 24.0111558434839, 35.8029501848227, 35.8029501848227
13.2, 45, 50772.0591562781, 19.7992917932574, 0.928394801097195, 0.928394801097195
13.2, 45, 66195.4322434412, 19.2914810641625, 0.550235869227531, 0.550235869227531
13.2, 45, 47363.6661097263, 20.0685366715685, 1.0630574120011, 1.0630574120011
13.2, 45, 19902.7764508919, 21.2759580135189, 5.98521694141482, 5.98521694141482
13.2, 45, 39900.5151261104, 20.5397185970455, 1.48849529409474, 1.48849529409474
13.2, 45, 74866.732832787, 19.0900594677447, 0.434436734327329, 0.434436734327329
13.2, 45, 82545.9819399738, 18.9439559950128, 0.361002096005894, 0.361002096005894
13.2, 45, 106171.719883028, 18.6283535384433, 0.229496342278619, 0.229496342278619
13.2, 45, 94122.014260214, 18.7599655960001, 0.282619972807026, 0.282619972807026
13.2, 45, 55431.0307804117, 19.61223734667, 0.783444980800781, 0.783444980800781
13.2, 45, 59322.7541290298, 19.6319108649163, 0.684931628377185, 0.684931628377185
13.2, 45, 114221.038645558, 18.4694576373584, 0.203347977101083, 0.203347977101083
13.2, 45, 78925.5166005358, 19.0282740070502, 0.393135093952, 0.393135093952
13.2, 45, 63096.2993854333, 19.3936301169532, 0.605948000756784, 0.605948000756784
13.2, 45, 86358.0944477848, 18.8993234493532, 0.331341638288088, 0.331341638288088
13.2, 45, 110454.377590452, 18.567036984956, 0.214660611248231, 0.214660611248231
13.2, 45, 35864.0452240558, 20.7795087552391, 1.84052287954811, 1.84052287954811
13.2, 45, 102270.885599624, 18.6534024509903, 0.244662856478561, 0.244662856478561
13.2, 45, 15625.3491197509, 23.0212986293325, 9.68121139016299, 9.68121139016299
13.2, 45, 98275.2673711873, 18.6797685109276, 0.262281703155193, 0.262281703155193
13.2, 45, 31352.5742431042, 21.0124223239215, 2.41763424066977, 2.41763424066977
13.2, 45, 12313.9582502865, 22.5336653922509, 15.5170405633573, 15.5170405633573
13.2, 45, 118521.184105338, 18.4634946805162, 0.191715543444072, 0.191715543444072
13.2, 45, 27954.591413931, 21.1733665963443, 3.03909219686198, 3.03909219686198
13.2, 45, 23661.526874057, 21.8398598857805, 4.23960949267591, 4.23960949267591
13.2, 45, 70936.4289641694, 19.2203716274935, 0.481640104330054, 0.481640104330054
13.2, 45, 43616.6098039867, 20.3724031756756, 1.24623498410928, 1.24623498410928
16.6, 55, 17867.5515609687, 21.7652508894877, 9.15368581910848, 9.15368581910848
16.6, 55, 125637.055996345, 18.3877470989716, 0.20747930927834, 0.20747930927834
16.6, 55, 49304.731241175, 19.807149989706, 1.22247182825836, 1.22247182825836
16.6, 55, 107667.483438579, 18.6074812032667, 0.27078355428531, 0.27078355428531
16.6, 55, 13536.3513622991, 22.9216117473262, 16.0120267998002, 16.0120267998002
16.6, 55, 71994.5089139876, 19.1461599074966, 0.579982264504498, 0.579982264504498
16.6, 55, 58949.4992802703, 19.4584247735011, 0.863655495599455, 0.863655495599455
16.6, 55, 22441.0358182932, 20.9345419939241, 5.80047673858736, 5.80047673858736
16.6, 55, 44296.5080074942, 19.8307916472566, 1.50683347198268, 1.50683347198268
16.6, 55, 36163.6188854124, 20.1076548679491, 2.265078120627, 2.265078120627
16.6, 55, 121252.135326159, 18.4363699283627, 0.220566362796363, 0.220566362796363
16.6, 55, 85072.0354168332, 18.8324850607434, 0.418963400171204, 0.418963400171204
16.6, 55, 103030.125538593, 18.6109968334098, 0.292673058569925, 0.292673058569925
16.6, 55, 62664.4453756571, 19.3182930613143, 0.76166308494462, 0.76166308494462
16.6, 55, 66814.802347003, 19.1511266386389, 0.671621933741905, 0.671621933741905
16.6, 55, 40023.6860323227, 20.3563108994464, 1.84868057936682, 1.84868057936682
16.6, 55, 98428.0786967929, 18.7052786521598, 0.318212576297495, 0.318212576297495
16.6, 55, 94101.3387888276, 18.7365240146046, 0.345945890970535, 0.345945890970535
16.6, 55, 112249.405136342, 18.541593643746, 0.2517944419942, 0.2517944419942
16.6, 55, 31272.4427821611, 20.5767680935139, 3.01988954371356, 3.01988954371356
16.6, 55, 76452.8638484087, 19.0625190030148, 0.518185514149112, 0.518185514149112
16.6, 55, 53939.2603758079, 19.6676345378916, 1.02315173365127, 1.02315173365127
16.6, 55, 130240.937716863, 18.3435824135538, 0.195735083846963, 0.195735083846963
16.6, 55, 27281.4692961602, 20.8755273958803, 3.96092444894359, 3.96092444894359
16.6, 55, 81017.1760328248, 18.9834687699148, 0.462576998836875, 0.462576998836875
16.6, 55, 90129.3849290184, 18.7809056600762, 0.376175674619081, 0.376175674619081
16.6, 55, 134854.212682816, 18.3289989496668, 0.185163179879921, 0.185163179879921
16.6, 55, 116773.970377645, 18.4532827516731, 0.235074764576524, 0.235074764576524

0 comments on commit 0910bca

Please sign in to comment.