如何使用 pgfplots 在圖中設定和定位圖例?

如何使用 pgfplots 在圖中設定和定位圖例?

我想在這兩張圖中加入一個圖例: 在此輸入影像描述

我有這個代碼:

\begin{figure}[!ht]
\resizebox{\textwidth}{!}{
\begin{tikzpicture}
\begin{axis}[xmin=0, xmax=90,axis y line*=left,ymin=8.62,ymax=8.69, xlabel=
{Tempo [ore]}, ylabel={Pressione [bar]},title={$P_{media}$ VS 
$T_{media}$},legend style={at={(1,1)},anchor=south east}]
\addplot[thick,black] table[x=tempo,y=P_mean]
{capitolo4/grafici/t_meanVSp_mean2.txt};
\addlegendentry={Pressione}
\end{axis}
\begin{axis}[xmin=0, xmax=90,axis y line*=right,ymin=20.5,ymax=23.0, ylabel= 
{Temperatura [°C]}]
\addplot[dashed,black] table[x=tempo,y=T_mean]
{capitolo4/grafici/t_meanVSp_mean2.txt};
\addlegendentry={Temperatura}
\end{axis}  
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[xmin=0, xmax=90,ymin=8.55,ymax=8.75,xlabel={Tempo [ore]}, 
ylabel={Pressione [bar]},title={$P_{sperimentale}$ VS $P_{ideale}$ },legend 
style={at={(1,1)},anchor=north east}]
\addplot[only marks,mark=o] table[x=tempo,y=p_sp]
{capitolo4/grafici/t_meanVSp_mean3.txt};
\addplot[thick,black] table[x=tempo,y=p_id]
{capitolo4/grafici/t_meanVSp_mean3.txt};
\legend={Psperimentale,Pideale}
\end{axis}
\end{tikzpicture}
}

但當我編譯 pdf 時,它會返回: 在此輸入影像描述

所以它不會在圖中寫下標記的名稱,並且圖例的位置不在應有的位置(我設定為東南,它在圖的東北打印)。

答案1

你真的應該在你的問題中加入一個可編譯的 MWE。我剛剛刪除了所有依賴外部包的東西,我有這個:

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

\begin{tikzpicture}
\begin{axis}[xmin=0, xmax=90,axis y line*=left,ymin=8.62,ymax=8.69, xlabel=
{Tempo [ore]}, ylabel={Pressione [bar]},title={$P_{media}$ VS 
$T_{media}$},legend pos = north east]
\addplot[thick,black]  {8.63};
\legend{Pressione}
\end{axis}
\begin{axis}[xmin=0, xmax=90,axis y line*=right,ymin=20.5,ymax=23.0, ylabel= 
{Temperatura [°C]} ,legend style={at={(1,.8)},anchor=north east} ]
\addplot[dashed,black]  {x};
\legend{Temperatura}
\end{axis}  
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[xmin=0, xmax=90,ymin=8.55,ymax=8.75,xlabel={Tempo [ore]}, 
ylabel={Pressione [bar]},title={$P_{sperimentale}$ VS $P_{ideale}$ },legend 
style={at={(1,1)},anchor=north east}]
\addplot[only marks,mark=o] {x};
\addplot[thick,black]  {x};
\legend{Psperimentale,Pideale}
\end{axis}
\end{tikzpicture}
\end{document}

編譯為:

在此輸入影像描述

主要問題在於奇怪的使用\addlegendentry=- 我認為你混淆了命令和按鍵。

您可以在手冊第 4.9.4 節(圖例)及後續第 234 頁中找到有關圖例的信息1.13版本

相關內容