一张图表中不同颜色的误差线

一张图表中不同颜色的误差线

我在一张图中有两个图。每个图都有自己的误差线。我该如何根据它们的图为误差线着色(即红色和蓝色)?例如,如果我将误差线颜色设置为红色,那么即使是图片中显示的蓝色图,最终误差线也会是红色的。 在此处输入图片描述

这是我使用的代码

\documentclass{standalone}

\usepackage{amsmath,amssymb}
\usepackage{graphicx}% Include figure files
\usepackage{dcolumn}% Align table columns on decimal point
\usepackage{bm}% bold math
\usepackage[numbers,super,comma,sort&compress]{natbib}
\usepackage{pgfplots}
\usepackage{pgfplotstable}%fitting functions
\usepackage{tikz}
\usetikzlibrary{tikzmark,patterns}
\usetikzlibrary{arrows,arrows.meta,shapes.arrows}

\pgfplotsset{compat=newest,
    every axis x label/.append style={black},
    every axis y label/.append style={black},
    every major grid/.append style={gray!50,line width=0.4pt},
    every minor grid/.append style={gray!50},
    every major tick/.append style={black,line width=0.6pt},%thick
    every minor tick/.append style={black,line width=0.6pt},
    every x tick label/.append style={black},
    every y tick label/.append style={black}
}

\pgfdeclareplotmark{fat-}
{%
    \pgfsetlinewidth{0.4}
    \pgfpathmoveto{\pgfqpoint{\pgfplotmarksize}{0pt}}%
    \pgfpathlineto{\pgfqpoint{-\pgfplotmarksize}{0pt}}%
    \pgfusepathqstroke
}%

\pgfplotsset{/pgfplots/error bars/error bar style={red,mark size=1.5},/pgfplots/error bars/error mark={fat-}}
%\pgfplotsset{/pgfplots/label shift={0pt}}
             
\pgfplotsset{error bars/.cd,
    x dir=both, x explicit,
    y dir=both, y explicit,
    }
 
\pgfplotsset{compat=newest}
\pgfplotsset{label style={font=\Large},
            tick label style={font=\large}}

            
\begin{document}

\begin{tikzpicture}%[baseline={([yshift=-20ex]current bounding box.north)}]
\begin{axis}[enable tick line clipping=false,
    enable tick line clipping=false,
    clip mode=individual,
    width=5.5cm,
    height=4.5cm,
    axis line style={line width=0.6pt},
%    axis on top=true,
    x label style={at=(ticklabel cs:0.5),anchor=near ticklabel},
    xmin=1,xmax=4,
    xtick={1,2,...,4},
    xtick pos=bottom,
    xtick align=outside,
    y label style={at=(ticklabel cs:0.5),anchor=near ticklabel},
    ymin=0,ymax=2,
    ytick={0,1,...,3},
    ytick pos=left,
    ytick align=outside,
]

\addplot [only marks,mark=*,mark options={scale=0.7,blue,fill=white}] table [x=x,y=y,y error=ey] {
x       y         ey
1       0.59      0.059
1.2     0.717     0.0717
1.4     0.844     0.0844
1.6     0.964     0.0964
1.8     1.088     0.0088
2       1.211     0.0211
2.2     1.34      0.034
2.4     1.469     0.0469
2.6     1.591     0.0591
2.8     1.708     0.0708
3       1.8       0.08
3.2     1.836     0.0836
3.4     1.67      0.067
3.6     0.874     0.0874
3.8     0.093     0.093
};
    
\addplot [only marks,mark=*,mark options={scale=0.7,red,fill=white}] table [x=x,y=y,y error=ey] {
x       y           ey
1       0.519       0.0519
1.2     0.568       0.0568
1.4     0.779       0.0479
1.6     0.847       0.0547
1.8     0.945       0.0945
2       1.013       0.0713
2.2     1.141       0.0841
2.4     1.266       0.0666
2.6     1.355       0.0555
2.8     1.419       0.0419
3       1.512       0.0512
3.2     1.602       0.0612
3.4     1.685       0.0685
3.6     1.741       0.0741
3.8     1.851       0.0851
};


\end{axis}
\end{tikzpicture}

\end{document}```

答案1

error bars/error bar style={blue}您可以通过添加选项将误差线颜色更改为蓝色\addplot。也就是说,对于包含蓝色数据点的绘图,您应该有

\addplot [
  only marks,mark=*,
  mark options={scale=0.7,blue,fill=white},
  error bars/error bar style={blue} % ⟵ this was the line I added
] table [x=x,y=y,y error=ey] {...}

包含红色数据点和蓝色数据点的图。所有红色数据点都有红色误差线,所有蓝色数据点都有蓝色误差线。

另外,仅供参考,如果您加载 PGFPlots,则不需要\usepackage{tikz}。毕竟,您制作的每个图都被包围\begin{tikzpicture} ... \end{tikzpicture},这表明 TiZ 可以正常工作,无需明确加载包:)

相关内容