pfgplots 刻度标签对齐

pfgplots 刻度标签对齐

我正在尝试使用 pgfplots 生成经济图表。刻度标签的默认设置是居中。有什么方法可以让刻度标签左对齐或右对齐吗?我的目标是让 160 左对齐,让 200 右对齐,这样我就可以在中间放另一个标签。还有什么方法可以减小刻度标签的文本大小吗?

我当前的源输出

\begin{center}
\begin{tikzpicture}
\begin{axis}[
scale = 1.2,
axis lines = middle,
xmin = 0, xmax = 650,
ymin = 0, ymax = 1300,
axis lines* = left,
xtick = {0,160,200,480,600}, ytick = {800,1200},
clip = false,
]

\node [right] at (current axis.right of origin) {X};
\node [above] at (current axis.above origin) {Y};

\addplot[domain = 0:600, samples = 1000, color= red]{-2*x+1200};

\addplot[domain = 50:350, restrict y to domain = 0:1150, samples = 1000, color= blue]{sqrt(128000000/(x))};

\filldraw[black] (200,800) circle (2pt) node[]{};
\draw[black, dashed] (200,0) -- (200,800);
\draw[black, dashed] (0,800) -- (200,800);

\addplot[domain = 0:480, samples = 1000, color= orange]{-2.5*x+1200};

\addplot[domain = 10:310, restrict y to domain = 0:1150, samples = 1000, color= green]{((8388608000000000000/(x^2))^(1/5))};

\filldraw[black] (160,800) circle (2pt) node[]{};
\draw[black, dashed] (160,0) -- (160,800);
\draw[black, dashed] (0,800) -- (160,800);

\end{axis}
\end{tikzpicture}
\end{center}

答案1

像这样(与@KersouMan 评论中提到的类似,但稍微精致一些)?

在此处输入图片描述

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

\begin{document}
    \begin{tikzpicture}[
dot/.style = {circle, fill, inner sep=2pt, node contents={}},
lbl/.style = {inner xsep=0pt, below #1} 
                        ]
\begin{axis}[
scale = 1.2,
axis lines = middle,
xlabel=X,   xlabel style={anchor=west},
ylabel=Y,   ylabel style={anchor=south},
xmin = 0, xmax = 650,
ymin = 0, ymax = 1300, restrict y to domain = 0:1150,
xtick = {0,480,600}, ytick = {800,1200},
extra x ticks = {160,200}, extra x tick label=\empty,
clip=false,
            ]

\addplot[domain = 50:350, samples = 100, color= blue]   {sqrt(128000000/(x))};
\addplot[domain = 10:310, samples = 100, color= green]  {((8388608000000000000/(x^2))^(1/5))};

\draw[red] (0,1200) -- (600,0);
\draw[red] (0,1200) -- (480,0);

\draw[dashed] (160,0) node[lbl= left] {160} |- (0,800) node[pos=0.5, dot];
\draw[dashed] (200,0) node[lbl=right] {200} |- (0,800) node[pos=0.5, dot];

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

编辑: 在 160 和 200 之间的刻度,您可以插入extra x ticks,例如extra x ticks = {180},,其标签旋转 90 度。考虑前面提到的 MWE 是:

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

\begin{document}
    \begin{tikzpicture}[
dot/.style = {circle, fill=black, inner sep=2pt, node contents={}},
lbl/.style = {font=\footnotesize, inner xsep=0pt, 
              text=black, yshift=-0.5mm, below #1}
                        ]
\begin{axis}[
scale = 1.2,
axis lines = middle,
xlabel=X,   xlabel style={anchor=west},
ylabel=Y,   ylabel style={anchor=south},
xmin = 0, xmax = 650,
ymin = 0, ymax = 1300, restrict y to domain = 0:1150,
xtick = {0,160,200,480,600}, xticklabels= {0, , ,480,600},
ytick = {800,1200},
xticklabel style = {font=\footnotesize},
extra x ticks = {180}, 
extra x tick style={tick label style={rotate=90, anchor=east}},
every axis plot post/.append style={very thick},
clip=false,
            ]
\addplot[domain = 50:350, samples = 100, color= blue]   {sqrt(128000000/(x))};
\addplot[domain = 10:310, samples = 100, color= green]  {(8388608000000000000/(x^2))^(1/5)};

\draw[red] (0,1200) -- (600,0);
\draw[red] (0,1200) -- (480,0);

\draw[densely dashed, very thin, gray]   
    (160,0) node[lbl= left] {160} |- (0,800) node[pos=0.5, dot]
    (200,0) node[lbl=right] {200} |- (0,800) node[pos=0.5, dot];
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容