如何自动将 tikz 框与其内容绑定?

如何自动将 tikz 框与其内容绑定?

如何让 tikz 自动将图形限制在标记点周围?可能吗?

谢谢!!

\documentclass[convert]{standalone}
\usepackage{tikz,pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[grid=both,
  axis y line=left,axis x line=bottom,
  xlabel={airplanes},
  ylabel={steamships},
  title={Seattle's economy}]
    \node[label={45:{$A$}},circle,fill,inner sep=2pt] at (axis cs:0,1) {};
    \node[label={45:{$B$}},circle,fill,inner sep=2pt] at (axis cs:2,2) {};
    \node[label={45:{$C$}},circle,fill,inner sep=2pt] at (axis cs:4,0) {};
\end{axis}
\end{tikzpicture}

\end{document}

mwe 的形象

答案1

pgfplots计算轴限值时仅考虑其自己的绘图命令(我认为)。您已使用 TikZ 命令放置点,因此pgfplots不关心它们。(pgfplots不是 TikZ,它是一个使用 TikZ 的单独包。)

但您可以使用该nodes near coords功能pgfplots将自定义文本放置在散点图中的数据点旁边。请参阅下面的示例。

在此处输入图片描述

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
  grid=both,
  axis y line=left,
  axis x line=bottom,
  xlabel={airplanes},
  ylabel={steamships},
  title={Seattle's economy},
  enlargelimits=0.1 % <-- extends axis limits to give some space around points
  ]
  
  \addplot [
     only marks,
     mark=*,
     nodes near coords=$\pgfplotspointmeta$, % places the label text in math mode
     nodes near coords align=above right,
     point meta=explicit symbolic % means that meta values are given in the data stream, and are not numerical
     ]
     table[
        meta=m % specify the column for the point labels (the nodes near coords)
        ] {
    x y m    
    0 1 A
    2 2 B
    4 0 C
    };
\end{axis}
\end{tikzpicture}

\end{document}

答案2

另一种选择是tkz-base

\documentclass[border=5mm]{standalone}
\usepackage{tkz-base}

\begin{document}
% Show point coordinates for the point B
\tikzset{arrow coord style/.style={dashed,-}}
\tikzset{xcoord style/.style={below=0pt}}
%%

\begin{tikzpicture}[xscale=1.2]
    \tkzInit[xmax=1,ymax=1,xstep=0.2,ystep=0.2]
    \tkzGrid(0,0)(1,1)
    \tkzText
        %[
        % draw,
        % line width = 1pt,%
        % color = red,%
        % fill = orange!20
        %]
        (0.5,1.2){Seattle's economy}
    %\tkzAxeXY
    \tkzDrawX[label = airplanes,midway, below = 20pt]
    \tkzLabelX
    %
    \tkzDrawY[label = steamships,midway, above=30pt,sloped]
    \tkzLabelY
    %
    \tkzDefPoints{0/1/A,0.5/0.5/B,1/0/C}
    \tkzDrawPoints[shape=cross,size=6,thick,red](A,B,C)
    \tkzLabelPoints[above right](A,B,C)
    %Show point coordinates for the point B
    \tkzPointShowCoord(B)
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容