我试图将网格放入我的图形中,但出现了下面的问题。网格样式必须是辅助线和虚线,我知道该怎么做,但问题仍然存在,无法正确放置网格。
这是我正在使用的代码。
\documentclass[a4paper,10pt]{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\usepackage{lmodern}
\pgfplotsset{compat=1.3}
\usepgfplotslibrary{groupplots}
\begin{filecontents*}{dataCL.csv}
x, y
10, 0.7
15, 0.6
20, 0.5
\end{filecontents*}
\begin{filecontents*}{dataCD.csv}
x, y
10, 0.4
15, 0.3
20, 0.2
\end{filecontents*}
\begin{document}
\pgfkeys{
/pgf/number format/.cd,
set decimal separator={,{\!}},
set thousands separator={}
}
\pgfplotsset{
every axis/.append style = {
line width = 0.5pt,
tick style = {line width=1pt},
grid style={dashed}
}
}
\begin{tikzpicture}
% provide shared options here with pgfplotsset:
\pgfplotsset{
height=6cm, width=9cm,
no markers=major
}
% this is the leftmost y axis (y2)
\begin{axis}[
xmin=0,xmax=1,%--- CF
xshift=0cm,%-- CF
width=2cm,
hide x axis,
axis y line*=left,
ymin=0, ymax=1.2,
ytick = {0,0.24,0.48,...,1.2},
ylabel={$C_l$}
]
\end{axis}
\begin{axis}[
xmin=0, xmax=10,
xshift=7cm,%-- CF
width=2cm,
hide x axis,
axis y line*=right,
ymin=0, ymax=0.5,
ytick = {0,0.1,0.2,...,0.5},
ylabel={$C_D$}
]
\end{axis}
\begin{axis}[
height=2cm,
xmin=0, xmax=25,
ymin=0, ymax=1.2,
minor ytick = {0,5,10,...,25},
axis x line*=bottom,
hide y axis,
xlabel={$\alpha$}
]
\end{axis}
\begin{axis}[
xmin = 0, xmax=25,
ymin = 0, ymax=1.2,
hide x axis,
hide y axis,
]
\addplot+ [mark = none] table [col sep=comma, x=x, y=y]{dataCL.csv};
\end{axis}
\begin{axis}[
xmin = 0, xmax=25,
ymin = 0, ymax=0.5,
hide x axis,
hide y axis,
]
\addplot+ [mark = none] table [col sep=comma, x=x, y=y]{dataCD.csv};
\end{axis}
\begin{axis}[grid]
\end{axis}
\end{tikzpicture}
\end{document}
答案1
我找到了一种方法来做到这一点。
\documentclass[a4paper,10pt]{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\usepackage{lmodern}
\pgfplotsset{compat=1.3}
\usepgfplotslibrary{groupplots}
\begin{filecontents*}{dataCL.csv}
x, y
10, 0.7
15, 0.6
20, 0.5
\end{filecontents*}
\begin{filecontents*}{dataCD.csv}
x, y
10, 0.4
15, 0.3
20, 0.2
\end{filecontents*}
\begin{document}
\pgfkeys{
/pgf/number format/.cd,
set decimal separator={,{\!}},
set thousands separator={}
}
\pgfplotsset{
every axis/.append style = {
line width = 0.5pt,
tick style = {line width=1pt},
grid style={dashed}
}
}
\begin{tikzpicture}
% provide shared options here with pgfplotsset:
\pgfplotsset{
height=6cm, width=9cm,
no markers=major
}
% this is the leftmost y axis (y2)
\begin{axis}[ymajorgrids,
xmin=0,xmax=1,%--- CF
% xshift=0cm,%-- CF
%width=2cm,
hide x axis,
axis y line*=left,
ymin=0, ymax=1.2,
ytick = {0,0.24,0.48,...,1.2},
ylabel={$C_l$}
]
\end{axis}
\begin{axis}[
xmin=0, xmax=10,
xshift=7cm,%-- CF
width=2cm,
hide x axis,
axis y line*=right,
ymin=0, ymax=0.5,
ytick = {0,0.1,0.2,...,0.5},
ylabel={$C_D$}
]
\end{axis}
\begin{axis}[xmajorgrids,
height=6cm,
xmin=0, xmax=25,
ymin=0, ymax=1.2,
minor ytick = {0,5,10,...,25},
axis x line*=bottom,
hide y axis,
xlabel={$\alpha$}
]
\end{axis}
\begin{axis}[
xmin = 0, xmax=25,
ymin = 0, ymax=1.2,
hide x axis,
hide y axis,
]
\addplot+ [mark = none] table [col sep=comma, x=x, y=y]{dataCL.csv};
\end{axis}
\begin{axis}[
xmin = 0, xmax=25,
ymin = 0, ymax=0.5,
hide x axis,
hide y axis,
]
\addplot+ [mark = none] table [col sep=comma, x=x, y=y]{dataCD.csv};
\end{axis}
\end{tikzpicture}
\end{document}