我有一张pgfplots
如下的图形:
我使用 Python 脚本程序生成的代码如下所示:
\documentclass[margin=1pt,]{standalone}
\usepackage{scalerel}
\usepackage{pgfplots,tikz}
\usetikzlibrary{calc}
\ifpdftex\usepackage[scaled=1]{helvet}\fi
\ifxetex\usepackage{fontspec}\setsansfont{TeX Gyre Heros}\fi
\usepackage[helvet]{sfmath}
\usepackage{amsmath,latexsym}
\usetikzlibrary{shapes.misc,positioning,patterns}
\tikzset{cross/.style={cross out, draw=black, minimum size=2*(#1-\pgflinewidth), inner sep=0pt, outer sep=0pt},cross/.default={3pt}}
\pgfplotsset{compat=newest}
\begin{document}
\pgfplotsset{scaled x ticks=false}
\definecolor{myyellow}{rgb}{0.96,0.742,0.29}
\definecolor{myblue}{rgb}{0.1,0.32,0.738}
\definecolor{myotheryellow}{rgb}{0.98828125,0.5625,0.13671875}
\definecolor{myotherblue}{rgb}{0.18359375,0.3515625,0.7578125}
\providecommand\ATLASlabel{Internal}
\renewcommand\sfdefault{phv}
\renewcommand\rmdefault{phv}
\renewcommand\ttdefault{pcr}
\font\greekcapstenrm=cmr10
\font\greekcapssevenrm=cmr7
\font\greekcapsfiverm=cmr5
\newfam\greekcapsfam
\textfont\greekcapsfam=\greekcapstenrm
\scriptfont\greekcapsfam=\greekcapssevenrm
\scriptscriptfont\greekcapsfam=\greekcapsfiverm
\let\tmpLambda=\Lambda \def\Lambda{{\fam\greekcapsfam\tmpLambda}}
\begin{tikzpicture}
\begin{axis}[
width = 0.8\textwidth,
width = 0.8\textwidth,
y=1cm,
xlabel = {\textsf{Parameter Value}},
clip = false,
ymin=-0.5,
ymax= 13.5,
xmin=0.5218352188569495,
xmax=1.6920683616453638,
minor tick num=4,
ytick style={draw=none},
yticklabels=\empty,
enlarge x limits=true,
xticklabel style={/pgf/number format/fixed},
scaled ticks=false,
]
\node at ({rel axis cs:0,0}|-{axis cs:0,0}) [anchor = east]{\textsf{norm\_bkg}};
\node at ({rel axis cs:0,0}|-{axis cs:0,1}) [anchor = east]{\textsf{mu}};
\node at ({rel axis cs:0,0}|-{axis cs:0,2}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_9}};
\node at ({rel axis cs:0,0}|-{axis cs:0,3}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_8}};
\node at ({rel axis cs:0,0}|-{axis cs:0,4}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_7}};
\node at ({rel axis cs:0,0}|-{axis cs:0,5}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_6}};
\node at ({rel axis cs:0,0}|-{axis cs:0,6}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_5}};
\node at ({rel axis cs:0,0}|-{axis cs:0,7}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_4}};
\node at ({rel axis cs:0,0}|-{axis cs:0,8}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_3}};
\node at ({rel axis cs:0,0}|-{axis cs:0,9}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_2}};
\node at ({rel axis cs:0,0}|-{axis cs:0,10}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_10}};
\node at ({rel axis cs:0,0}|-{axis cs:0,11}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_1}};
\node at ({rel axis cs:0,0}|-{axis cs:0,12}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_0}};
\node at ({rel axis cs:0,0}|-{axis cs:0,13}) [anchor = east]{\textsf{gamma\_stat\_CR\_bin\_0}};
\draw[color=red,solid] (axis cs:0.96608,0.00)--(axis cs:1.02975,0.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:0.99791,0.00){};
\node[xshift=3em,anchor=base] at ({rel axis cs:1,0}|-{axis cs:0,0.0}) {$0.998^{+0.032}_{-0.032}$};
\draw[color=red,solid] (axis cs:0.52184,1.00)--(axis cs:1.69207,1.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:1.10187,1.00){};
\node[xshift=3em,anchor=base] at ({rel axis cs:1,0}|-{axis cs:0,1.0}) {$1.1^{+0.6}_{-0.6}$};
\draw[color=red,solid] (axis cs:0.95813,2.00)--(axis cs:1.04271,2.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:1.00042,2.00){};
\node[xshift=3em,anchor=base] at ({rel axis cs:1,0}|-{axis cs:0,2.0}) {$1.00^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.95197,3.00)--(axis cs:1.03637,3.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:0.99417,3.00){};
\node[xshift=3em,anchor=base] at ({rel axis cs:1,0}|-{axis cs:0,3.0}) {$0.99^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.96731,4.00)--(axis cs:1.04759,4.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:1.00745,4.00){};
\node[xshift=3em,anchor=base] at ({rel axis cs:1,0}|-{axis cs:0,4.0}) {$1.01^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.97100,5.00)--(axis cs:1.05142,5.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:1.01121,5.00){};
\node[xshift=3em,anchor=base] at ({rel axis cs:1,0}|-{axis cs:0,5.0}) {$1.01^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.95597,6.00)--(axis cs:1.03721,6.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:0.99659,6.00){};
\node[xshift=3em,anchor=base] at ({rel axis cs:1,0}|-{axis cs:0,6.0}) {$1.00^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.96513,7.00)--(axis cs:1.04544,7.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:1.00528,7.00){};
\node[xshift=3em,anchor=base] at ({rel axis cs:1,0}|-{axis cs:0,7.0}) {$1.01^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.96977,8.00)--(axis cs:1.05069,8.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:1.01023,8.00){};
\node[xshift=3em,anchor=base] at ({rel axis cs:1,0}|-{axis cs:0,8.0}) {$1.01^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.93833,9.00)--(axis cs:1.02097,9.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:0.97965,9.00){};
\node[xshift=3em,anchor=base] at ({rel axis cs:1,0}|-{axis cs:0,9.0}) {$0.98^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.96196,10.00)--(axis cs:1.04405,10.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:1.00301,10.00){};
\node[xshift=3em,anchor=base] at ({rel axis cs:1,0}|-{axis cs:0,10.0}) {$1.00^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.95952,11.00)--(axis cs:1.04276,11.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:1.00114,11.00){};
\node[xshift=3em,anchor=base] at ({rel axis cs:1,0}|-{axis cs:0,11.0}) {$1.00^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.94300,12.00)--(axis cs:1.02794,12.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:0.98547,12.00){};
\node[xshift=3em,anchor=base] at ({rel axis cs:1,0}|-{axis cs:0,12.0}) {$0.99^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.99900,13.00)--(axis cs:1.00100,13.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:1.00000,13.00){};
\node[xshift=3em,anchor=base] at ({rel axis cs:1,0}|-{axis cs:0,13.0}) {$1.000^{+0.001}_{-0.001}$};
\end{axis}
\end{tikzpicture}
\end{document}
我对此非常满意,除了一个细节:右侧的标签节点没有以任何有意义的方式对齐 - 我希望它们水平对齐,以便所有点都在同一条(假想的)垂直线上。
我可以想象用几种不同的方式来做到这一点:
- 将节点拆分为一个带有点前数字的节点,然后是该节点的其余部分。
- 在节点中使用一些
\rlap
魔法来避免将点之前的数字计入边界框
但我不确定是否有更优雅的方法来实现这一点。
非常感谢您的任何建议!
答案1
非常感谢您的任何建议!
你为什么不替换anchor=base
为anchor=west
?
\documentclass[margin=1pt,]{standalone}
\usepackage{scalerel}
\usepackage{pgfplots,tikz}
\usetikzlibrary{calc}
\ifpdftex\usepackage[scaled=1]{helvet}\fi
\ifxetex\usepackage{fontspec}\setsansfont{TeX Gyre Heros}\fi
\usepackage[helvet]{sfmath}
\usepackage{amsmath,latexsym}
\usetikzlibrary{shapes.misc,positioning,patterns}
\tikzset{cross/.style={cross out, draw=black, minimum size=2*(#1-\pgflinewidth), inner sep=0pt, outer sep=0pt},cross/.default={3pt}}
\pgfplotsset{compat=newest}
\begin{document}
\pgfplotsset{scaled x ticks=false}
\definecolor{myyellow}{rgb}{0.96,0.742,0.29}
\definecolor{myblue}{rgb}{0.1,0.32,0.738}
\definecolor{myotheryellow}{rgb}{0.98828125,0.5625,0.13671875}
\definecolor{myotherblue}{rgb}{0.18359375,0.3515625,0.7578125}
\providecommand\ATLASlabel{Internal}
\renewcommand\sfdefault{phv}
\renewcommand\rmdefault{phv}
\renewcommand\ttdefault{pcr}
\font\greekcapstenrm=cmr10
\font\greekcapssevenrm=cmr7
\font\greekcapsfiverm=cmr5
\newfam\greekcapsfam
\textfont\greekcapsfam=\greekcapstenrm
\scriptfont\greekcapsfam=\greekcapssevenrm
\scriptscriptfont\greekcapsfam=\greekcapsfiverm
\let\tmpLambda=\Lambda \def\Lambda{{\fam\greekcapsfam\tmpLambda}}
\begin{tikzpicture}
\begin{axis}[
width = 0.8\textwidth,
width = 0.8\textwidth,
y=1cm,
xlabel = {\textsf{Parameter Value}},
clip = false,
ymin=-0.5,
ymax= 13.5,
xmin=0.5218352188569495,
xmax=1.6920683616453638,
minor tick num=4,
ytick style={draw=none},
yticklabels=\empty,
enlarge x limits=true,
xticklabel style={/pgf/number format/fixed},
scaled ticks=false
]
\node at ({rel axis cs:0,0}|-{axis cs:0,0}) [anchor = east]{\textsf{norm\_bkg}};
\node at ({rel axis cs:0,0}|-{axis cs:0,1}) [anchor = east]{\textsf{mu}};
\node at ({rel axis cs:0,0}|-{axis cs:0,2}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_9}};
\node at ({rel axis cs:0,0}|-{axis cs:0,3}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_8}};
\node at ({rel axis cs:0,0}|-{axis cs:0,4}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_7}};
\node at ({rel axis cs:0,0}|-{axis cs:0,5}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_6}};
\node at ({rel axis cs:0,0}|-{axis cs:0,6}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_5}};
\node at ({rel axis cs:0,0}|-{axis cs:0,7}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_4}};
\node at ({rel axis cs:0,0}|-{axis cs:0,8}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_3}};
\node at ({rel axis cs:0,0}|-{axis cs:0,9}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_2}};
\node at ({rel axis cs:0,0}|-{axis cs:0,10}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_10}};
\node at ({rel axis cs:0,0}|-{axis cs:0,11}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_1}};
\node at ({rel axis cs:0,0}|-{axis cs:0,12}) [anchor = east]{\textsf{gamma\_stat\_SR\_bin\_0}};
\node at ({rel axis cs:0,0}|-{axis cs:0,13}) [anchor = east]{\textsf{gamma\_stat\_CR\_bin\_0}};
\draw[color=red,solid] (axis cs:0.96608,0.00)--(axis cs:1.02975,0.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:0.99791,0.00){};
\node[anchor=west] at ({rel axis cs:1,0}|-{axis cs:0,0.0}) {$0.998^{+0.032}_{-0.032}$};
\draw[color=red,solid] (axis cs:0.52184,1.00)--(axis cs:1.69207,1.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:1.10187,1.00){};
\node[anchor=west] at ({rel axis cs:1,0}|-{axis cs:0,1.0}) {$1.1^{+0.6}_{-0.6}$};
\draw[color=red,solid] (axis cs:0.95813,2.00)--(axis cs:1.04271,2.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:1.00042,2.00){};
\node[anchor=west] at ({rel axis cs:1,0}|-{axis cs:0,2.0}) {$1.00^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.95197,3.00)--(axis cs:1.03637,3.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:0.99417,3.00){};
\node[anchor=west] at ({rel axis cs:1,0}|-{axis cs:0,3.0}) {$0.99^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.96731,4.00)--(axis cs:1.04759,4.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:1.00745,4.00){};
\node[anchor=west] at ({rel axis cs:1,0}|-{axis cs:0,4.0}) {$1.01^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.97100,5.00)--(axis cs:1.05142,5.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:1.01121,5.00){};
\node[anchor=west] at ({rel axis cs:1,0}|-{axis cs:0,5.0}) {$1.01^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.95597,6.00)--(axis cs:1.03721,6.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:0.99659,6.00){};
\node[anchor=west] at ({rel axis cs:1,0}|-{axis cs:0,6.0}) {$1.00^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.96513,7.00)--(axis cs:1.04544,7.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:1.00528,7.00){};
\node[anchor=west] at ({rel axis cs:1,0}|-{axis cs:0,7.0}) {$1.01^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.96977,8.00)--(axis cs:1.05069,8.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:1.01023,8.00){};
\node[anchor=west] at ({rel axis cs:1,0}|-{axis cs:0,8.0}) {$1.01^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.93833,9.00)--(axis cs:1.02097,9.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:0.97965,9.00){};
\node[anchor=west] at ({rel axis cs:1,0}|-{axis cs:0,9.0}) {$0.98^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.96196,10.00)--(axis cs:1.04405,10.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:1.00301,10.00){};
\node[anchor=west] at ({rel axis cs:1,0}|-{axis cs:0,10.0}) {$1.00^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.95952,11.00)--(axis cs:1.04276,11.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:1.00114,11.00){};
\node[anchor=west] at ({rel axis cs:1,0}|-{axis cs:0,11.0}) {$1.00^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.94300,12.00)--(axis cs:1.02794,12.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:0.98547,12.00){};
\node[anchor=west] at ({rel axis cs:1,0}|-{axis cs:0,12.0}) {$0.99^{+0.04}_{-0.04}$};
\draw[color=red,solid] (axis cs:0.99900,13.00)--(axis cs:1.00100,13.00);
\node[circle,fill,inner sep=2pt,color=red,draw=none] at (axis cs:1.00000,13.00){};
\node[anchor=west] at ({rel axis cs:1,0}|-{axis cs:0,13.0}) {$1.000^{+0.001}_{-0.001}$};
\end{axis}
\end{tikzpicture}
\end{document}