latex 画图对于写paper很重要,因为这么画出来的图再也不用像matlab那样画完之后还得自己重新edit plots弄线的粗细,然后保存成pdf,然后剪切,最后再插入latex中,如果有几个图的话那么图的大小可能有细微的差别,对于要精益求精的人来说也不是很爽。
因此latex自己来画一些直方图,对比图还蛮有意思的。
因此需要pgfplots 和tick这两个包
http://pgfplots.sourceforge.net/ (特别注意,有些例子需要加载额外的包,如下面代码中的红色部分)
http://pgfplots.sourceforge.net/pgfplots.pdf
关于安装(在windows下)
其实就是设置环境变量,在http://pgfplots.sourceforge.net/pgfplots.pdf中有说明, 你可以将pgf 和 pgfplots这2个文件夹放在电脑的任何地方,值需要将路径设置为环境变量TEXINPUTS 和 TEXDOCS,然后你就可以用了。
\documentclass[12pt]{article}
\usepackage{times}
\usepackage{epsfig}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[lined,boxed,commentsnumbered, ruled]{algorithm2e}
\usepackage{bm}
\usepackage{multirow}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.9}
\usepackage{pgfplotstable}
\pgfplotstableset{fixed zerofill,precision=3}
\usepackage{tikz}
\usetikzlibrary{patterns} %很重要,这个你用everything查下看看是否有这个**code.tex
\usepgfplotslibrary{groupplots} % 很重要,这个你用everything查下看看是否有这个**code.tex
\usepgfplotslibrary{patchplots} % 很重要,这个你用everything查下看看是否有这个**code.tex
\usepackage{times}
\usepackage{booktabs}
\usepackage{array}
\usepackage{colortbl}
\usepackage{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
title={Mesh on top of patches (i): obscured}]
\addplot3[patch,patch type=biquadratic,shader=interp,
patch refines=3]
coordinates {
(0,0,1) (6,1,1.6) (5,5,1.3) (-1,5,0)
(3,1,0) (6,3,0.4) (2,6,1.1) (0,3,0.9)
(3,3.75,0.5)
};
\addplot3[patch,patch type=biquadratic,mesh,black,
patch refines=3]
coordinates {
(0,0,1) (6,1,1.6) (5,5,1.3) (-1,5,0)
(3,1,0) (6,3,0.4) (2,6,1.1) (0,3,0.9)
(3,3.75,0.5)
};
\end{axis}
\end{tikzpicture}
% Example using groupplots library
\begin{tikzpicture}
\begin{groupplot}[group style={group size=2 by 2},height=3cm,width=3cm]
\nextgroupplot
\addplot coordinates {(0,0) (1,1) (2,2)};
\nextgroupplot
\addplot coordinates {(0,2) (1,1) (2,0)};
\nextgroupplot
\addplot coordinates {(0,2) (1,1) (2,1)};
\nextgroupplot
\addplot coordinates {(0,2) (1,1) (1,0)};
\end{groupplot}
\end{tikzpicture}
% Same example created as done without the library
\begin{tikzpicture}
\begin{axis}[name=plot1,height=3cm,width=3cm]
\addplot coordinates {(0,0) (1,1) (2,2)};
\end{axis}
\begin{axis}[name=plot2,at={($(plot1.east)+(1cm,0)$)},anchor=west,height=3cm,width=3cm]
\addplot coordinates {(0,2) (1,1) (2,0)};
\end{axis}
\begin{axis}[name=plot3,at={($(plot1.south)-(0,1cm)$)},anchor=north,height=3cm,width=3cm]
\addplot coordinates {(0,2) (1,1) (2,1)};
\end{axis}
\begin{axis}[name=plot4,at={($(plot2.south)-(0,1cm)$)},anchor=north,height=3cm,width=3cm]
\addplot coordinates {(0,2) (1,1) (1,0)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
domain=0:1,
xmax=1,
ymax=1,
]
\addplot3[surf] {x*y};
\addplot3[blue,/pgfplots/quiver,
quiver/u=y,
quiver/v=x,
quiver/w=0,
quiver/scale arrows=0.1,
-stealth,samples=10] {1};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}
\addplot+[ybar] plot coordinates
{(0,3) (1,2) (2,4) (3,1) (4,2)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[xbar,enlargelimits=0.15]
\addplot
[draw=blue,pattern=horizontal lines light blue]
coordinates
{(10,5) (15,10) (5,15) (24,20) (30,25)};
\addplot
[draw=black,pattern=horizontal lines dark blue]
coordinates
{(3,5) (5,10) (15,15) (20,20) (35,25)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[nodes near coords={(\coordindex)},
title=Rectangle from matrix input]
% note that surf implies 'patch type=rectangle'
\addplot3[surf,shader=interp,samples=2,
patch type=rectangle]
{x*y};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
title={Grids with shader=faceted interp}]
\addplot3[]
coordinates {
(0,0,1) (6,1,1.6) (5,5,1.3) (-1,5,0)
(3,1,0) (6,3,0.4) (2,6,1.1) (0,3,0.9)
(3,3.75,0.5)
};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
enlargelimits=0.15,
legend style={at={(0.5,-0.20)},
anchor=north,legend columns=-1},
ylabel={\#participants},
symbolic x coords={tool1, tool2, tool3, tool4,
tool5, tool6, tool7},
xtick=data,
x tick label style={rotate=45,anchor=east},
]
\addplot+[ybar] plot coordinates {(tool1,0) (tool2,2)
(tool3,2) (tool4,3) (tool5,0) (tool6,2) (tool7,0)};
\addplot+[ybar] plot coordinates {(tool1,0) (tool2,0)
(tool3,0) (tool4,3) (tool5,1) (tool6,1) (tool7,0)};
\addplot+[ybar] plot coordinates {(tool1,6) (tool2,6)
(tool3,8) (tool4,2) (tool5,6) (tool6,5) (tool7,6)};
\addplot+[ybar] plot coordinates {(tool1,4) (tool2,2)
(tool3,0) (tool4,2) (tool5,3) (tool6,2) (tool7,4)};
\legend{never, rarely, sometimes, often}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[view/h=40,colormap/violet]
\addplot3[
surf,
%shader=interp,
shader=flat,
samples=50,
domain=-3:3,y domain=-2:2]
{sin(deg(x+y^2))};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
3d box,
zmax=1.4,
colorbar,
xlabel=$x$,
ylabel=$y$,
zlabel={$f(x,y) = x\cdot y$},
title={Using Coordinate Filters to fix $z=1.4$}]
% `pgfplotsexample4.dat' contains similar data as in
% `pgfplotsexample4_grid.dat', but it uses a uniform
% matrix structure (same number of points in every scanline).
% See examples above for extracts.
%\addplot3[surf,mesh/ordering=y varies]
% table {C:\CTEX\MiKTeX\doc\latex\pgfplots\plotdata\pgfplotsexample4.dat};
\addplot3[scatter,scatter src=\thisrow{f(x)},only marks, z filter/.code={\def\pgfmathresult{1.4}}]
table {pgfplotsexample4.dat}; % 这个地方是你的数据在哪里,所以要加载相应的目录
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\pgfplotsset{small}
\matrix {
\begin{axis}
\addplot {x};
\end{axis}
&
% differently large labels are aligned automatically:
\begin{axis}[ylabel={$f(x)=x^2$},ylabel style={font=\Huge}]
\addplot {x^2};
\end{axis}
\\
%
\begin{axis}[xlabel=$x$,xlabel style={font=\Huge}]
\addplot {x^3};
\end{axis}
&
\begin{axis}
\addplot {x^4};
\end{axis}
\\
};
\end{tikzpicture}
\end{document}