如何调整右侧第二个 y 轴上的刻度和标签

如何调整右侧第二个 y 轴上的刻度和标签

是否可以调整右侧 y 轴上的标签和刻度?我制作的图像是这样的

在此处输入图片描述

而它看起来应该是这样的:

在此处输入图片描述

\definecolor{qqzzqq}{rgb}{0.,0.6,0.}
\definecolor{yqqqqq}{rgb}{0.5019607843137255,0.,0.}
\begin{tikzpicture}[line cap=round,line join=round,>=stealth,x=1.9090909090909092cm,y=1.2195121951219514cm]
\begin{axis}[
x=1.9090909090909092cm,y=1.2195121951219514cm,
axis lines=middle,
ymajorgrids=true,
xmajorgrids=true,
xmin=0.0,
minor y tick num = 3,
grid = both,
xmax=5.5,
ymin=0.0,
ymax=4.1,
ylabel=$y$,
xtick={1,2,3,4,5},
xticklabels={$2$,$3$,$3$,$4$,$8$},
yticklabels={,,2,4,6,8},]
\clip(0.,0.) rectangle (5.5,4.1);
\fill[line width=0.pt,color=yqqqqq,fill=yqqqqq,fill opacity=0.69] (0.6,0.) -- (0.6,1.) -- (1.4,1.) -- (1.4,0.) -- cycle;
\fill[line width=0.pt,color=yqqqqq,fill=yqqqqq,fill opacity=0.6] (1.6,0.) -- (1.6,1.5) -- (2.4,1.5) -- (2.4,0.) -- cycle;
\fill[line width=0.pt,color=yqqqqq,fill=yqqqqq,fill opacity=0.6] (2.6,0.) -- (2.6,1.5) -- (3.4,1.5) -- (3.4,0.) -- cycle;
\fill[line width=0.pt,color=yqqqqq,fill=yqqqqq,fill opacity=0.6] (3.6,0.) -- (3.6,2.) -- (4.4,2.) -- (4.4,0.) -- cycle;
\fill[line width=0.pt,color=qqzzqq,fill=qqzzqq,fill opacity=1.0] (4.6,0.) -- (4.6,4.) -- (5.4,4.) -- (5.4,0.) -- cycle;
\draw [line width=1.2pt,color=yqqqqq] (1.,0.5)-- (2.,1.);
\draw [line width=1.2pt,color=yqqqqq] (2.,1.)-- (3.,1.6);
\draw [line width=1.2pt,color=yqqqqq] (3.,1.6)-- (4.,2.4);
\draw [line width=1.2pt,color=yqqqqq] (4.,2.4)-- (5.,4.);
\begin{scriptsize}
\draw [fill=qqzzqq] (5.,4.) circle (2.0pt);
\draw [fill=yqqqqq] (4.,2.4) circle (2.0pt);
\draw [fill=yqqqqq] (3.,1.6) circle (2.0pt);
\draw [fill=yqqqqq] (2.,1.) circle (2.0pt);
\draw [fill=yqqqqq] (1.,0.5) circle (2.0pt);
\end{scriptsize}
\end{axis}
\begin{axis}[
  x=1.9090909090909092cm,y=1.2195121951219514cm,
    axis y line*=right,
    axis x line=none,
    ymin=0, ymax=100,
    minor y tick num = 3,
    xmin=0.0,
xmax=5.5,
ymin=0.0,
ymax=4.1,
    yticklabels={,x,20\%,40\%,60\%,80\%,100\%},]
  ]
\end{axis}
\end{tikzpicture}

答案1

pgfplots

  • 对于条形图,我们可以使用ybar宽度为 1.5 厘米、每 2 厘米居中的图表
ybar,
bar shift=0pt,
bar width=1.5cm,x=2cm,

bar shift=0pt,这是为了模拟两个不同颜色且中间没有空格的系列

  • 对于颜色,你可以修改默认使用的颜色
\pgfplotsset{
  /pgfplots/bar cycle list/.style=
    {
      /pgfplots/cycle list=
        {
          {black,fill=green!70!black,opacity=0.5},
          {black,fill=brown,opacity=0.5},
        },
    },
  every axis/.append style={font=\scriptsize,},
}

我们将这些修改放在一个组里面,以便在文档的其余部分中找到默认值(\begingroup ... \endgroup

  • 您还可以将 x 轴的常用信息放入其中every axis/.style,这使得修改更容易。

完整代码

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begingroup
\pgfplotsset{
  /pgfplots/bar cycle list/.style=
    {
      /pgfplots/cycle list=
        {
          {black,fill=green!70!black,opacity=0.5},
          {black,fill=brown,opacity=0.5},
        },
    },
  every axis/.append style={font=\scriptsize,},
  every axis/.style=
    {
      yscale=0.6,
      scale only axis,
      xmin=1,xmax=5,
      x=2cm,%<- Distance between bar centers
      enlarge x limits=0.15,
    },
}

\begin{tikzpicture}
  % let both axes use the same layers
  \pgfplotsset{set layers}
  \begin{axis}[
      axis x line*=bottom,%
      axis y line*=left, % the '*' avoids arrow heads
      ymin=0,ymax=8,
      %
      ybar,
      bar shift=0pt,
      bar width=1.5cm,%x=2cm,
      %
      nodes near coords,
      xtick={1,...,5},
      ytick={0,2,...,8},
      %
      ymajorgrids=true,
      xmajorgrids=true,
      minor y tick num = 3,
      major grid style = {draw = black!50},
      minor grid style = {draw = black!25},
      grid = both,
      % 
      xticklabels={},
      yticklabels={,2,4,...,8},
      %
      xlabel=$x$,
      ylabel=$y$,
      xlabel style={
          at={(1,0)},
          anchor=north west,
        },
      ylabel style={
          at={(0,1)},
          rotate=-90,
          anchor=south west,
        },
    ]
    \addplot coordinates {
        (1,2)
        (2,3)
        (3,3)
        (4,4)
      };
    \addplot coordinates {
        (5,8)
      };
  \end{axis}
  \begin{axis}[
      ymin=0,ymax=100,
      axis y line*=right,
      axis x line=none,
      ylabel=$\%$,
      yticklabels={,,20,40,...,100},
      ylabel style={
          at={(1,1)},
          rotate=-90,
          anchor=south,
        },
    ]
    \addplot [red,thick,mark=*] coordinates {
        (1,10.)
        (2,20.)
        (3,32.)
        (4,47.)
        (5,100.)
      };
  \end{axis}
\end{tikzpicture}
\endgroup
\end{document}

在此处输入图片描述

答案2

对于第二个 y 轴托盘,此部分答案(我的颜色):

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[axis y line*=left,ymin=-0,ymax=8,xmin=0,xmax=5.6]
            \fill[brown,opacity=.69] (.6,0) rectangle (1.4,2);
            \fill[brown,opacity=.6] (1.6,0) rectangle (2.4,3);
            \fill[brown,opacity=.6] (2.6,0) rectangle (3.4,3);
            \fill[brown,opacity=.6] (3.6,0) rectangle (4.4,4);
            \fill[green] (4.6,0) rectangle (5.4,8);
        \end{axis}
        \begin{axis}[hide x axis,axis y line*=right,ymin=0,ymax=1,
            ytick=\empty,
            extra y ticks={0,.2,...,1},
            extra y tick labels={0\%,20\%,40\%,60\%,80\%,100\%},
            %extra ylabel=$\%$
            ]
            \addplot[draw=none] {x};   % dummy plot
        \end{axis}
    \end{tikzpicture}
\end{document}

输出:

在此处输入图片描述

相关内容