旋转 tikzpicture 的轴以水平方向显示条形图

旋转 tikzpicture 的轴以水平方向显示条形图

例子:

\documentclass[10pt,border=3mm,tikz]{standalone}
\usepackage{pgfplots}

\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
    %

\begin{document}
    \begin{tikzpicture}
    
    \begin{axis}[%
    width=3.208in,
    height=2.406in,
    at={(2.042in,1.531in)},
    scale only axis,
    xmin=-0.0732000000000001,
    xmax=1.2732,
    xlabel style={font=\color{white!15!black}},
    xlabel={x},
    ymin=-0.0732000000000001,
    ymax=1.2732,
    ylabel style={font=\color{white!15!black}},
    ylabel={y},
    axis background/.style={fill=white}
    ]
    \addplot [color=mycolor1, only marks, mark=o, mark options={solid, mycolor1}, forget plot]
      table[row sep=crcr]{%
    0.767861601282467   0.3458589136031\\
    0.756680548341953   0.95864147735458\\
    0.787814379362316   0.615634775664367\\
    0.482346943930302   0.140139439118997\\
    0.788420766278634   0.252324016431217\\
    0.609541593446003   0.833025781488994\\
    0.598497835040654   0.447882785562502\\
    0.962167060742761   0.297077799336121\\
    0.0672914051246174  0.829030827812664\\
    0.0685031200093895  0.257294078290989\\
    0.979842293331438   0.250600951574307\\
    0.679902746964661   0.934100057924172\\
    0.510673213422503   0.650716813847921\\
    0.650982431174077   0.324690703869729\\
    0.587879973826315   0.167319776765514\\
    0.25698833084105    0.271900912155622\\
    0.986921712124531   0.323353384105197\\
    0.624261860962849   0.330737351577275\\
    0.871562473469132   0.701588447496151\\
    0.148334408564265   0.503016243850673\\
    0.267655595945384   0.838096145967706\\
    0.43063392865761    0.877759753558797\\
    0.220588538068738   0.897891019472078\\
    0.729660418601116   0.899526138648854\\
    0.708895728868174   0.291546924111268\\
    };
    \end{axis}
    
    \begin{axis}[%
    width=1.266in,
    height=2.406in,
    at={(0.19in,1.531in)},
    scale only axis,
    xmin=-0.0732000000000001,
    xmax=1.2732,
    ymin=-0.012,
    ymax=1.212,
    axis line style={draw=none},
    ticks=none
    ]
    \addplot[ybar interval, fill=mycolor1, fill opacity=0.6, draw=black, area legend] table[row sep=crcr] {%
    x   y\\
    0   1.06666666666667\\
    0.3 0.8\\
    0.6 1.2\\
    0.9 0.266666666666667\\
    1.2 0.266666666666667\\
    };
    \addplot [color=black, forget plot]
      table[row sep=crcr]{%
    -0.0600000000000001 0\\
    1.26    0\\
    };
    \end{axis}
    
    \begin{axis}[%
    width=3.208in,
    height=0.827in,
    at={(2.042in,0.216in)},
    scale only axis,
    xmin=-0.0732000000000001,
    xmax=1.2732,
    ymin=-0.0146666666666667,
    ymax=1.48133333333333,
    axis line style={draw=none},
    ticks=none
    ]
    \addplot[ybar interval, fill=mycolor1, fill opacity=0.6, draw=black, area legend] table[row sep=crcr] {%
    x   y\\
    0   0.8\\
    0.3 0.666666666666667\\
    0.6 1.46666666666667\\
    0.9 0.4\\
    1.2 0.4\\
    };
    \addplot [color=black, forget plot]
      table[row sep=crcr]{%
    -0.0600000000000001 0\\
    1.26    0\\
    };
    \end{axis}
    
    \begin{axis}[%
    width=5.833in,
    height=4.375in,
    at={(0in,0in)},
    scale only axis,
    xmin=0,
    xmax=1,
    ymin=0,
    ymax=1,
    axis line style={draw=none},
    ticks=none,
    axis x line*=bottom,
    axis y line*=left
    ]
    \end{axis}
    \end{tikzpicture}%
\end{document}

产生输出

在此处输入图片描述

我想要的是让 y 轴上的条形图沿水平方向延伸,而不是沿垂直方向延伸。也就是说,以某种方式将左侧的条形图顺时针旋转 90 度。

有没有办法做到这一点?

答案1

虽然还不完美,但是你也可以旋转轴:

...
    % ~~~ left histo ~~~~~~~~~~~~~~~~~~~~~~
    \begin{axis}[%
    width=1.266in,
    height=2.406in,
    at={(0.19in,1.531in)},
    scale only axis,
    xmin=-0.0732000000000001,
    xmax=1.2732,
    ymin=-0.012,
    ymax=1.212,
    axis line style={draw=none},
    ticks=none,
    rotate=90   % <<<<<<<<<<<<<<<<<
    ]
...

结果

% https://tex.stackexchange.com/questions/714865/rotate-axis-of-tikzpicture-to-show-bars-in-horizontal-direction

\documentclass[10pt,border=3mm,tikz]{standalone}
\usepackage{pgfplots}

\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
    %

\begin{document}
 \begin{tikzpicture}
    % ~~~ Scatterplot in the middle ~~~~~~~~~~~
    \begin{axis}[%
    width=3.208in,
    height=2.406in,
    at={(2.042in,1.531in)},
    scale only axis,
    xmin=-0.0732000000000001,
    xmax=1.2732,
    xlabel style={font=\color{white!15!black}},
    xlabel={x},
    ymin=-0.0732000000000001,
    ymax=1.2732,
    ylabel style={font=\color{white!15!black}},
    ylabel={y},
    axis background/.style={fill=white}
    ]
    \addplot [color=mycolor1, only marks, mark=o, mark options={solid, mycolor1}, forget plot]
      table[row sep=crcr]{%
    0.767861601282467   0.3458589136031\\
    0.756680548341953   0.95864147735458\\
    0.787814379362316   0.615634775664367\\
    0.482346943930302   0.140139439118997\\
    0.788420766278634   0.252324016431217\\
    0.609541593446003   0.833025781488994\\
    0.598497835040654   0.447882785562502\\
    0.962167060742761   0.297077799336121\\
    0.0672914051246174  0.829030827812664\\
    0.0685031200093895  0.257294078290989\\
    0.979842293331438   0.250600951574307\\
    0.679902746964661   0.934100057924172\\
    0.510673213422503   0.650716813847921\\
    0.650982431174077   0.324690703869729\\
    0.587879973826315   0.167319776765514\\
    0.25698833084105    0.271900912155622\\
    0.986921712124531   0.323353384105197\\
    0.624261860962849   0.330737351577275\\
    0.871562473469132   0.701588447496151\\
    0.148334408564265   0.503016243850673\\
    0.267655595945384   0.838096145967706\\
    0.43063392865761    0.877759753558797\\
    0.220588538068738   0.897891019472078\\
    0.729660418601116   0.899526138648854\\
    0.708895728868174   0.291546924111268\\
    };
    \end{axis}
    
    % ~~~ left histo ~~~~~~~~~~~~~~~~~~~~~~
    \begin{axis}[%
    width=1.266in,
    height=2.406in,
    at={(0.19in,1.531in)},
    scale only axis,
    xmin=-0.0732000000000001,
    xmax=1.2732,
    ymin=-0.012,
    ymax=1.212,
    axis line style={draw=none},
    ticks=none,
    rotate=90   % <<<<<<<<<<<<<<<<<
    ]
    \addplot[ybar interval, 
    fill=mycolor1, fill opacity=0.6, draw=black, area legend] table[row sep=crcr] {%
    x   y\\
    0   1.06666666666667\\
    0.3 0.8\\
    0.6 1.2\\
    0.9 0.266666666666667\\
    1.2 0.266666666666667\\
    };
%    % ~~~ just a line ~~~
%    \addplot [color=black, forget plot]
%      table[row sep=crcr]{%
%    -0.0600000000000001 0\\
%    1.26    0\\
%    };
    \end{axis}
    
    % ~~~ lower histo ~~~~~~~~~~~~~~~
    \begin{axis}[%
    width=3.208in,
    height=0.827in,
    at={(2.042in,0.216in)},
    scale only axis,
    xmin=-0.0732000000000001,
    xmax=1.2732,
    ymin=-0.0146666666666667,
    ymax=1.48133333333333,
    axis line style={draw=none},
    ticks=none
    ]
    \addplot[ybar interval, fill=mycolor1, fill opacity=0.6, draw=black, area legend] table[row sep=crcr] {%
    x   y\\
    0   0.8\\
    0.3 0.666666666666667\\
    0.6 1.46666666666667\\
    0.9 0.4\\
    1.2 0.4\\
    };
    \addplot [color=black, forget plot]
      table[row sep=crcr]{%
    -0.0600000000000001 0\\
    1.26    0\\
    };
    \end{axis}
    
    % ~~~ not needed ~~~~~~~~~~~~~
%    \begin{axis}[%
%    width=5.833in,
%    height=4.375in,
%    at={(0in,0in)},
%    scale only axis,
%    xmin=0,
%    xmax=1,
%    ymin=0,
%    ymax=1,
%    axis line style={draw=none},
%    ticks=none,
%    axis x line*=bottom,
%    axis y line*=left
%    ]
%    \end{axis}
  \end{tikzpicture}%
\end{document}

相关内容