x 轴向右移动一位

x 轴向右移动一位

我想画出二项分布的概率函数。我写了以下代码来探索。我的问题:X轴应该向一侧移动,这样零点就不在原点了。这是我的代码:

\documentclass[a4paper,11pt,fleqn]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{ntheorem}
\usepackage{here}
\usepackage{multirow}
\usepackage{eurosym}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows,shadows}
\pagestyle{empty}
\usepackage{graphicx}
\usepackage{array}
\usetikzlibrary{decorations.pathreplacing} 
\usepackage{colortbl}
\usepackage[ngerman]{babel}
\usepackage{geometry}
\geometry{a4paper, top=15mm, left=25mm, right=25mm, bottom=20mm,
headsep=10mm, footskip=12mm}
\theoremstyle{break}
\newtheorem{def1}{Definition}[section]
\usepackage{amsfonts}
\usepackage{color}  
\usepackage{subfig}
\usepackage{gauss}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{gensymb}
\usepackage{systeme}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\pagestyle{empty}
\usepackage{booktabs}
\usetikzlibrary{positioning,fit,calc}
\usetikzlibrary{backgrounds}
\usepackage{amssymb}
\usepackage{amsmath}
\addtokomafont{caption}{\footnotesize}
\setlength{\mathindent}{0pt}
\makeatletter
\g@addto@macro\normalsize{%  <----
  \setlength\abovedisplayskip{0pt}%  <----
  \setlength\belowdisplayskip{10pt}%  <----
  \setlength\abovedisplayshortskip{0pt}%  <----
  \setlength\belowdisplayshortskip{20pt}%  <----
}
\makeatother
\setlength{\intextsep}{0pt}
\tikzset{
    declare function={
        normcdf(\x,\m,\s)=1/(1 + exp(-0.07056*((\x-\m)/\s)^3 - 1.5976*(\x-\m)/\s));
    }
}

\usepackage{tabularx}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}} % linksbündig mit Breitenangabe
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}} % zentriert mit Breitenangabe
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}} % rechtsbündig mit Breitenangabe

\newcommand*\xbar[1]{%
  \hbox{%
    \vbox{%
      \hrule height 0.5pt % The actual bar
      \kern0.5ex%         % Distance between bar and symbol
      \hbox{%
        \kern-0.1em%      % Shortening on the left side
        \ensuremath{#1}%
        \kern-0.1em%      % Shortening on the right side
      }%
    }%
  }%
}

 \usepackage{etoolbox}
\makeatletter
\patchcmd\g@matrix
 {\vbox\bgroup}
 {\vbox\bgroup\normalbaselines}% restore the standard baselineskip
 {}{}
\makeatother


\newcommand{\BAR}{%
  \hspace{-\arraycolsep}%
  \strut\vrule % the `\vrule` is as high and deep as a strut
  \hspace{-\arraycolsep}%
}

\usepackage{tabstackengine}[2016-10-04]
\stackMath


\usepackage{xpatch}
% REQUIRES V1.2 OF listofitems PACKAGE [2016-10-22]
\xpatchcmd{\readTABstack}{\readlist}{\greadlist}{}{}

\begin{document}
\begin{center}
{\color{black}\rule{16cm}{0.7mm}}
\end{center}
\begin{center}
\begin{large}
\textbf{Statistik 2 für Sozialökonomen} 
\\[3mm]
\textbf{SS 2017}
\\[3mm]
\textbf{Übungsblatt 3} 
\end{large}
\begin{center}
{\color{black}\rule{16cm}{0.7mm}}
\end{center}
\end{center}


\begin{figure}[H]
\centering
\begin{tikzpicture}[
    declare function={binom(\k,\n,\p)=\n!/(\k!*(\n-\k)!)*\p^\k*(1-\p)^(\n-\k);}
]
\begin{axis}[
    scale only axis,
    axis lines=middle,
    inner axis line style={=>},
        samples at={0,...,20},
        y tick label style={
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=2,
        /tikz/.cd
    },
        xtick={0,4,...,20},ybar=0pt, bar width=1,
                width=5cm,height=5cm,
                xmax=22,
                ymax=0.35,
                axis line style = thick,
                every axis x label/.style={at={(current axis.right of origin)},anchor=west},
every axis y label/.style={at={(current axis.north west)},above=0.5mm},
                xlabel={$x$},
                ylabel={$f(x)$}
]
\addplot [fill=red, fill opacity=0.5] {binom(x,20,0.1)};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

答案1

替换选项axis lines=middle,

xmin=-1,
axis y line=left,
axis x line=middle,

在此处输入图片描述

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[
    declare function={binom(\k,\n,\p)=\n!/(\k!*(\n-\k)!)*\p^\k*(1-\p)^(\n-\k);}
]
\begin{axis}[
    scale only axis,
    xmin=-1,
    axis y line=left,
    axis x line=middle,
    inner axis line style={=>},
        samples at={0,...,20},
        y tick label style={
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=2,
        /tikz/.cd
    },
        xtick={0,4,...,20},ybar=0pt, bar width=1,
                width=5cm,height=5cm,
                xmax=22,
                ymax=0.35,
                axis line style = thick,
                every axis x label/.style={at={(current axis.right of origin)},anchor=west},
every axis y label/.style={at={(current axis.north west)},above=0.5mm},
                xlabel={$x$},
                ylabel={$f(x)$}
]
\addplot [fill=red, fill opacity=0.5] {binom(x,20,0.1)};
\end{axis}
\end{tikzpicture}
\end{document}

相关内容