我有以下情节:
\begin{tikzpicture}
\begin{semilogxaxis} [
width=\textwidth,
height=0.4\textwidth,
xlabel={Frequency [Hz]},
ylabel={Volume [dB]},
xmin=10,
xmax=10000,
grid=major,
smooth,
legend entries={Trumpet, Trombone, Horn, Tuba},
]
\addplot [blue] table {recordings/Bb-C4.txt};
\addplot [orange!50!yellow] table {recordings/C-C4.txt};
\addplot [green!70!black] table {recordings/Eb-C4.txt};
\addplot [red] table {recordings/F-C4.txt};
\end{semilogxaxis}
\end{tikzpicture}
这很好,但是虽然在这里描述“频率”很重要,但我还想添加 x 轴刻度和特定频率的标签来表示音符名称。
我感觉我已经接近添加extra x ticks
以下选项了semilogxaxis
:
extra x ticks = {262, 523, 784, 1047},
extra x tick labels={C4, C5, G5, C6 },
% extra x tick style={xticklabel=above}
但我真的很难理解extra x tick style
。这些标签不能干扰正常的 x-tick 标签。答案可能是将它们移动到图的顶部,但将它们移动到轴上方可能是我能做的最好的事情。
使用“两个法令”(如pgfplots 文档) 看起来很接近我想要的,但它只描述了交替的 Y 轴。
以下是简化的 MWE:
\documentclass[a4paper,11pt]{book}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{semilogxaxis} [
width=\textwidth,
height=0.4\textwidth,
xlabel={Frequency [Hz]},
ylabel={Volume [dB]},
xmin=10,
xmax=10000,
grid=major,
smooth,
extra x ticks = {262, 523, 784, 1047},
extra x tick labels={C4, C5, G5, C6 }
% extra x tick style={xticklabel=above}
]
\addplot [blue] coordinates {
(10,-81)
(102,-93)
(204,-87)
(253,-32)
(301,-67)
(355,-77)
(403,-78)
(452,-81)
(500,-74)
(522,-41)
(554,-77)
(602,-83)
(651,-86)
(705,-87)
(753,-88)
(775,-60)
(780,-52)
(785,-56)
(800,-92)
};
\end{semilogxaxis}
\end{tikzpicture}
\end{document}
答案1
像这样?
\documentclass[twocolumn]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
width=\textwidth,
height=0.4\textwidth,
ylabel={Volume [dB]},
xmin=10,
xmax=10000,
grid=major,
legend entries={Trumpet, Trombone, Horn, Tuba},
}
\begin{semilogxaxis} [
axis x line*=bottom, % <---
xlabel={Frequency [Hz]},
]
\addplot coordinates {
(10,-81) (102,-93) (204,-87) (253,-32) (301,-67)
(355,-77) (403,-78) (452,-81) (500,-74) (522,-41)
(554,-77) (602,-83) (651,-86) (705,-87) (753,-88)
(775,-60) (780,-52) (785,-56) (800,-92)
};
\end{semilogxaxis}
\begin{semilogxaxis} [
axis x line*=top, % <---
xtick = {262, 523, 784, 1047},
xticklabels = {C4, C5, G5, C6}
]
\addplot coordinates {
(10,-81) (102,-93) (204,-87) (253,-32) (301,-67)
(355,-77) (403,-78) (452,-81) (500,-74) (522,-41)
(554,-77) (602,-83) (651,-86) (705,-87) (753,-88)
(775,-60) (780,-52) (785,-56) (800,-92)
};
\end{semilogxaxis}
\end{tikzpicture}
\end{document}
编辑(1):
现在向第二个axis
添加了选项axis y line =none,
。这应该允许ytick
仅在第一个axis
环境中写入。
抱歉,但我可以用你的真实数据测试其效果(它们仍然未知),因此为了测试,我对第二个坐标进行了微小的变化axis
。
编辑(2):
添加\pgfplotsset
了axis
OP 在下面的评论中建议的选项。
\documentclass[twocolumn]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
width=\textwidth,
height=0.4\textwidth,
grid=major,
scale only axis, % <--- added
xmin=10, xmax=10000,
ymin=-100, ymax=-20, % <--- added
ylabel={Volume [dB]},
no marks, % <--- added
every axis plot post/.append style={thick},
legend entries={Trumpet, Trombone, Horn, Tuba},
}
\begin{semilogxaxis} [
axis x line*=bottom,
xlabel={Frequency [Hz]},
]
\addplot coordinates {
(10,-81) (102,-93) (204,-87) (253,-32) (301,-67)
(355,-77) (403,-78) (452,-81) (500,-74) (522,-41)
(554,-77) (602,-83) (651,-86) (705,-87) (753,-88)
(775,-60) (780,-52) (785,-56) (800,-92)
};
\end{semilogxaxis}
\begin{semilogxaxis} [
axis x line*=top,
axis y line =none, % <--- added
xtick = {262, 523, 784, 1047},
xticklabels = {C4, C5, G5, C6}
]
\addplot +[red] coordinates { % <--- changed coordinates
(10,-51) (102,-83) (204,-77) (253,-42) (301,-37)
(355,-87) (403,-78) (452,-71) (500,-74) (522,-33)
(554,-55) (602,-88) (651,-86) (705,-87) (753,-78)
(775,-54) (780,-56) (785,-65) (800,-88)
};
\end{semilogxaxis}
\end{tikzpicture}
\end{document}