PGFplots:带颜色条的条形图

PGFplots:带颜色条的条形图

我正在尝试制作一个二维条形图,其中条形使用颜色条根据另一个第三个变量着色,即条形的颜色与显示的变量无关,但与其他变量有关。

我之前曾设法得到颜色匹配的散点图,但不确定如何用适当的颜色填充条形图。

\documentclass[tikz,border=3.14pt]{standalone}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{colormaps}

\pgfplotsset{colormap access=direct}
\tikzset{mycolor/.code={\pgfplotscolormapdefinemappedcolor{#1}}}

\begin{document}


\begin{tikzpicture}
\begin{axis}[colorbar, colormap/jet,
colorbar style={
            ylabel={Years},
            ylabel style={anchor=west, text width=3cm, rotate=270},
        },
    width=15cm,
height=9cm,
    legend style={at={(0.5,-0.15)},
      anchor=north,legend columns=-1},
    xlabel={$x$},
    symbolic y coords={A, B, C, D, E, F, G, H, I, J, K, L, M},
    ylabel = {$y$},
    xtick={0,5,10,15,20,25,20,35},
    xmax=35,
    xmin=0,
    ytick={A, B, C, D, E, F, G, H, I, J, K, L, M},
          bar shift=0pt,
    ]

\addplot+ [xbar, point meta=\thisrow{years}, 
    visualization depends on={\thisrow{years} \as \myvalue},fill= mapped color]  table {
    x       y     years
    22.6   A      1.800
    31.7   B      1.300
    4.8    C      2.000
    16.7   D      2.300
    3.9    E      4.100
    0.244333 F    0.61
    3.622143 G    9.1
    0.566104 H    57
    2.98 I       29.8
    1.85 J        3.7
    0.334255 K    6.7
    1.9 L         38
    0.395 M        0
  };

\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

所以在这张图片中,“G”应该是蓝色,但“I”应该是绿色。

提前谢谢您:)如果答案实在太简单,请原谅。

答案1

您可以使用Jake 的解决方案 #1, 哪个BambOo 链接到\coordindex,如果用 替换符号坐标,则几乎一一对应。

\documentclass[tikz,border=3.14pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{colormaps}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    colorbar, colormap/jet,
    colorbar style={
            ylabel={Years},
            ylabel style={anchor=west, text width=3cm, rotate=270},
        },
    scatter,
    scatter src=x,
    only marks,
    clip mode=individual,
    scatter/@pre marker code/.append code={
            \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
            \pgfmathsetmacro\negheight{-\pgfplotspointmeta}         
            \fill [draw=black] 
             (axis direction cs:0,0.3) rectangle (axis direction cs:\negheight,-0.3);
            \pgfplotsset{mark=none}
        },
    xmin=0,
    width=15cm,
    height=9cm,
    legend style={at={(0.5,-0.15)},
      anchor=north,legend columns=-1},
    xlabel={$x$},
    %symbolic y coords={A, B, C, D, E, F, G, H, I, J, K, L, M},
    ylabel = {$y$},
    xtick={0,5,10,15,20,25,20,35},
    xmax=35,
    xmin=0,
    ytick={0,...,12},
    yticklabels={A, B, C, D, E, F, G, H, I, J, K, L, M},
  ]
  \addplot table[x=x,y expr=\coordindex] {
    x       y     years
    22.6   A      1.800
    31.7   B      1.300
    4.8    C      2.000
    16.7   D      2.300
    3.9    E      4.100
    0.244333 F    0.61
    3.622143 G    9.1
    0.566104 H    57
    2.98 I       29.8
    1.85 J        3.7
    0.334255 K    6.7
    1.9 L         38
    0.395 M        0
  };
  \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

使用符号坐标可能也是可行的,但需要付出更多努力。

附录:这是一种拼写方法BambOo 的绝妙想法

\documentclass[tikz,border=3.14pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{colormaps}
\pgfplotsset{/pgfplots/colormap={jet}{rgb255(0cm)=(0,0,128) rgb255(1cm)=(0,0,255)
rgb255(3cm)=(0,255,255) rgb255(5cm)=(255,255,0) rgb255(7cm)=(255,0,0)
rgb255(8cm)=(128,0,0)}}
\newsavebox\JetColorBar
% convert 'hot' -> \result 
\pgfplotscolormaptoshadingspec{jet}{12.5cm}\jetresult
% define and use a shading in pgf: 
\def\tempb{\pgfdeclarehorizontalshading{tempshading}{1cm}}% 
% where '\result' is inserted as last argument: 
\expandafter\tempb\expandafter{\jetresult}% 
\sbox\JetColorBar{\pgfuseshading{tempshading}}%
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    colorbar, colormap/jet,
    colorbar style={
            ylabel={Years},
            ylabel style={anchor=west, text width=3cm, rotate=270},
        },
    scatter,
    scatter src=x,
    only marks,
    clip mode=individual,
    scatter/@pre marker code/.append code={
            \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
            \pgfmathsetmacro\negheight{-\pgfplotspointmeta} 
            \clip
             (axis direction cs:0,0.3) rectangle (axis direction cs:\negheight,-0.3);
            \path  (axis direction cs:\negheight,0)
             node[anchor=west,outer sep=0pt,inner sep=0pt]{\usebox\JetColorBar};        
            \pgfplotsset{mark=none}
        },
    xmin=0,
    width=15cm,
    height=9cm,
    legend style={at={(0.5,-0.15)},
      anchor=north,legend columns=-1},
    xlabel={$x$},
    %symbolic y coords={A, B, C, D, E, F, G, H, I, J, K, L, M},
    ylabel = {$y$},
    xtick={0,5,10,15,20,25,20,35},
    xmax=35,
    xmin=0,
    ytick={0,...,12},
    yticklabels={A, B, C, D, E, F, G, H, I, J, K, L, M},
  ]
  \addplot table[x=x,y expr=\coordindex] {
    x       y     years
    22.6   A      1.800
    31.7   B      1.300
    4.8    C      2.000
    16.7   D      2.300
    3.9    E      4.100
    0.244333 F    0.61
    3.622143 G    9.1
    0.566104 H    57
    2.98 I       29.8
    1.85 J        3.7
    0.334255 K    6.7
    1.9 L         38
    0.395 M        0
  };
  \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

或者与point meta不同x

\documentclass[tikz,border=3.14pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{colormaps}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    colorbar, colormap/jet,
    colorbar style={
            ylabel={Years},
            ylabel style={anchor=west, text width=3cm, rotate=270},
        },
    scatter,
    scatter src=x,
    only marks,
    clip mode=individual,
    visualization depends on=\thisrow{x}\as\myx,
    scatter/@pre marker code/.append code={
            \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
            \pgfmathsetmacro\negheight{-1*\myx}         
            \fill [draw=black] 
             (axis direction cs:0,0.3) rectangle (axis direction cs:\negheight,-0.3);
            \pgfplotsset{mark=none}
        },
    xmin=0,
    width=15cm,
    height=9cm,
    legend style={at={(0.5,-0.15)},
      anchor=north,legend columns=-1},
    xlabel={$x$},
    %symbolic y coords={A, B, C, D, E, F, G, H, I, J, K, L, M},
    ylabel = {$y$},
    xtick={0,5,10,15,20,25,20,35},
    xmax=35,
    xmin=0,
    ytick={0,...,12},
    yticklabels={A, B, C, D, E, F, G, H, I, J, K, L, M},
    point meta=\thisrow{years}
  ]
  \addplot table[x=x,y expr=\coordindex] {
    x       y     years
    22.6   A      1.800
    31.7   B      1.300
    4.8    C      2.000
    16.7   D      2.300
    3.9    E      4.100
    0.244333 F    0.61
    3.622143 G    9.1
    0.566104 H    57
    2.98 I       29.8
    1.85 J        3.7
    0.334255 K    6.7
    1.9 L         38
    0.395 M        0
  };
  \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容