Überarbeitungsverlauf[Zurück]
Klicke auf Einblenden/Ausblenden von Überarbeitungen 3

18 Aug '16, 11:55

Henri's gravatar image

Henri
15.7k133943

Man kann solche einen Graphen mit `pgfplots` erzeugen. Um aus einer Tabelle ein Histogramm zu erstellen benötigt man die `statistics`-Bibliothek von `pgfplots`. Ich habe die Zufallszahlen mit Lua generiert. Da Lua leider nur gleichverteilte Zufallszahlen kann habe ich die Box-Muller-Transformation zur Erzeugung Gauß-verteilter Zufallszahlen implementiert. Wenn man statt des Lua-Codes lieber eine eigene Datei verwenden möchte, so ersetzt man \generatetable{1000}\loadedtable durch \pgfplotstableread{<Dateiname>}\loadedtable dann wird auch kein LuaTeX mehr benötigt. \documentclass{article} \usepackage{pgfplots} \usepgfplotslibrary{statistics} \usepackage{luacode} \begin{luacode*} -- Initialize the random number generator according to -- http://lua-users.org/wiki/MathLibraryTutorial math.randomseed( tonumber(tostring(os.time()):reverse():sub(1,6)) ) -- standard Box-Muller transform -- https://en.wikipedia.org/wiki/Box-Muller_transform function gauss_random(mu, sigma) -- State variables local generate = false local z0 = 0.0 local z1 = 0.0 -- Parameters local mu = mu or 0.0 local sigma = sigma or 1.0 local function random() generate = not generate if (not generate) then return z1 * sigma + mu; end local u1, u2 repeat u1 = math.random() u2 = math.random() until ( u1 > 1e-16 ) z0 = math.sqrt(-2.0 * math.log(u1)) * math.cos(2*math.pi * u2) z1 = math.sqrt(-2.0 * math.log(u1)) * math.sin(2*math.pi * u2) return z0*sigma + mu end return random end function generate_table(N) local gauss = gauss_random() tex.sprint("{\r") tex.sprint("x y\r") for i = 1, N do tex.sprint(gauss() .. " " .. gauss() .. "\r") end tex.sprint("}") end \end{luacode*} \newcommand\generatetable[2]{% \expandafter\pgfplotstableread\directlua{generate_table(#1)}#2 } \begin{document} \generatetable{1000}\loadedtable \begin{tikzpicture} \begin{axis}[ name=main,grid=major,xlabel={$x$},ylabel={$y$}, yticklabel pos=right,ylabel near ticks, width=8cm,height=8cm,enlargelimits=false, xmin=-4,xmax=4,ymin=-4,ymax=4 ] \addplot+[only marks,mark=o] table {\loadedtable}; \addlegendentry{data}; \end{axis} \begin{axis}[ at=(main.below south),anchor=north,rotate=180, width=8cm,height=4cm,enlargelimits=false, axis lines=none,ybar ] \addplot+[hist={bins=30,data min=-4,data max=4}] table [y index=0] {\loadedtable}; \end{axis} \begin{axis}[ at=(main.right of east),anchor=west,rotate=-90, width=8cm,height=4cm,enlargelimits=false, axis lines=none,ybar ] \addplot+[hist={bins=30,data min=-4,data max=4}] table [y index=1] {\loadedtable}; \end{axis} \end{tikzpicture} \end{document} > ![alt text][1] [1]: http://texwelt.de/wissen/upfiles/test_113.png
Klicke auf Einblenden/Ausblenden von Überarbeitungen 2
Unschönen Lua-Code verbessert; jetzt mit Closure

18 Aug '16, 11:53

Henri's gravatar image

Henri
15.7k133943

Man kann solche einen Graphen mit `pgfplots` erzeugen. Um aus einer Tabelle ein Histogramm zu erstellen benötigt man die `statistics`-Bibliothek von `pgfplots`. Ich habe die Zufallszahlen mit Lua generiert. Da Lua leider nur gleichverteilte Zufallszahlen kann habe ich die Box-Muller-Transformation zur Erzeugung Gauß-verteilter Zufallszahlen implementiert. Wenn man statt des Lua-Codes lieber eine eigene Datei verwenden möchte, so ersetzt man \generatetable{1000}\loadedtable durch \pgfplotstableread{<Dateiname>}\loadedtable dann wird auch kein LuaTeX mehr benötigt. \documentclass{article} \usepackage{pgfplots} \usepgfplotslibrary{statistics} \usepackage{luacode} \begin{luacode*} -- Initialize the random number generator according to -- http://lua-users.org/wiki/MathLibraryTutorial math.randomseed( tonumber(tostring(os.time()):reverse():sub(1,6)) ) -- State variables of gauss_random generate = false z1 = 0.0 z2 = 0.0 -- Parameters of gauss_random mu = 0.0 sigma = 1.0 -- standard Box-Muller transform -- https://en.wikipedia.org/wiki/Box-Muller_transform function gauss_random() gauss_random(mu, sigma) -- State variables local generate = false local z0 = 0.0 local z1 = 0.0 -- Parameters local mu = mu or 0.0 local sigma = sigma or 1.0 local function random() generate = not generate if (not generate) then return z1 * sigma + mu; end repeat u1 = math.random() u2 = math.random() until ( u1 > 1e-16 ) z0 = math.sqrt(-2.0 * math.log(u1)) * math.cos(2*math.pi * u2) z1 = math.sqrt(-2.0 * math.log(u1)) * math.sin(2*math.pi * u2) return z0*sigma + mu end return random end function generate_table(N) local gauss = gauss_random() tex.sprint("{\r") tex.sprint("x y\r") for i = 1, N do tex.sprint(gauss_random() tex.sprint(gauss() .. " " .. gauss_random() gauss() .. "\r") end tex.sprint("}") end \end{luacode*} \newcommand\generatetable[2]{% \expandafter\pgfplotstableread\directlua{generate_table(#1)}#2 } \begin{document} \generatetable{1000}\loadedtable \begin{tikzpicture} \begin{axis}[ name=main,grid=major,xlabel={$x$},ylabel={$y$}, yticklabel pos=right,ylabel near ticks, width=8cm,height=8cm,enlargelimits=false, xmin=-4,xmax=4,ymin=-4,ymax=4 ] \addplot+[only marks,mark=o] table {\loadedtable}; \addlegendentry{data}; \end{axis} \begin{axis}[ at=(main.below south),anchor=north,rotate=180, width=8cm,height=4cm,enlargelimits=false, axis lines=none,ybar ] \addplot+[hist={bins=30,data min=-4,data max=4}] table [y index=0] {\loadedtable}; \end{axis} \begin{axis}[ at=(main.right of east),anchor=west,rotate=-90, width=8cm,height=4cm,enlargelimits=false, axis lines=none,ybar ] \addplot+[hist={bins=30,data min=-4,data max=4}] table [y index=1] {\loadedtable}; \end{axis} \end{tikzpicture} \end{document} > ![alt text][1] [1]: http://texwelt.de/wissen/upfiles/test_113.pnghttp://texwelt.de/wissen/upfiles/test_113.png
Klicke auf Einblenden/Ausblenden von Überarbeitungen 1

10 Mai '16, 15:48

Henri's gravatar image

Henri
15.7k133943