我正在尝试创建一个带有 symlog 轴和黑色误差线的条形图。但是,到目前为止,我得到的最好的结果如下。现在我的误差线消失在我的条形图后面,并且由于某种原因颜色不是黑色。我尝试更改 的顺序\addplot
,但随后我的图例变成了蓝色和红色,而不是黑色和绿色。
如何才能使我的误差线变黑、位于前面并带有正确的图例?
\documentclass{standalone}
\usepackage{xcolor}
\definecolor{pythonGreen}{RGB}{0,128,0}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{patterns}
\usetikzlibrary{math}
\tikzmath
{
function symlog(\x,\a){
\yLarge = ((\x>\a) - (\x<-\a)) * (ln(max(abs(\x/\a),1)) + 1);
\ySmall = (\x >= -\a) * (\x <= \a) * \x / \a ;
return \yLarge + \ySmall ;
};
function symexp(\y,\a){
\xLarge = ((\y>1) - (\y<-1)) * \a * exp(abs(\y) - 1) ;
\xSmall = (\y>=-1) * (\y<=1) * \a * \y ;
return \xLarge + \xSmall ;
};
}
\begin{document}
\begin{tikzpicture}
\def\basis{1}
\pgfplotsset
{
y coord trafo/.code={\pgfmathparse{symlog(#1,\basis)}\pgfmathresult},
y coord inv trafo/.code={\pgfmathparse{symexp(#1,\basis)}\pgfmathresult},
yticklabel style={/pgf/number format/.cd,int detect,precision=2},
}
\pgfplotstableread{ % x, y1, y2, y_err1, y_err2
0 0.78 -1.03 0.21 1.07
1 -540 -1150 0.06 216
2 2.6 5.15 0.08 1.16
3 7.36 9.86 0.004 0.55
4 0.47 -0.03 0.75 0.76
5 -0.87 -0.87 0.99 1.06
6 0.10 -0.33 0.24 0.26
}\dataset
\begin{axis} [width=\linewidth,
ybar = 0cm,
bar width = 10pt,
xtick = data,
scaled ticks = base 10:0,
ytick = {-10000, -1000, -100, -10, -1, 0, 1, 10, 100, 1000, 10000},
scaled y ticks=false,
minor ytick = {-9000, -8000, ..., -2000, -900, -800, ..., -200, -90, -80, ..., -20, -9, -8, ..., -2, -.9, -.8, ..., .9, 2, 3, ..., 9, 20, 30, ..., 90, 200, 300, ..., 900, 2000, 3000, ..., 9000},
ymajorgrids,
yminorgrids,
xmajorgrids,
xticklabels={A, B, C, D, E, F, G},
x tick label style={rotate=90},
ylabel=\tiny{Y-Value},
tick label style={font=\tiny},
legend style={nodes=right, font=\tiny},
legend pos = south east
]
\addplot+[forget plot, mark=none, draw=black, fill=black]
plot[error bars/.cd, y dir=plus, y explicit]
table[x=0, y=1, y error expr = {symlog(\thisrow{1} + \thisrow{3}, \basis) - symlog(\thisrow{1}, \basis)}]\dataset;
\addplot+[forget plot, mark=none,xticklabels=\empty, draw=none, fill=none]
plot[error bars/.cd, y dir=minus, y explicit]
table[x=0, y=1, y error expr = {symlog(\thisrow{1}, \basis) - symlog(\thisrow{1} - \thisrow{3}, \basis)}]\dataset;
\addplot+[draw=black, fill=black!75]
table[x index=0, y index=1] \dataset;
\addplot+[forget plot, mark=none, draw=none, fill=none]
plot[error bars/.cd, y dir=plus, y explicit]
table[x=0,y=2,y error expr = {symlog(\thisrow{2} + \thisrow{4}, \basis) - symlog(\thisrow{2}, \basis)}]\dataset;
\addplot+[forget plot, mark=none, xticklabels=\empty, draw=none, fill=none]
plot[error bars/.cd, y dir=minus, y explicit]
table[x=0,y=2,y error expr = {symlog(\thisrow{2},\basis)-symlog(\thisrow{2}-\thisrow{4},\basis)}]\dataset;
\addplot+[draw=black, fill=pythonGreen]
table[x index=0, y index=2] \dataset;
\legend {data1, data2};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
不确定到底发生了什么,但我认为也许你可以简化一些事情。不要为误差线单独绘制图表,而是为\addplot
每个数据集绘制一个图表。你可以在同一个图中分别指定y error plus expr
和。after会使它们变黑。y error minus expr
error bar style={black}
error bars/.cd
其他的建议:
- 你
ycoord (inv) trafo
不应该\pgfmathresult
在最后有。这些代码应该只设置\pgfmathresult
,但\pgfmathparse
实际上并没有使用它。你在日志中收到的许多关于字符不在 nullfont 中的警告就是这个原因。 \tiny
不是接受参数的宏,因此为了限制效果,请勿{\tiny foo}
使用\tiny{foo}
。或者,在这种情况下,使用ylabel style={font=\tiny}
。- 在您的两个
\addplot
s 中,您有xticklabels
。这是错误的,这是属于轴的设置。 - 我认为大多数情况下建议使用
\newcommand
over\def
,这样您就不会意外覆盖现有的宏。
\documentclass[border=5mm]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}
\usetikzlibrary{patterns}
\usetikzlibrary{math}
\definecolor{pythonGreen}{RGB}{0,128,0}
\tikzmath
{
function symlog(\x,\a){
\yLarge = ((\x>\a) - (\x<-\a)) * (ln(max(abs(\x/\a),1)) + 1);
\ySmall = (\x >= -\a) * (\x <= \a) * \x / \a ;
return \yLarge + \ySmall ;
};
function symexp(\y,\a){
\xLarge = ((\y>1) - (\y<-1)) * \a * exp(abs(\y) - 1) ;
\xSmall = (\y>=-1) * (\y<=1) * \a * \y ;
return \xLarge + \xSmall ;
};
}
\begin{document}
\begin{tikzpicture}
\newcommand\basis{1}
\pgfplotsset
{
y coord trafo/.code={\pgfmathparse{symlog(#1,\basis)}},
y coord inv trafo/.code={\pgfmathparse{symexp(#1,\basis)}},
yticklabel style={/pgf/number format/.cd,int detect,precision=2},
}
\pgfplotstableread{ % x, y1, y2, y_err1, y_err2
0 0.78 -1.03 0.21 1.07
1 -540 -1150 0.06 216
2 2.6 5.15 0.08 1.16
3 7.36 9.86 0.004 0.55
4 0.47 -0.03 0.75 0.76
5 -0.87 -0.87 0.99 1.06
6 0.10 -0.33 0.24 0.26
}\dataset
\begin{axis} [width=\linewidth,
ybar = 0cm,
bar width = 10pt,
xtick = data,
scaled ticks = base 10:0,
ytick = {-10000, -1000, -100, -10, -1, 0, 1, 10, 100, 1000, 10000},
scaled y ticks=false,
minor ytick = {-9000, -8000, ..., -2000, -900, -800, ..., -200, -90, -80, ..., -20, -9, -8, ..., -2, -.9, -.8, ..., .9, 2, 3, ..., 9, 20, 30, ..., 90, 200, 300, ..., 900, 2000, 3000, ..., 9000},
ymajorgrids,
yminorgrids,
xmajorgrids,
xticklabels={A, B, C, D, E, F, G},
x tick label style={rotate=90},
ylabel={Y-Value},
ylabel style={font=\tiny},
tick label style={font=\tiny},
legend style={nodes=right, font=\tiny},
legend pos = south east
]
\addplot+[
draw=black,
fill=black!75,
error bars/.cd,
y dir=both,
y explicit,
error bar style={black}
]
table[x index=0,
y index=1,
y error plus expr = {symlog(\thisrowno{1} + \thisrowno{3}, \basis) - symlog(\thisrowno{1}, \basis)},
y error minus expr = {symlog(\thisrowno{1}, \basis) - symlog(\thisrowno{1} - \thisrowno{3}, \basis)}
] \dataset;
\addplot+[
draw=black,
fill=pythonGreen,
error bars/.cd,
y dir=both,
y explicit,
error bar style={black}
]
table[x index=0,
y index=1,
y error plus expr = {symlog(\thisrowno{2} + \thisrowno{4}, \basis) - symlog(\thisrowno{2}, \basis)},
y error minus expr = {symlog(\thisrowno{2},\basis)-symlog(\thisrowno{2}-\thisrowno{4},\basis)}
] \dataset;
\legend {data1, data2};
\end{axis}
\end{tikzpicture}
\end{document}