如何標記 X 軸?

如何標記 X 軸?

問題:我正在嘗試繪製兩張圖,其中我想在第一張圖中標記 x 軸上的完美正相關,在第二張圖中標記完美負相關。

微量元素:

\documentclass{article}
\usepackage[a4paper,top=0.6in,bottom=0.3in,left=0.5in,right=0.5in,headheight=14.5pt]{geometry}
\usepackage{blindtext}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage[utf8]{inputenc}
\usepackage[misc]{ifsym}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{forest}
\usepackage{tikz}
\usepackage{parskip}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}[domain = 0:5]
\draw[<->] (-1,0) -- (5.5,0) node[right] {$x$};
\draw[<->] (0,-1) -- (0,5.5) node[right] {$y$};
\draw[color = blue, thick] (1,1) -- (2,2) -- (3,3) -- (4,4) -- (5,5);
\fill (1,1)  circle[radius=2pt];
\fill (2,2)  circle[radius=2pt];
\fill (3,3)  circle[radius=2pt];
\fill (4,4)  circle[radius=2pt];
\fill (5,5)  circle[radius=2pt];
\end{tikzpicture}\qquad\qquad\qquad\qquad
\begin{tikzpicture}[domain = 0:5]
\draw[<->] (-1,0) -- (5.5,0) node[right] {$x$};
\draw[<->] (0,-1) -- (0,5.5) node[right] {$y$};
\draw[color = blue, thick] (1,5) -- (2,4) -- (3,3) -- (4,2) -- (5,1);
\fill (1,5)  circle[radius=2pt];
\fill (2,4)  circle[radius=2pt];
\fill (3,3)  circle[radius=2pt];
\fill (4,2)  circle[radius=2pt];
\fill (5,1)  circle[radius=2pt];
\end{tikzpicture}
\end{document}

答案1

只需在繪製 x 軸時添加第二個節點即可。pos=0.6表示它放置在路徑的中間點之後(0 是起點,1 是終點)。

\draw[<->] (-1,0) -- (5.5,0) node[right] {$x$} node[pos=0.6,below=3mm] {Perfect positive correlation};

如果您有興趣,可以使用 使程式碼變得更短, \draw plot如下例所示:

\documentclass{article}
\usepackage[a4paper,top=0.6in,bottom=0.3in,left=0.5in,right=0.5in,headheight=14.5pt]{geometry}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
   % domain and samples applies to the plot commands below
   domain = 1:5,samples=5
   ]

\draw[<->] (-1,0) -- (5.5,0) node[right] {$x$} node[pos=0.6,below=3mm] {Perfect positive correlation};
\draw[<->] (0,-1) -- (0,5.5) node[right] {$y$};
\draw[color = blue, thick, mark=*]  plot(\x,\x);

\begin{scope}[xshift=8cm]
\draw[<->] (-1,0) -- (5.5,0) node[right] {$x$} node[pos=0.6,below=3mm] {Perfect negative correlation};
\draw[<->] (0,-1) -- (0,5.5) node[right] {$y$};
\draw[color = blue, thick, mark=*]  plot(\x,-\x+6);
\end{scope}
\end{tikzpicture}
\end{document}

相關內容