堆叠面积图的一些问题 - pgfplot

堆叠面积图的一些问题 - pgfplot

我对堆叠面积图有三个问题。

1:我想让绘图更宽,以适合我的演示文稿。但如果我在 x 方向上缩放,字体(标签)也会缩放。如何避免这种情况?

在此处输入图片描述

在此处输入图片描述

2:我尝试过的所有方法都\sffamily不起作用。可能是我没有找到正确的开关。我该怎么做?

3:我想更改xlabels。绘图应从 2014 年开始,但标签应从 2015 年开始。没有标签的刻度应有 |。我尝试说明我想要什么xlabels

__________________________________________
      |  |   |  |   |  |   |  |   |  |   |
0   2015   2017   2019   2021   2023   2025

这是我迄今为止尝试过的:

\documentclass{standalone}              
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\usepackage[ngerman]{babel}

\pgfplotstableread[col sep=comma,header=true]{
    Year,pid,pack,diff
    2014,4.5,4.5,0
    2015,3.1,4,0.9
    2016,4.5,5.8,1.3
    2017,3.1,4.9,1.8
    2018,2.8,5.2,2.4
    2019,2.3,3.9,1.6
    2020,2,3.9,1.9
    2021,2.1,3.9,1.8
    2022,2.1,5.5,3.4
    2023,0,3.95,3.95
    2024,0,4,4
    2025,0,3.95,3.95
}\table

\pgfplotsset{
    /pgfplots/area cycle list/.style={/pgfplots/cycle list={%
        {red,fill=red,mark=none},
        {blue,fill=blue,mark=none},
    }
},
every axis label = {font=\sffamily},
every tick label/.append style={font=\sffamily},
}


\begin{document}

\begin{tikzpicture}[font=\sffamily]
    \begin{axis}[ 
            every axis label={font=\sffamily},
            legend style = {font=\sffamily},
            label style = {font=\sffamily},
            tick style = {font=\sffamily},
            area style, 
            stack plots=y, 
            enlarge x limits=false, 
            enlarge y limits=upper, 
            x tick label style={/pgf/number format/1000 sep=},      
            ylabel=y, y label style={at={(0.05,1)}},%
            xlabel=t, x label style={at={(1,0.0)}},%
        ]
%\pgftransformxscale{1.5}%scales in xdirection but stretches also the font
        \addplot table [x=Year, y=pid, fill=green] {\table} \closedcycle;
    \end{axis} 
\end{tikzpicture}

\end{document}

答案1

  1. 问题是如何您缩放图表。如果您直接使用widthheight命令缩放轴,字体大小将不会改变。
  2. 这是一个非常常见的问题,并且已得到解答这里例如。简而言之,您只更改了“普通”字体,而没有更改“数学”字体。而且由于数字tick是在数学模式下打印的,因此您只获得了默认的数学字体。
  3. 在这里,您可以简单地使用键“手动”声明标签xtick并提供您想要显示的值。正如您在代码中看到的,您还可以使用“智能”版本。使用键,minor x tick num您可以提供次要刻度数。

\documentclass{standalone}
\usepackage[ngerman]{babel}
\usepackage[eulergreek]{sansmath}
\usepackage{pgfplots}
    \usepgfplotslibrary{dateplot}
    \pgfplotstableread[col sep=comma,header=true]{
        Year,pid,pack,diff
        2014,4.5,4.5,0
        2015,3.1,4,0.9
        2016,4.5,5.8,1.3
        2017,3.1,4.9,1.8
        2018,2.8,5.2,2.4
        2019,2.3,3.9,1.6
        2020,2,3.9,1.9
        2021,2.1,3.9,1.8
        2022,2.1,5.5,3.4
        2023,0,3.95,3.95
        2024,0,4,4
        2025,0,3.95,3.95
    }\table
    \pgfplotsset{
        /pgfplots/area cycle list/.style={
            /pgfplots/cycle list={%
                {red,fill=red,mark=none},
                {blue,fill=blue,mark=none},
            },
        },
    }
\begin{document}
\begin{tikzpicture}[
    font=\sffamily\sansmath,
]
    \begin{axis}[
        area style,
        stack plots=y,
        enlarge x limits=false,
        enlarge y limits=upper,
        x tick label style={/pgf/number format/1000 sep=},
        ylabel=y, y label style={at={(0.05,1)}},%
        xlabel=t, x label style={at={(1,0.0)}},%
        % -------------------------------------------------
        % set `xtick's
        xtick={2015,2017,...,2025},
        % show in between major xticks 1 minor tick
        minor x tick num=1,
        % change width of the plot
        width=10cm,
        % and maintain (original) height
        % (or also change me, if needed)
        height=\axisdefaultheight,
%        % if the plot should fill the whole page, try the following key
%        scale mode=stretch to fill,
    ]
%\pgftransformxscale{1.5}%scales in xdirection but stretches also the font
        \addplot table [x=Year, y=pid, fill=green] {\table} \closedcycle;
    \end{axis}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容