%如何使用顶部标签与下方标签不同?
\documentclass[12pt]{article}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{siunitx} % SI-units
\usepackage{pgfplots}
\usepgfplotslibrary{units} % to add units easily to axis
\usepgfplotslibrary{fillbetween} % to fill inbetween curves
\usepgfplotslibrary{colormaps} % to create colormaps
\pgfplotsset{width=22cm, height=9cm}
\pgfplotsset{compat=newest} %(making it only compatalbe with
%new releases of pgfplots)
\usepackage{tikz}
\pgfdeclarehorizontalshading{visiblelight}{50bp}{
color(0.00000000000000bp)=(violet);
color(8.33333333333333bp)=(blue);
color(16.66666666666670bp)=(cyan);
color(25.00000000000000bp)=(green);
color(33.33333333333330bp)=(yellow);
color(41.66666666666670bp)=(orange);
color(50.00000000000000bp)=(red)
}%
\begin{document}
\addtolength{\textwidth}{4cm}
\addtolength{\textheight}{6cm}
\addtolength{\hoffset}{-4.5cm}
\addtolength{\voffset}{-1cm}
\begin{tikzpicture}
%\pgfplotsset{width=18 cm}
\begin{axis}[
xlabel={Comprimento de onda},
xticklabel style = {font=\small,yshift=0.2ex},
xmin=10^-13,
xmax=10^3,
x unit=\si{\meter},
xmode=log,
ymin=0,
ymax=1,
height=3cm,
yticklabels={},
ytick=\empty,
legend cell align=left,
legend style={at={(0.85,-0.77)},anchor=north}
]
\addplot[draw=none, name path=start, forget plot] coordinates{(10^-12,0)(10^-12,1)};
\addplot[draw=none, name path=gamma, forget plot] coordinates{(10^-9,0)(10^-9,1)};
\addplot[draw=none, name path=xrays, forget plot] coordinates{(10^-2,0)(10^-2,1)};
\addplot[draw=none, name path=uv, forget plot] coordinates{(4*10^-7,0)(4*10^-7,1)};
\addplot[draw=none, name path=visible, forget plot] coordinates{(7.5*10^-7,0)(7.5*10^-7,1)};
\addplot[draw=none, name path=ir, forget plot] coordinates{(10^2.5,0)(10^2.5,1)};
\addplot[draw=none, name path=microwave, forget plot] coordinates{(10^5,0)(10^5,1)};
\addplot[draw=none, name path=radiowave, forget plot] coordinates{(10^9,0)(10^9,1)};
\node[fill=none]
at (10^-12,0.7) {
\begin{footnotesize}
$raios$
\end{footnotesize}};
\node[fill=none]
at (*10^-12,0.4) {\begin{small}
$gama$
\end{small}};
\node[fill=none]
at (2*10^-10,0.55) {\begin{footnotesize} $\text{raios x}$ \end{footnotesize}};
\node[fill=none]
at (0.3*10^-7,0.55) {\begin{footnotesize} $ultravioleta$ \end{footnotesize}};
\addplot[shading=visiblelight, area legend] fill between[of=uv and visible];
\node[fill=none]
at (10^-5,0.55) {\begin{footnotesize} $infravermelho$ \end{footnotesize}};
\node[fill=none]
at (10^-2,0.55) {\begin{footnotesize} $\text{micro-ondas}$ \end{footnotesize}};
\node[fill=none]
at (10^2,0.55) {\begin{footnotesize} $\text{radio}$ \end{footnotesize}};
\end{axis}
\begin{axis}[
axis x line=top,
xticklabel = {},
xmin=10^-24,
xmax=10^-2,
xmode=log,
ymin=0,
ymax=1,
height=3cm,
yticklabels={},
ytick=\empty,
x unit=\si{Hz},
% x axis style=red!75!black
]
\end{axis}
\end{tikzpicture}
\end{document}[![enter image description here][1]][1]
答案1
通过你的尝试,你已经走上了正确的道路,你几乎已经成功了。你需要xtick pos
为每个axis
环境添加正确的值,然后你就成功了。有关详细信息(以及更多改进),请查看代码中的注释。
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{siunitx}
\usepackage{pgfplots}
\usepgfplotslibrary{
fillbetween,
units,
}
\pgfplotsset{
% use this `compat' level or higher
compat=1.11,
%
% moved common axis options here
my axis style/.style={
width=22cm,
height=3cm,
ymin=0,
ymax=1,
xticklabel style={
font=\small,
},
ytick=\empty,
axis on top,
},
}
\pgfdeclarehorizontalshading{visiblelight}{50bp}{
color(0.00000000000000bp)=(violet);
color(8.33333333333333bp)=(blue);
color(16.66666666666670bp)=(cyan);
color(25.00000000000000bp)=(green);
color(33.33333333333330bp)=(yellow);
color(41.66666666666670bp)=(orange);
color(50.00000000000000bp)=(red)
}
\begin{document}
\begin{tikzpicture}
% because `xmode=log' does not work in styles I changed `axis' to `semilogxaxis'
\begin{semilogxaxis}[
my axis style,
%
xlabel={Comprimento de onda},
xmin=1e-13,
xmax=1e3,
x unit=\si{\meter},
ytick=\empty,
xtick pos=lower, % <-- this plots ticks on the lower border only
]
\addplot [draw=none,name path=start] coordinates {(1e-12,0)(1e-12,1)};
\addplot [draw=none,name path=gamma] coordinates {(1e-9,0)(1e-9,1)};
\addplot [draw=none,name path=xrays] coordinates {(1e-2,0)(1e-2,1)};
\addplot [draw=none,name path=uv] coordinates {(4e-7,0)(4e-7,1)};
\addplot [draw=none,name path=visible] coordinates {(7.5e-7,0)(7.5e-7,1)};
\addplot [draw=none,name path=ir] coordinates {(10^2.5,0)(10^2.5,1)};
\addplot [draw=none,name path=microwave] coordinates {(1e5,0)(10^5,1)};
\addplot [draw=none,name path=radiowave] coordinates {(1e9,0)(1e9,1)};
\addplot [
shading=visiblelight,
area legend,
] fill between [
of=uv and visible,
];
% added scope to simplify node options
\begin{scope}[
font=\footnotesize,
]
% (use `\textit' instead of math mode)
\node at (1e-12,0.7) {\textit{raios}};
\node [font=\small] at (1e-12,0.4) {\textit{gama}};
\node at (2e-10,0.55) {raios x};
\node at (0.3e-7,0.55) {\textit{ultravioleta}};
\node at (1e-5,0.55) {\textit{infravermelho}};
\node at (1e-2,0.55) {micro-ondas};
\node at (1e2,0.55) {radio};
\end{scope}
\end{semilogxaxis}
\begin{semilogxaxis}[
my axis style,
%
axis x line*=top, % <-- (use starred form to avoid arrow on axis line)
xmin=1e-24,
xmax=1e-2,
x unit=\si{Hz},
xtick pos=upper, % <-- this plots ticks on the upper border only
]
\end{semilogxaxis}
\end{tikzpicture}
\end{document}