
我想知道是否有一種方法可以讓線圖“Unseamed”和“Requirement”跨越圖表的整個寬度,而不僅僅是從第一個和最後一個 x 軸標籤的中心點開始。有沒有辦法創建不可見的 x 軸標籤來實現此目的?
\documentclass[a4paper,10pt]{report}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[width=11cm,
ybar,
enlargelimits=0.3,legend style={at={(0.98,0.98)},
cells={anchor=west}
},
legend entries={Straight,Unseamed,Zigzag,Requirement},
bar width=1cm,legend columns=2,
ylabel={Tensile strength{,} $N\,mm^{-1}$},
symbolic x coords={Plain,Single lap,Double lap},
xtick=data,
ytick={0,2,4,6,8,10,12,14},
x tick label style={rotate=45,anchor=east},
]
\addplot [draw=black,pattern=crosshatch dots,error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt}]
coordinates
{
(Plain,7)+-(0.41,0.41)
(Single lap,11)+-(0.27,0.27)
(Double lap,12)+-(0.47,0.47)
};
\addplot[black,sharp plot]
coordinates {(Plain,10.857) (Double lap,10.857)}
;
\addplot [draw=black,error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt}]
coordinates
{
(Plain,4)+-(0.31,0.31)
(Single lap,5)+-(0.27,0.27)
(Double lap,6)+-(0.38,0.38)
};
\addplot[black,sharp plot,dashed]
coordinates {(Plain,3.430) (Double lap,3.430)}
;
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
這是實現此目的的一種迂迴方式。首先在座標系的下角處加上s \coordinate
,然後以這些座標為參考畫線。圖例條目新增有.(Plain,3.43)
(Plain, 10.857)
rel axis cs
\addlegendimage
請注意,單位通常不應以斜體書寫,因此建議進行更改。您可以這樣做$\mathrm{N}\,\mathrm{mm}^{-1}$
,但處理單元的更好方法是包siunitx
,它可以讓您編寫\si{\newton\per\square\milli\metre}
.另外,我可以提到units
的庫pgfplots
,它的功能可以與siunitx
下面的程式碼(取自手冊pgfplots
)結合使用。
\documentclass[border=2mm,tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{units}
\pgfplotsset{unit code/.code 2 args={\si{#1#2}}}
\usetikzlibrary{patterns}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=11cm,
ybar,
y unit=\newton\per\square\milli\metre,
enlargelimits=0.3,
legend style={
at={(0.98,0.98)},
cells={anchor=west},
column 3/.style={width=2cm}
},
legend entries={Straight,Unseamed,Zigzag,Requirement},
bar width=1cm,legend columns=2,
ylabel={Tensile strength},
symbolic x coords={Plain,Single lap,Double lap},
xtick=data,
ytick={0,2,4,6,8,10,12,14},
x tick label style={rotate=45,anchor=east},
]
\addplot [draw=black,pattern=crosshatch dots,error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt}]
coordinates
{
(Plain,7)+-(0.41,0.41)
(Single lap,11)+-(0.27,0.27)
(Double lap,12)+-(0.47,0.47)
};
\addlegendimage{line legend,black,sharp plot}
\addplot [draw=black,error bars/.cd,y dir=both,y explicit,error bar style={line width=1pt}]
coordinates
{
(Plain,4)+-(0.31,0.31)
(Single lap,5)+-(0.27,0.27)
(Double lap,6)+-(0.38,0.38)
};
\addlegendimage{line legend,black,sharp plot,dashed}
\coordinate (A) at (axis cs:Plain,3.430);
\coordinate (B) at (axis cs:Plain,10.857);
\coordinate (O1) at (rel axis cs:0,0);
\coordinate (O2) at (rel axis cs:1,0);
\draw [black,sharp plot,dashed] (A -| O1) -- (A -| O2);
\draw [black,sharp plot] (B -| O1) -- (B -| O2);
\end{axis}
\end{tikzpicture}
\end{document}