pgfplot 图片放置

pgfplot 图片放置

首先我非常感谢 stackexchange 尤其是薛定谔的猫帮助我 pgfplots 和 tikz。

我正在准备关于连续随机变量的讲座,我使用节点作为线等制作了下面的图。

我只是想问一下是否有某种方法,以便我可以将这张图片移动到页面上的任何地方而不改变坐标,因为我已经根据这张图片使用了线条坐标,如果我移动它,就需要改变它。

附有完整的运行代码。

提前致谢。

\documentclass{article}
\usepackage[top=2in,bottom=1in,left=1.15in,right=1.15in]{geometry}
% \usepackage[portrait, paperwidth=21cm, paperheight=29.7cm, margin=1.75in]{geometry}
% \documentclass[a4paper, 10pt]{article}
% \usepackage[utf8]{inputenc}
% \usepackage{biblatex}
% \addbibresource{bib.bib}
\setlength{\parindent}{0em}
\bibliography{bib}
\usepackage{color,soul}
\usepackage{tikz}
\usepackage{pgfplots}  
% \pgfplotsset{compat=1.5} %for 3d graph plane text active this package
\pgfplotsset{compat=newest}
\usetikzlibrary{3d}
\usepackage{fontawesome}
\usepackage{enumitem}
\usepackage{tikz}
\usepackage{eso-pic}
\usepackage{graphicx}
\usetikzlibrary{positioning}
\usepackage{forest}
\usepackage{mathtools}
\usepackage{amsmath}
\usepackage{nicefrac}
\usepackage{xcolor}
\usepackage{amssymb}
% \usepackage{geometry}
\usepackage{fontspec}
\usepackage{lettrine}
\usepackage{ragged2e}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows}
\usetikzlibrary{arrows.meta}
\usepackage{tikz-qtree}
\usepackage{varwidth}
\usetikzlibrary{trees,arrows}
\usetikzlibrary{patterns,arrows,decorations.pathreplacing}
\usepackage{amsfonts}
\usetikzlibrary{automata}
\usepackage[makeroom]{cancel}
\usepackage{textcomp}
\usetikzlibrary{calc,positioning,fit}
\usepackage{upgreek}
\usetikzlibrary{backgrounds, shadows}
\usepackage{subfig}
\usepackage{venndiagram}
\usepackage{tikz-cd}
\usetikzlibrary{math}
\usetikzlibrary{tikzmark}
\usetikzlibrary{decorations.markings}
\usepackage{eqparbox}
\usepackage{empheq}
\usepackage[most]{tcolorbox}
\usepackage{filecontents}
\usepackage{amsthm}
\usepackage{multicol}
\usepackage{scalerel}


\setul{0.5ex}{0.3ex}
\setulcolor{blue}

%%%%%%%%%%%%%%%%%%% Color Boxing %%%%%%%%%%%%%%%%%
\newtcbox{\mymath}[1][]{%
    nobeforeafter, math upper, tcbox raise base,
    enhanced, colframe=blue!30!black,
    colback=blue!30, boxrule=0.5pt,
    #1}


\newcommand{\indices}[2]{{% \indices{<rows>}{<columns>}
  \begin{array}{@{}r@{}}
    \scriptstyle #2~\smash{\eqmakebox[ind]{$\scriptstyle\rightarrow$}} \\[-\jot]  
    \scriptstyle #1~\smash{\eqmakebox[ind]{$\scriptstyle\downarrow$}}
  \end{array}}}


% \everymath{\color{blue}}
% \everydisplay{\color{blue}}

\newcommand{\smallfont}[1]{{%
  \fontsize{10pt}{12pt}\normalfont #1%
}}
\newcommand{\normfont}[1]{{%
  \fontsize{20pt}{24pt}\normalfont #1%
}}
\newcommand{\bigfont}[1]{{%
  \fontsize{30pt}{36pt}\normalfont #1%
}}

%%%%%%%%%%%% Page Referencing Sizing %%%%%%%%%%%%%% 
\makeatletter
\def\parsecomma#1,#2\endparsecomma{\def\page@x{#1}\def\page@y{#2}}
\tikzdeclarecoordinatesystem{page}{
    \parsecomma#1\endparsecomma
    \pgfpointanchor{current page}{north east}
    % Save the upper right corner
    \pgf@xc=\pgf@x%
    \pgf@yc=\pgf@y%
    % save the lower left corner
    \pgfpointanchor{current page}{south west}
    \pgf@xb=\pgf@x%
    \pgf@yb=\pgf@y%
    % Transform to the correct placement
    \pgfmathparse{(\pgf@xc-\pgf@xb)/2.*\page@x+(\pgf@xc+\pgf@xb)/2.}
    \expandafter\pgf@x\expandafter=\pgfmathresult pt
    \pgfmathparse{(\pgf@yc-\pgf@yb)/2.*\page@y+(\pgf@yc+\pgf@yb)/2.}
    \expandafter\pgf@y\expandafter=\pgfmathresult pt
}
\makeatother

%%%%%%%%%%%%%% Define Colors %%%%%%%%%%%%%%%
\definecolor{light-gray}{gray}{0.9}
\definecolor{amber}{rgb}{1.0, 0.75, 0.0}
\definecolor{aurometalsaurus}{rgb}{0.43, 0.5, 0.5}
\definecolor{britishracinggreen}{rgb}{0.0, 0.26, 0.15}
\definecolor{cambridgeblue}{rgb}{0.64, 0.76, 0.68}
\definecolor{babyblueeyes}{rgb}{0.63, 0.79, 0.95}
\definecolor{darkcyan}{rgb}{0.0, 0.55, 0.55}
\definecolor{coolblack}{rgb}{0.0, 0.18, 0.39}
\definecolor{darktangerine}{rgb}{1.0, 0.66, 0.07}
\definecolor{orangered}{rgb}{1.0, 0.27, 0.0}
\definecolor{ao(english)}{rgb}{0.0, 0.5, 0.0}
\definecolor{darkcandyapplered}{rgb}{0.64, 0.0, 0.0}

%%%%%%%%%% Font %%%%%%%%%%%%%%%%%%%%%
\setmainfont[Ligatures=TeX,Color=coolblack]{Futura Medium.ttf}



%%%%%%%%%%%%%% absolute definition %%%%%%%%%%%%%
\DeclarePairedDelimiter\abs{\lvert}{\rvert}%
\DeclarePairedDelimiter\norm{\lVert}{\rVert}%

% Swap the definition of \abs* and \norm*, so that \abs
% and \norm resizes the size of the brackets, and the 
% starred version does not.
\makeatletter
\let\oldabs\abs
\def\abs{\@ifstar{\oldabs}{\oldabs*}}
%
\let\oldnorm\norm
\def\norm{\@ifstar{\oldnorm}{\oldnorm*}}
\makeatother



%%%%%%%%%%%%%%%%%%% thick dot %%%%%%%%%%%%%%%%%%%%%
\makeatletter
\newcommand*\bigcdot{\mathpalette\bigcdot@{.5}}
\newcommand*\bigcdot@[2]{\mathbin{\vcenter{\hbox{\scalebox{#2}{$\m@th#1\bullet$}}}}}
\makeatother

%%%%%%%%%%%%%%%%% Circling %%%%%%%%%%%%%%%%%%%%%
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
            \node[shape=circle,draw,inner sep=2pt] (char) {#1};}}

%%%%%%%%%%%%% Permutation and Combination Symbols %%%%%%%%%%%%%%%%%%%%
\newcommand\Myperm[2][^n]{\prescript{#1\mkern-2.5mu}{}P_{#2}}
\newcommand\Mycomb[2][^n]{\prescript{#1\mkern-0.5mu}{}C_{#2}}


\newcommand*{\Scale}[2][4]{\scalebox{#1}{$#2$}}%
\newcommand*{\Resize}[2]{\resizebox{#1}{!}{$#2$}}%

%%%%%%%%%%%%%% Header of the Pages %%%%%%%%%%%%
\AddToShipoutPictureBG{\begin{tikzpicture}[remember picture, overlay]
    \draw [line width=0.3pt,color=light-gray,step=0.5cm] (current page.south west) grid (current page.north east);
        \node at (page cs:-0.75,0.8) {\includegraphics[width=0.2\textwidth]{seal.png}};    
%   \node[text=Brown4!30!DarkBlue,scale=2] at (page cs:0.1,0.8) {Probability Methods in Engineering}; 
  \path (current page.north east) ++(-1.1,-1.1) node[below left, scale=1.0] {\thepage};
\end{tikzpicture}}

% \pagenumbering{gobble}
\linespread{1.5}

%%%%%%%%%%%%%%%% skull sign  %%%%%%%%%%%%%%%%%%
\DeclareFontFamily{U}{skulls}{}
\DeclareFontShape{U}{skulls}{m}{n}{ <-> skull }{}
\newcommand{\skull}{\text{\usefont{U}{skulls}{m}{n}\symbol{'101}}}


%%%%%%%%%%% Conditional Bar %%%%%%%%%%%%%%%%%%
\newcommand{\expect}{\mathbf{E}\expectarg}
\DeclarePairedDelimiterX{\expectarg}[1]{[}{]}{%
  \ifnum\currentgrouptype=16 \else\begingroup\fi
  \activatebar#1
  \ifnum\currentgrouptype=16 \else\endgroup\fi
}

\newcommand{\innermid}{\nonscript\;\delimsize\vert\nonscript\;}
\newcommand{\activatebar}{%
  \begingroup\lccode`\~=`\|
  \lowercase{\endgroup\let~}\innermid 
  \mathcode`|=\string"8000
}

\renewcommand{\qedsymbol}{$\blacksquare$}

\newcommand{\quotes}[1]{``#1''}

\newcommand{\cp}[2]{$P(\,#1 \mid #2\,)$}

\newcommand{\parm}{\mathord{\color{black!33}\bullet}}%

\newtcolorbox{myquote}[1][]{%
    colback=black!5,
    colframe=black!5,
    notitle,
    sharp corners,
    borderline west={2pt}{0pt}{red!80!black},
    enhanced,
    breakable,
    }
\newcommand{\vertLineFromPoint}[1]{
  \draw[dashed, color=black] 
  (#1) -- (#1|-{rel axis cs:0,0})
}
\newcommand{\horLineFromPoint}[1]{
  \draw[dashed, color=black] 
  (#1) -- (#1-|{rel axis cs:0,0})
}

    \tikzset{
        hatch distance/.store in=\hatchdistance,
        hatch distance=10pt,
        hatch thickness/.store in=\hatchthickness,
        hatch thickness=2pt
    }

    \makeatletter
    \pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{flexible hatch}
    {\pgfqpoint{0pt}{0pt}}
    {\pgfqpoint{\hatchdistance}{\hatchdistance}}
    {\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
    {
        \pgfsetcolor{\tikz@pattern@color}
        \pgfsetlinewidth{\hatchthickness}
        \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
        \pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
        \pgfusepath{stroke}
    }
    \makeatother



\begin{document}
\pagestyle{empty}
\fontsize{14pt}{16pt}\selectfont
\boldmath

\begin{figure}[htb]
\centering
\begin{tikzpicture}% left picture
\begin{axis}[
xmin=-5, xmax=5,
% grid=both,
hide y axis,
hide x axis,
axis lines=middle,
minor tick num=9,
axis line style={latex-latex},
ticklabel style={font=\tiny},
axis equal
]
\addplot [ultra thick,domain=-pi:pi,samples=200,blue]({1*sin(deg(x))-2}, {sqrt(4)*cos(deg(x))+2}) node [rotate=90, xshift=38pt] {$\{\mathbf{a \leq X \leq b}\}$};

\addplot [ultra thick,domain=-pi:pi,samples=200,red]({2*sin(deg(x))+1.5}, {sqrt(2)*cos(deg(x))+2}) node [yshift=27pt] {$\{\mathbf{c \leq X \leq d}\}$};

\addplot [ultra thick, domain=-pi:pi,samples=200,orange]({4*sin(deg(x))}, {sqrt(9)*cos(deg(x))+2}) node [yshift=10pt]  {$\mathbf{\Upomega}$};
\end{axis}
\end{tikzpicture}
    \hfill        
\begin{tikzpicture}[scale=0.85]% right picture
    % \tikzset{
    %     hatch distance/.store in=\hatchdistance,
    %     hatch distance=10pt,
    %     hatch thickness/.store in=\hatchthickness,
    %     hatch thickness=2pt
    % }

    % \makeatletter
    % \pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{flexible hatch}
    % {\pgfqpoint{0pt}{0pt}}
    % {\pgfqpoint{\hatchdistance}{\hatchdistance}}
    % {\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
    % {
    %     \pgfsetcolor{\tikz@pattern@color}
    %     \pgfsetlinewidth{\hatchthickness}
    %     \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
    %     \pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
    %     \pgfusepath{stroke}
    % }
    % \makeatother

    \begin{axis}[scale only axis,
        axis lines =middle,
        hide y axis, 
        xmin=-4,xmax=4,
        x axis line style={draw opacity=1},
        ymin=0,ymax=0.5,
        xtick={-2,-0.5,1,2},
        xticklabels={$a$, $b$, $c$, $d$},
        axis on top,
        % y axis line style={draw opacity=0},
        legend style={legend cell align=right,legend plot pos=right}] 
    \addplot[ultra thick, color=orange,domain=-4:4,samples=100] {1/sqrt(2*pi)*exp(-x^2/2)};
    % \addlegendentry{z}
    \addplot+[ultra thick, mark=none,
        domain=1:2,
        samples=100,
        pattern=flexible hatch,
        hatch distance=6pt,
        hatch thickness=0.6pt,
        area legend,
        pattern color=red]{1/sqrt(2*pi)*exp(-x^2/2)} \closedcycle;
    % \addlegendentry{Interval 1}
    \addplot+[ultra thick, mark=none,
        domain=-2:-0.5,
        samples=100,
        pattern=flexible hatch,
        hatch distance=5pt,
        hatch thickness=0.5pt,
        draw=blue,
        pattern color=blue,
        area legend]{1/sqrt(2*pi)*exp(-x^2/2)} \closedcycle;    
        % \addlegendentry{Interval 2}
    \end{axis}
\end{tikzpicture}
    \end{figure}
\begin{tikzpicture}[remember picture, overlay,shift=(current page.center)]
% \draw (-4,4.5) -- (3,3);
\draw [->,ultra thick,red]   (-4,4.5) to[out=-30,in=-90] (5.5,3.8) node [yshift=-65pt] {$\Scale[0.65]{P(c\leq X \leq d)=\displaystyle\int\limits_c^df(\beta)d\beta}$};
\draw [-,ultra thick,blue]   (-6.5,7) to[out=30,in=90] (-0.75,5) node [yshift=72] {$\Scale[0.65]{P(a\leq X \leq b)=\displaystyle\int\limits_a^bf(\alpha)d\alpha}$};;
\draw [->,ultra thick,blue]   (-0.75,5) to[out=-90,in=-90] (3,3.8);
\node at (5.5,9) [orange]  {$f_X(x)$};
\node at (7.6,7) [orange]  {$\displaystyle\int\limits_{-\infty}^{+\infty}f_X(x)dx=1$};
\end{tikzpicture}
\vspace{0.75cm}
\begin{center}
    \huge Continuous Random Variables
\end{center}

\end{document}

代码图片

答案1

无需使用硬编码坐标,也无需使用图片overlay,也无需使用命令\Scale,节点可以用 缩放scale。我把所有东西都放在一张图片中,使用符号坐标也摆脱了未使用的包。

\documentclass{article}
\usepackage[top=2in,bottom=1in,left=1.15in,right=1.15in]{geometry}
\usepackage{upgreek}
\usepackage{pgfplots}  
\pgfplotsset{compat=newest}
\usetikzlibrary{patterns.meta}
\usepackage{mathtools}
\begin{document}
\pagestyle{empty}
\fontsize{14pt}{16pt}\selectfont
\boldmath

\begin{figure}[htb]
\centering
\begin{tikzpicture}% left picture
\begin{axis}[
xmin=-5, xmax=5,
% grid=both,
hide y axis,
hide x axis,
axis lines=middle,
minor tick num=9,
axis line style={latex-latex},
ticklabel style={font=\tiny},
axis equal
]
\addplot [ultra thick,domain=-pi:pi,samples=200,blue]({1*sin(deg(x))-2}, {sqrt(4)*cos(deg(x))+2})
 coordinate[pos=1] (blueL)
 node [rotate=90, xshift=38pt] {$\{\mathbf{a \leq X \leq b}\}$};

\addplot [ultra thick,domain=-pi:pi,samples=200,red]({2*sin(deg(x))+1.5}, {sqrt(2)*cos(deg(x))+2}) 
 coordinate[pos=1] (redL)
node [yshift=27pt]  {$\{\mathbf{c \leq X \leq d}\}$};

\addplot [ultra thick, domain=-pi:pi,samples=200,orange]({4*sin(deg(x))}, {sqrt(9)*cos(deg(x))+2}) 
 coordinate[pos=1] (orangeL)
node [yshift=10pt]  {$\mathbf{\Upomega}$};
\end{axis}
%
\begin{axis}[scale=0.85,xshift=6.4cm,
        scale only axis,
        axis lines =middle,
        hide y axis, 
        xmin=-4,xmax=4,
        x axis line style={draw opacity=1},
        ymin=0,ymax=0.5,
        xtick={-2,-0.5,1,2},
        xticklabels={$a$, $b$, $c$, $d$},
        axis on top,
        % y axis line style={draw opacity=0},
        legend style={legend cell align=right,legend plot pos=right}] 
    \addplot[ultra thick, color=orange,domain=-4:4,samples=100] {1/sqrt(2*pi)*exp(-x^2/2)}
    node[pos=0.55,above right,orange]  {$f_X(x)$}
    node[pos=0.65,above right,orange]  {$\displaystyle\int\limits_{-\infty}^{+\infty}f_X(x)dx=1$}
    ;
    % \addlegendentry{z}
    \addplot+[ultra thick, mark=none,
        domain=1:2,
        samples=100,
        pattern={Lines[angle=45,distance={5pt}]},
        pattern color=red]{1/sqrt(2*pi)*exp(-x^2/2)} \closedcycle;
    \path (1.5,0) coordinate (redR);    
    % \addlegendentry{Interval 1}
    \addplot+[ultra thick, mark=none,
        domain=-2:-0.5,
        samples=100,
        pattern={Lines[angle=45,distance={5pt}]},
        pattern color=blue]{1/sqrt(2*pi)*exp(-x^2/2)} \closedcycle;   
    \path (-1.25,0) coordinate (blueR);          
        % \addlegendentry{Interval 2}
    \end{axis}
%   
\begin{scope}
\draw [->,ultra thick,red]   (redL) to[out=-30,in=-90]  
node [pos=0.8,below,scale=0.65] 
{$P(c\leq X \leq d)=\displaystyle\int\limits_c^df(\beta)\,\mathrm{d}\beta$}(redR);
\draw [->,ultra thick,blue]   (blueL) to[out=-40,in=-90] 
node [pos=0.65,below,scale=0.65] {$P(a\leq X \leq
b)=\displaystyle\int\limits_a^bf(\alpha)\,\mathrm{d}\alpha$} (blueR) ;
\end{scope}
\end{tikzpicture}
\caption{Continuous Random Variables.}
\end{figure}
\end{document}

在此处输入图片描述

由于这个图形是独立的,因此可以移动。

相关内容