我正在尝试使用 Beamer 的叠加层和命令在一帧上显示多个散点图\only
;请参阅以前的 stackchange 问题和答案。我希望使用颜色标度显示第三 (z) 轴。我的问题是 z 值的范围略有不同,因此当我遍历叠加层时,颜色条标度会上下跳动。我意识到我可以通过在 中指定ymin
和ymax
来指定颜色条标度上的最小值和最大值colorbar style
,但值到颜色的映射仍然会发生变化。有没有办法锁定映射,以便每个图对于相同的 z 值具有相同的颜色?
在下面的图片中,请注意黄色带如何从第一张图片中的 8 开始移动到第二张图片中的 10 附近,并且在刻度的底端出现间隙。
\documentclass{beamer}
\usepackage{pgfplots}
\begin{document}
\begin{frame}{An example}
\pgfplotsset{height=8cm,compat=newest}
\vspace{-1em}
\begin{center}
\begin{tikzpicture}
\begin{axis}[xlabel=X axis, x tick label
style={/pgf/number format/1000
sep={}}, % don't put "," in numbers
ylabel=Y axis, colorbar,colormap/bluered,
colorbar style={ylabel={Z axis},ymin=0,ymax=14}]
\only<1>{\addplot[only marks,scatter,scatter src=explicit]
coordinates { (7228.00,100.00) [1.63] (7403.00, 97.50)
[0.18] (7053.00,102.50) [14.00] (7140.50, 98.75) [12.83]
(7490.50,103.75) [0.04] (7315.50, 96.25) [0.54]
(6965.50,101.25) [14.11] (7009.25, 98.13) [14.02]
(7359.25,103.13) [0.27] (7534.25, 95.63) [0.45]
(7184.25,100.63) [4.65] (7096.75, 96.88) [13.66]
(7446.75,101.88) [0.09] (7271.75, 99.38) [0.84]
(6921.75,104.38) [14.16] (6943.63, 99.69) [14.12]
(7293.63,104.69) [0.58] (7468.63, 97.19) [0.25]
(7118.63,102.19) [13.72] (7206.13, 95.94) [3.47]
(7556.13,100.94) [0.16] (7381.13, 98.44) [0.21]
(7031.13,103.44) [14.04] (6987.38, 96.56) [14.02]
(7337.38,101.56) [0.34] (7512.38, 99.06) [0.22]
(7162.38,104.06) [10.02] (7074.88, 97.81) [13.84]
(7424.88,102.81) [0.12] (7249.88, 95.31) [1.59]
(6899.88,100.31) [14.17] (6910.81, 97.66) [14.14]
(7260.81,102.66) [0.86] (7435.81, 95.16) [0.26]
(7085.81,100.16) [13.88] (7173.31, 96.41) [7.70]
(7523.31,101.41) [0.10] (7348.31, 98.91) [0.31]
(6998.31,103.91) [14.08] (7042.06, 95.78) [13.88]
(7392.06,100.78) [0.18] (7567.06, 98.28) [0.34]
(7217.06,103.28) [1.79] (7129.56, 99.53) [13.38]
(7479.56,104.53) [0.08] (7304.56, 97.03) [0.61]
(6954.56,102.03) [14.13] (6932.69, 97.34) [14.11]
(7282.69,102.34) [0.65] (7457.69, 99.84) [0.14]
(7107.69,104.84) [13.79] (7195.19, 98.59) [3.77]
(7545.19,103.59) [0.07] (7370.19, 96.09) [0.24]
(7020.19,101.09) [14.04] (6976.44, 99.22) [14.08]
(7326.44,104.22) [0.39] (7501.44, 96.72) [0.33]
(7151.44,101.72) [12.26] (7063.94, 95.47) [13.79]
(7413.94,100.47) [0.15] (7238.94, 97.97) [1.56]
(6888.94,102.97) [14.19] (6894.41, 98.98) [14.17]
(7244.41,103.98) [1.11] (7419.41, 96.48) [0.20]
(7069.41,101.48) [13.96] (7156.91, 95.23) [10.57]
(7506.91,100.23) [0.15] (7331.91, 97.73) [0.40]
(6981.91,102.73) [14.10] (7025.66, 97.11) [13.97]
(7375.66,102.11) [0.22] (7550.66, 99.61) [0.23]
(7200.66,104.61) [2.73] (7113.16, 98.36) [13.59]
(7463.16,103.36) [0.06] (7288.16, 95.86) [0.83]
(6938.16,100.86) [14.14] (6960.03, 96.17) [14.06]
(7310.03,101.17) [0.48] (7485.03, 98.67) [0.21]
(7135.03,103.67) [13.41] (7222.53, 99.92) [1.82]
(7572.53,104.92) [0.17] (7397.53, 97.42) [0.19]
(7047.53,102.42) [14.01] (7003.78, 98.05) [14.03]
(7353.78,103.05) [0.29] (7528.78, 95.55) [0.45]
(7178.78,100.55) [5.68] (7091.28, 96.80) [13.70]
(7441.28,101.80) [0.10] (7266.28, 99.30) [0.92]
(6916.28,104.30) [14.16] (6905.34, 96.33) [14.13]
(7255.34,101.33) [0.97] (7430.34, 98.83) [0.16]
(7080.34,103.83) [13.94] (7167.84, 97.58) [8.69] };}
\only<2>{\addplot[only marks,scatter,scatter src=explicit]
coordinates { (7228.00,100.00) [14.73] (7403.00, 97.50)
[14.86] (7053.00,102.50) [1.80] (7140.50, 98.75) [4.95]
(7490.50,103.75) [14.89] (7315.50, 96.25) [14.81]
(6965.50,101.25) [1.44] (7009.25, 98.13) [2.00]
(7359.25,103.13) [14.84] (7534.25, 95.63) [14.89]
(7184.25,100.63) [14.52] (7096.75, 96.88) [3.40]
(7446.75,101.88) [14.88] (7271.75, 99.38) [14.78]
(6921.75,104.38) [1.24] (6943.63, 99.69) [1.46]
(7293.63,104.69) [14.80] (7468.63, 97.19) [14.88]
(7118.63,102.19) [2.19] (7206.13, 95.94) [14.43]
(7556.13,100.94) [14.90] (7381.13, 98.44) [14.85]
(7031.13,103.44) [1.69] (6987.38, 96.56) [2.10]
(7337.38,101.56) [14.83] (7512.38, 99.06) [14.89]
(7162.38,104.06) [9.88] (7074.88, 97.81) [2.69]
(7424.88,102.81) [14.87] (7249.88, 95.31) [14.71]
(6899.88,100.31) [1.22] (6910.81, 97.66) [1.52]
(7260.81,102.66) [14.78] (7435.81, 95.16) [14.87]
(7085.81,100.16) [2.28] (7173.31, 96.41) [12.70]
(7523.31,101.41) [14.90] (7348.31, 98.91) [14.83]
(6998.31,103.91) [1.55] (7042.06, 95.78) [2.73]
(7392.06,100.78) [14.86] (7567.06, 98.28) [14.90]
(7217.06,103.28) [14.74] (7129.56, 99.53) [3.51]
(7479.56,104.53) [14.89] (7304.56, 97.03) [14.80]
(6954.56,102.03) [1.36] (6932.69, 97.34) [1.67]
(7282.69,102.34) [14.79] (7457.69, 99.84) [14.88]
(7107.69,104.84) [2.29] (7195.19, 98.59) [14.48]
(7545.19,103.59) [14.90] (7370.19, 96.09) [14.85]
(7020.19,101.09) [1.71] (6976.44, 99.22) [1.67]
(7326.44,104.22) [14.82] (7501.44, 96.72) [14.89]
(7151.44,101.72) [3.55] (7063.94, 95.47) [3.12]
(7413.94,100.47) [14.87] (7238.94, 97.97) [14.72]
(6888.94,102.97) [1.08] (6894.41, 98.98) [1.31]
(7244.41,103.98) [14.76] (7419.41, 96.48) [14.87]
(7069.41,101.48) [1.95] (7156.91, 95.23) [9.66]
(7506.91,100.23) [14.89] (7331.91, 97.73) [14.82]
(6981.91,102.73) [1.47] (7025.66, 97.11) [2.30]
(7375.66,102.11) [14.85] (7550.66, 99.61) [14.90]
(7200.66,104.61) [14.70] (7113.16, 98.36) [3.36]
(7463.16,103.36) [14.88] (7288.16, 95.86) [14.79]
(6938.16,100.86) [1.35] (6960.03, 96.17) [1.98]
(7310.03,101.17) [14.81] (7485.03, 98.67) [14.89]
(7135.03,103.67) [2.30] (7222.53, 99.92) [14.72]
(7572.53,104.92) [14.91] (7397.53, 97.42) [14.86]
(7047.53,102.42) [1.78] (7003.78, 98.05) [1.98]
(7353.78,103.05) [14.84] (7528.78, 95.55) [14.89]
(7178.78,100.55) [14.35] (7091.28, 96.80) [3.29]
(7441.28,101.80) [14.88] (7266.28, 99.30) [14.77]
(6916.28,104.30) [1.21] (6905.34, 96.33) [1.66]
(7255.34,101.33) [14.77] (7430.34, 98.83) [14.87]
(7080.34,103.83) [1.95] (7167.84, 97.58) [11.95] };}
\only<3>{\addplot[only marks,scatter,scatter src=explicit]
coordinates { (7228.00,100.00) [1.63] (7403.00, 97.50)
[0.18] (7490.50,103.75) [0.04] (7315.50, 96.25) [0.54]
(7359.25,103.13) [0.27] (7534.25, 95.63) [0.45] (7096.75,
96.88) [13.66] (7271.75, 99.38) [0.84] (7293.63,104.69)
[0.58] (7468.63, 97.19) [0.25] (7206.13, 95.94) [3.47]
(7556.13,100.94) [0.16] (7381.13, 98.44) [0.21]
(7337.38,101.56) [0.34] (7512.38, 99.06) [0.22]
(7424.88,102.81) [0.12] (7249.88, 95.31) [1.59]
(7260.81,102.66) [0.86] (7435.81, 95.16) [0.26] (7173.31,
96.41) [7.70] (7523.31,101.41) [0.10] (7348.31, 98.91)
[0.31] (7392.06,100.78) [0.18] (7567.06, 98.28) [0.34]
(7217.06,103.28) [1.79] (7479.56,104.53) [0.08] (7304.56,
97.03) [0.61] (7282.69,102.34) [0.65] (7457.69, 99.84)
[0.14] (7195.19, 98.59) [3.77] (7545.19,103.59) [0.07]
(7370.19, 96.09) [0.24] (7326.44,104.22) [0.39] (7501.44,
96.72) [0.33] (7413.94,100.47) [0.15] (7238.94, 97.97)
[1.56] (7244.41,103.98) [1.11] (7419.41, 96.48) [0.20]
(7506.91,100.23) [0.15] (7331.91, 97.73) [0.40]
(7375.66,102.11) [0.22] (7550.66, 99.61) [0.23]
(7200.66,104.61) [2.73] (7463.16,103.36) [0.06] (7288.16,
95.86) [0.83] (7310.03,101.17) [0.48] (7485.03, 98.67)
[0.21] (7222.53, 99.92) [1.82] (7572.53,104.92) [0.17]
(7397.53, 97.42) [0.19] (7353.78,103.05) [0.29] (7528.78,
95.55) [0.45] (7178.78,100.55) [5.68] (7441.28,101.80)
[0.10] (7266.28, 99.30) [0.92] (7255.34,101.33) [0.97]
(7430.34, 98.83) [0.16] (7167.84, 97.58) [8.69]
(7053.00,102.50) [1.80] (7140.50, 98.75) [4.95]
(6965.50,101.25) [1.44] (7009.25, 98.13) [2.00] (7096.75,
96.88) [3.40] (6921.75,104.38) [1.24] (6943.63, 99.69)
[1.46] (7118.63,102.19) [2.19] (7031.13,103.44) [1.69]
(6987.38, 96.56) [2.10] (7162.38,104.06) [9.88] (7074.88,
97.81) [2.69] (6899.88,100.31) [1.22] (6910.81, 97.66)
[1.52] (7085.81,100.16) [2.28] (6998.31,103.91) [1.55]
(7042.06, 95.78) [2.73] (7129.56, 99.53) [3.51]
(6954.56,102.03) [1.36] (6932.69, 97.34) [1.67]
(7107.69,104.84) [2.29] (7020.19,101.09) [1.71] (6976.44,
99.22) [1.67] (7151.44,101.72) [3.55] (7063.94, 95.47)
[3.12] (6888.94,102.97) [1.08] (6894.41, 98.98) [1.31]
(7069.41,101.48) [1.95] (7156.91, 95.23) [9.66]
(6981.91,102.73) [1.47] (7025.66, 97.11) [2.30] (7113.16,
98.36) [3.36] (6938.16,100.86) [1.35] (6960.03, 96.17)
[1.98] (7135.03,103.67) [2.30] (7047.53,102.42) [1.78]
(7003.78, 98.05) [1.98] (7091.28, 96.80) [3.29]
(6916.28,104.30) [1.21] (6905.34, 96.33) [1.66]
(7080.34,103.83) [1.95] };}
\end{axis}
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}
答案1
通常,meta
标度(包含数据点的颜色信息)对于轴上的所有图都是相同的,因此给定值在所有图中都映射到相同的颜色,这正是您想要的。该行为由键控制point meta rel=<axis wide | per plot>
,axis wide
默认为(将其设置为per plot
将确定每个单独命令的标度\addplot
)。
但是,当您使用 时\only
,PGFplots 无法再同时看到所有图,因此point meta rel=axis wide
没有效果 -meta
每个图的范围都会重新缩放。在这种情况下,您必须自己修复比例,使用point meta min=<lower value>, point meta max=<upper value>
选项axis
。您不应该调整 的和ymin
,因为这实际上不会改变颜色到值的映射,而只会影响颜色条的显示方式(这就是为什么您的颜色条在最后两帧中被挤压的原因:范围仅从 1 到 14,但条仍从 0 到 14 绘制)。ymax
colorbar style
meta
下面是一个显示差异的示例。假设我们有三个具有不同 z(或meta
)值的数据点。在第一张幻灯片中,z 值为 0、7 和 14,分别映射到深蓝色、绿色和深红色。在第二张幻灯片中,z 值为 5、7 和 14,因此第一个点现在应该将其颜色更改为几乎绿色。其他点应保持不变。在第三个图中,最后一个数据点的 az 值为 10,因此它应该变得更橙色。
下面是比较两种不同方法的屏幕截图(在左栏设置point meta min
/并在右栏设置)。在左栏中,行为符合预期:颜色条始终延伸到相同的范围,并且点的颜色根据其 z 值而变化。在右栏中,z 值最低的数据点始终被分配深蓝色,而 z 值最高的数据点则被绘制为深红色,无论 z 值的大小如何。point meta max
colorbar style={ymin=..., ymax=...}
\documentclass{beamer}
\usepackage{pgfplots}
\begin{document}
\begin{frame}{An Example}
\pgfplotsset{height=8cm,compat=newest}
\vspace{-1em}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
xlabel=X axis,
x tick label style={/pgf/number format/1000 sep={}}, % don't put "," in numbers
ylabel=Y axis,
nodes near coords*, % For demonstration purposes only.
scatter, scatter src=explicit, only marks, % Same options for all plots, can be given axis wide
colorbar,
colormap/bluered,
colorbar style={ylabel={Z axis}},
point meta min=0, point meta max=15,
%colorbar style={ylabel={Z axis},ymin=0,ymax=14} % Uncomment this line to see why the colorbar style approach doesn't work
]
\only<1>{\addplot coordinates {(1,1) [0] (2,2) [7] (3,3) [14] };}
\only<2>{\addplot coordinates { (1,1) [5] (2,2) [7] (3,3) [14] };}
\only<3>{\addplot coordinates {(1,1) [5] (2,2) [7] (3,3) [10] };}
\end{axis}
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}