在具有有意义单位的一维对数轴上表示特殊的 TikZ 节点

在具有有意义单位的一维对数轴上表示特殊的 TikZ 节点

这个问题旨在建立在提供的出色解决方案之上这里。NB:链接到的情节是我们正在构建的一个很好的例子。

为了简单起见,请考虑以下示例:

\documentclass[tikz]{standalone}
\usetikzlibrary{calc,fadings}
\usepackage{miscchemsym}

\def\solutelength{1}
\def\soluteheight{.5}
\def\solutevariabilityhigh{0}
\def\solutevariabilitylow{0}
\def\solutelabelcolor{black}
\tikzset{
    pics/solute/.default={s-s},
    pics/solute/.style args={#1-#2}{
        code={
            \def\hasfading{f}
            \def\stylehigh{#2}
            \def\stylelow{#1}
            \coordinate (-north) at ({.5*\solutelength},\soluteheight);
            \coordinate (-south) at ({.5*\solutelength},0);
            \coordinate (-south west) at (0,0);
            \coordinate (-south east) at (\solutelength,0);
            \coordinate (-north west) at (0,\soluteheight);
            \coordinate (-north east) at (\solutelength,\soluteheight);
            \coordinate (-south west reduced) at ({0+\solutevariabilitylow},0);
            \coordinate (-south east reduced) at ({\solutelength-\solutevariabilityhigh},0);
            \coordinate (-north west reduced) at ({0+\solutevariabilitylow},\soluteheight);
            \coordinate (-north east reduced) at ({\solutelength-\solutevariabilityhigh},\soluteheight);
            \ifx\stylelow\hasfading
                \fill (-south west reduced) -- (-south) -- (-north) -- (-north west reduced) -- cycle;
                \fill[left color=.!0, right color=.] (-south west) -- (-south west reduced) -- (-north west reduced) -- (-north west) -- cycle;
            \else
                \fill (-south west reduced) -- (-south) -- (-north) -- (-north west) -- cycle;
            \fi
            \ifx\stylehigh\hasfading
                \fill (-south) -- (-south east reduced) -- (-north east reduced) -- (-north) -- cycle;
                \fill[left color=., right color=.!0] (-south east reduced) -- (-south east) -- (-north east) -- (-north east reduced) -- cycle;
            \else
                \fill (-south) -- (-south east reduced) -- (-north east) -- (-north) -- cycle;
            \fi
            \node[text=\solutelabelcolor] (-label) at ($(-south west) !0.5! (-north east)$) {\tikzpictext\strut};
        },
    },
    solute/length/.code={
        \def\solutelength{#1}
    },
    solute/height/.code={
        \def\soluteheight{#1}
    },
    solute/variability high/.code={
        \def\solutevariabilityhigh{#1}
    },
    solute/variability low/.code={
        \def\solutevariabilitylow{#1}
    },
    solute/label color/.code={
        \def\solutelabelcolor{#1}
    },
    solute/label/.style={
        pic text={#1}
    },
}

% the interface

% 1 - low concentration
% 2 - somewhat arbitrary, vertical positioning, user has to manage these manually
% 3 - color
% 4 - length (equal to abs max concentration - absolute min concentration)


\begin{document}

    \begin{tikzpicture}
        \draw[-stealth, thick] (0,0) -- (11,0);
        \foreach \x in {0,...,10} {
            \draw (\x,.1) -- ++(0,-.2);
        }
        
        \draw (0,.2) pic[yellow, solute/length=6, solute/label={a}] {solute};
        
        \draw (1,-.7) pic[cyan, solute/length=5, solute/label={b}] {solute};        

    \end{tikzpicture}

\end{document}

x 轴的范围从 1 nM 到 100 mM(大约 8 个数量级)。将出现以下刻度标记,并带有标签,表示 1 nM(M 常见单位的 1e-9)、1 {\mu}M(M 常见单位的 1e-6)、1 mM(M 常见单位的 1e-3)和 100 mM(M 常见单位的 1e-1)。可以绘制其他刻度标记 - 可以根据默认值和美观度来决定。

用户将提供与 nM 或 M 相对应的值,以对编译器来说最方便的数字为准。

问题在于,如何最好地调整 MWE 以包含以下内容:

\begin{axis}[
    xmode=log,
    xmin=1e-9, xmax=1e-1,
    axis x line=bottom,% only show the bottom x axis
    hide y axis,    
    ymin=0,ymax=5]

% calls to various nodes placed here.

end{axis}

以及将值作为与明确且有意义的工程单位相关联的参数进行处理。

答案1

这是一个解决方案。我没有使用另一个问题中链接的方法,因为我觉得它太复杂了。使用 很重要\pgfplotsset{compat=newest},因为这允许 中的坐标\solute使用 的坐标系axis

我认为不应该支持真实世界的单位,因为这增加了很多复杂性,并且对于用户来说转换为小数等是足够合理的。您还可以axis根据需要探索更多关于使用标签等设置样式的信息。

在各个形状相交的地方可能会有微小的线条伪影,但这是一个已知问题这基本上是一个渲染舍入错误。此外,ymin/ymax参数会被忽略,但如果你忽略它们,它就会中断。不知道那里发生了什么……

编辑:添加了 x 轴标签。

平均能量损失

\documentclass[margin=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc,fadings}
\usepackage{xifthen}
\newlength{\dx}\setlength{\dx}{10mm}
\newlength{\dy}\setlength{\dy}{5mm}
\newlength{\hy}\setlength{\hy}{0.5\dy}
\newcommand{\solute}[9]{% args:
  % 1: name, 2: color, 3: y-height,
  % 4: left (f)ade or (w)edge, 5: leftmost-x,  6: end left fade/wedge,
  % 7: start right fade/wedge, 8: rightmost-x, 9: right (f)ade or (w)edge
  \fill[fill=#2] (#6,#3\dy-\hy) rectangle (#7,#3\dy+\hy) node[pos=.5] {#1};
  \ifthenelse{\equal{#4}{f}}
    {\fill[#2,path fading=west] (#5,#3\dy-\hy) rectangle (#6,#3\dy+\hy);}
    {\fill[#2] (#5,#3\dy+\hy) -- (#6,#3\dy+\hy) -- (#6,#3\dy-\hy) -- cycle;}
  \ifthenelse{\equal{#9}{f}}
    {\fill[#2,path fading=east] (#7,#3\dy-\hy) rectangle (#8,#3\dy+\hy);}
    {\fill[#2] (#8,#3\dy+\hy) -- (#7,#3\dy+\hy) -- (#7,#3\dy-\hy) -- cycle;}
}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}[axis lines = middle,
      ymin=0, ymax=0, hide y axis,
      xmin=1, xmax=1e4, xmode = log]
    \solute{A}{green} { 2}{f}{10}{100}{1500}{4000}{w}
    \solute{B}{yellow}{ 1}{w}{ 1}{ 20}{ 300}{ 500}{f}
    \node[anchor=north] at (1e2,-\dy){Concentration [mM]};
    \end{axis}
  \end{tikzpicture}
\end{document}

结果

结果

相关内容