
我正在尋找一種自動建立圖像的方法,如下所示:
沒有 MWE,因為我現在擁有的只是這個圖像(我相信是由 SPSS 創建的,但我與該部分無關。) 當然,我可以簡單地將這個圖像添加到我的文檔中,但是因為這些問題出現的頻率很高,我想嘗試一種「原生」LaTeX 方式。我很確定某些圖形包足以勝任這項任務,但我甚至不知道從哪裡開始尋找。
我不指望你為我做這項工作,所以我真正要求的只是一個正確方向的指標、一個常見問題、一個 HOWTO,也許是一個例子。
答案1
這不是完整的答案,只是一個假設不需要自動處理資料的指標。
編輯:按照傑克的建議,使用\addplot table
代替可以\addplot plot coordinates
節省很多麻煩。
\documentclass{book}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\definecolor{mybarcolor}{RGB}{210 207 155}
\pgfmathsetmacro{\xmin}{-.5}
\begin{axis}[ymin=0,%
xmin=\xmin,%
xtick={0,2,4,6},%
xticklabels={.00,2.00,4.00,6.00},%
axis background/.style={fill=black!10},%
ylabel=\textbf{H\"{a}ufigkeit},%
xlabel=$D_\mathrm{rel}$,%
]
\addplot[ybar interval,fill=mybarcolor] table {%
0 7
.5 38
1 23
1.5 8
2 8
2.5 8
3 2
3.5 1
4 2
4.5 0
5 0
5.5 1
6 1
};
\addplot[domain=\xmin:6,samples=100] {18*exp(-.5*(x-1.5)^2)};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
這是 Asymptote 手冊中稍加修改的範例。它不僅自動根據統計數據創建圖像,而且還演示瞭如何首先生成數據。使用 處理以下hist.asy
文件asy -f pdf hist.asy
。
import graph;
import stats;
size(400,200,IgnoreAspect);
real lineWidth=2pt;
pen histFillPen=rgb(217/255, 206/255, 143/255)+opacity(0.7);
pen histLinePen=black;
pen linePen=red+lineWidth;
pen bgPen=rgb(241/255, 240/255, 238/255);
int n=10000;
real[] a=new real[n];
for(int i=0; i < n; ++i) a[i]=Gaussrand();
path g=graph(Gaussian,min(a),max(a));
pair gMin=min(g);
pair gMax=max(g)+(0,0.05);
filldraw(box(gMin,gMax),bgPen,black);
draw(g,linePen);
// Optionally calculate "optimal" number of bins a la Shimazaki and Shinomoto.
int N=bins(a);
histogram(a,min(a),max(a),N,normalize=true,low=0,fillpen=histFillPen,drawpen=histLinePen,bars=true);
xaxis("$x$",BottomTop,RightTicks);
yaxis("$dP/dx$",LeftRight,RightTicks(trailingzero));
答案3
如果您願意使用其他工具將繪圖建立為 PDF 並將其包含在您的 TeX 中,我絕對會推薦 gnuplot。它非常強大,你幾乎可以用它繪製任何你想要的東西。為了自動建立繪圖,您需要在 LaTeX Makefile 或序列中包含 gnuplot 命令。如果您想從多個資料檔案產生多個相似的繪圖,您也可以參數化資料檔案。
答案4
我認為使用從統計數據自動生成的圖像(和文字結果)來建立 LateX 文件對於右和揮舞或者針織物。
簡而言之,您可以製作一個普通的 LateX 文檔,該文檔的擴展名.Rnw
不是.tex
(noweb 文件),並插入了 R 程式碼的「區塊」。然後,R 中的 Sweave 將其匯出為真實.tex
文件,其中 R 區塊由 LateX 程式碼更改,並產生由這些 R 區塊產生的結果(圖像、表格或文字)。然後您可以.tex
像往常一樣編譯該文件pdflatex
。
例如,test.Rnw
具有最小 R 區塊的最小檔案可能是:
\documentclass{article}
\begin{document}
<<>>=
2+3
@
\end{document}
.tex
上面帶有擴展名並用它編譯的檔案pdflatex
將簡單地生成:
<<>>= 2+3 @
但轉換後R CMD Sweave test.Rnw
你會得到這個test.tex
:
\documentclass{article}
\usepackage{Sweave}
\begin{document}
\begin{Schunk}
\begin{Sinput}
> 2+3
\end{Sinput}
\begin{Soutput}
[1] 5
\end{Soutput}
\end{Schunk}
編譯後(使用pdflatex test.tex
)將產生:
> 2+3
[1]5
嗯,不是很令人印象深刻,只是顯示一個簡單的總和,如在 R 控制台中看到的那樣。但 R 是一個很棒的統計程序,R 區塊的結果可能是一個\includegraphics{}
帶有漂亮圖形的結果,或者是一個完整的 LaTeX 表浮點數 ( \begin{table} ...
),顯示或不顯示輸入。即使結果是簡單的數字,也可以使用(例如\Sexpr
: } )插入文字中\Sexpr{2+3}
。.csv
的文件)即可通過腳本,您可以獲得自動更新的圖表,還可以以純文本形式獲取更新的表格和更新的結果,而無需每次都編輯LaTeX 代碼,並且無需打開R。印象深刻了?
上面的螢幕截圖是用這個更詳細的hist.Rnw
文件產生的:
% File: hist.Rnw
%
% Usage:
%
% R CMD Sweave hist.Rnw
% pdflatex hist.tex
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\begin{document}
\lipsum[1] % dummy text
\begin{figure}[h]
\centering
% R Chunk
<<Histogran,echo=F,fig=T>>=
# Some random data
foo <- rnorm(1000, mean=1, sd=1)
# The histogram
hist(foo, prob=TRUE, border="tan4", ylab="Häufigkeit",
xlab="D_rel",col=rainbow(25,start=.4,end=.35), main="")
# To have also the ugly background area (optional):
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4],
col = "lightgrey")
hist(foo, prob=TRUE, border="tan4", ylab="Häufigkeit",
xlab="D_rel",col=rainbow(40,start=.1,end=.4), main="", add=T)
# Density plot
curve(dnorm(x, mean=mean(foo), sd=sd(foo)),
lwd=2, col="darkred", add=T)
@ % end of R chunk
\caption{This is a histogram of foo population with
mean=\Sexpr{round(mean(foo),2)} and sd=\Sexpr{round(sd(foo),2)}.
On the other hand, significance of Shapiro-Wilk test of normality is
p=\Sexpr{signif(shapiro.test(foo)$p.value,2)}, so this is a normal
population as sure as 2+2=\Sexpr{2+2}, as everybody knows (except some
processors with floating point bugs). }
\end{figure}
\lipsum[3] % more dummy text
\end{document}