pgfplots-添加与数据值偏移的水平线

pgfplots-添加与数据值偏移的水平线

我想添加一条水平线,偏移量为第一条线的 y 轴加上 30。下面的代码可以工作,但有两点需要改进:

  1. y0 + 30 现在是硬编码,我们可以使用变量来读取条形零的值,然后添加偏移量。
  2. 水平线未到达右边界。
    \documentclass[border=10pt]{standalone}
    \usepackage{pgfplots}
    \pgfplotsset{width=7cm,compat=1.8}
    \usepackage{pgfplotstable}
    \begin{document}
    \pgfplotstableread[col sep=comma]{
    key,value
    apple,36.4
    peach,92.5
    balana,92.6
    berry,92.7
    jujube,98.2
    orange,98.3
    }\mydata
    \definecolor{mycolor}{HTML}{4f81db}
    \begin{tikzpicture}[font=\tiny]
        \begin{axis}[
            name=demo,
            xmin = -.8,xmax=6.5,
            ymin = 0,ymax=180,
            xtick=data,
            xticklabels from table={\mydata}{key},
            xlabel = {x},
            ytick={},
            ylabel = {y},
            x tick label style={rotate=40,anchor=east},
            xlabel style = {at={(axis description cs:0.5,-0.1)},anchor=north},
            ylabel style = {at={(axis description cs:-0.05,.5)},rotate=90,anchor=south},
            axis lines=left,
            width=8cm,
            height=6cm,
            enlargelimits=0,
          ]
    
          \addplot[
            ybar,
            fill=mycolor,
            draw=none,
            bar width=.85,
          ] table [
            x expr=\coordindex,
            y expr={\thisrowno{1}}
          ] {\mydata};
          
          \addplot[mark=none, red, samples=1] {66.4};      
          
        \end{axis}
    \end{tikzpicture}
    \end{document}

输出: 在此处输入图片描述

答案1

像这样 ?

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.8}
\usepackage{pgfplotstable}

\begin{document}
\pgfplotstableread[col sep=comma]{
    key,value
    apple,36.4
    peach,92.5
    banana,92.6
    berry,92.7
    jujube,98.2
    orange,98.3
    pomegrenate,102.3   
    pear, 100.3
}\mydata

\definecolor{mycolor}{HTML}{4f81db}


\pgfplotstablegetrowsof{\mydata}
\pgfmathsetmacro{\numrows}{\pgfplotsretval-1}

\begin{tikzpicture}[font=\tiny]
    \begin{axis}[
        name=demo,
        xmin = -.8, xmax=\numrows+0.8, %<= xmax adjusted 
        ymin = 0, ymax=180,
        xtick=data,
        xticklabels from table={\mydata}{key},
        xlabel = {x},
        ytick={},
        ylabel = {y},
        x tick label style={rotate=40,anchor=east},
        xlabel style = {at={(axis description cs:0.5,-0.1)},anchor=north},
        ylabel style = {at={(axis description cs:-0.05,.5)},rotate=90,anchor=south},
        axis lines=left,
        width=8cm,
        height=6cm,
        enlargelimits=0,
      ]

      \addplot[
        ybar,
        fill=mycolor,
        draw=none,
        bar width=.85,
        nodes near coords=\pgfmathprintnumber{\pgfplotspointmeta},
        nodes near coords align={vertical}
      ] table [
        x expr=\coordindex,
        y expr={\thisrowno{1}}
      ] {\mydata};

      \pgfplotstablegetelem{0}{value}\of{\mydata}
      \pgfmathsetmacro{\offset}{\pgfplotsretval + 30}

      \addplot[mark=none, red, samples=2, domain=-1:\numrows+0.8] {\offset}; %<= xmax adjusted 
      
    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容