我怎样才能将 tikz 中的图形线与旁边的表格连接起来,如下图所示?
\documentclass[12pt,a4paper]{article}
\usepackage[english,greek]{babel}
\usepackage{ucs}
\usepackage[utf8x]{inputenc}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{tkz-tab}
\usetikzlibrary{shapes,snakes}
\usepackage{smartdiagram}
\usepackage{geometry}
\usetikzlibrary{positioning}
\geometry{left=0.75in,right=0.75in,top=0.65in,bottom=0.65in}
\usepackage{tkz-euclide}
\usetikzlibrary{shapes.geometric}
\usepackage{tkz-fct} \usetkzobj{all}
\usetikzlibrary{calc,decorations.pathreplacing}
\usetikzlibrary{decorations.markings}
\usepackage{pgfplots}
\usepackage{verbatim}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usepackage{filecontents}
\usetikzlibrary{decorations.text}
\begin{document}
\begin{center}
\begin{tabular}{|c|c|}
\hline
\textbf{$P$} & \textbf{$Q_A$} \\
\hline
10 & 5\\
\hline
8 & 8 \\
\hline
6 & 12 \\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
xlabel=$Q$ ,
ylabel=$P$ ]
\addplot table [y=P, x=$Q_A$]{data_1.txt};
\addlegendentry{$Q_A$}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
答案1
这确实需要一些手动调整,因此对于较长的表格来说,这会相当繁琐。不过对于这种情况来说,情况还不算太糟。
\documentclass[12pt,a4paper]{article}
\usepackage{pgfplots,pgfplotstable,booktabs}
\pgfplotsset{compat=1.9}
\usetikzlibrary{positioning,calc}
\pgfplotstableread{%
Q P
5 10
8 8
12 6
}\mydata
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
name=myaxis,
xmin=4.5,xmax=12.5,
ymin=5.5,ymax=10.5,
axis y line=left,
axis x line=bottom,
xlabel=$Q$ ,
ylabel=$P$ ]
\addplot table \mydata;
\addlegendentry{$Q_A$}
\coordinate (c1) at (axis cs:5,10);
\coordinate (c2) at (axis cs:8,8);
\coordinate (c3) at (axis cs:12,6);
\end{axis}
\node (tablenode) [right=2cm of myaxis] {\pgfplotstabletypeset[every head row/.style={before row=\toprule,after row=\midrule},
every last row/.style={after row=\bottomrule}]\mydata};
\foreach \C/\Y in {c1/0.4, c2/0.6, c3/0.8}
\draw [thick,dashed] (\C) -- ($(tablenode.north west)!\Y!(tablenode.south west)$);
\end{tikzpicture}
\end{center}
\end{document}