![使用 TikZ/PGFPlots 在色條內設定 ytick 時出現問題,無軸](https://rvso.com/image/305908/%E4%BD%BF%E7%94%A8%20TikZ%2FPGFPlots%20%E5%9C%A8%E8%89%B2%E6%A2%9D%E5%85%A7%E8%A8%AD%E5%AE%9A%20ytick%20%E6%99%82%E5%87%BA%E7%8F%BE%E5%95%8F%E9%A1%8C%EF%BC%8C%E7%84%A1%E8%BB%B8.png)
我想在 TikZ 中做一個色條,旁邊沒有任何軸。這是我迄今為止透過使用這個優秀論壇提供的幫助所得到的。
我的問題:我想要 y 刻度如下:-2e-3、-1,5e-3、-1e-3、...、2e-3。
例如,參見另一個程式的圖2;
$-2e-3$
在 y 刻度中使用類似的東西是行不通的;
錯誤:!包 PGF 數學錯誤:無法將輸入 '$-2^{-3}$' 解析為浮點數,抱歉。無法讀取的部分位於「$-2^{-3}$」附近。
到目前為止,這是我的程式碼。 (使用此程式碼我得到上面顯示的圖片 1。)
\documentclass[tikz,border={12pt,12pt}]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
hide axis,
scale only axis, %
height=0pt, % Grafik auf größe null
width=0pt, % Grafik auf größe null
colorbar sampled, % Diskrete Stufung
colormap={mymap}{[1pt] rgb(0pt)=(0.68235,0,1);
rgb(9pt)=(0,0.1216, 1);
rgb(17pt)=(0, 0.69412, 1);
rgb(26pt)=(0, 1, 0.6863);
rgb(34pt)=(0, 1, 0.098);
rgb(43pt)=(0.557,1,0);
rgb(51pt)=(1, 0.8353, 0);
rgb(60pt)=(1, 0.2275, 0);
rgb(63pt)=(1,0.02745,0)},
colorbar style={
title={$J$ in $\frac{A}{m^2}$}, % Titel über Colorbar
ylabel= {$\cdot e^{-3}$}, % Beschriftung seitlich, text gedreht
samples=9, % Anzahl diskreter Schritte, so viele wie yticks
width=15, % Breite der Colorbar (des farbigen Bereichs)
height=220, % Höhe der Colorbar
ytick={-2, -1.5, -1,..., 2},% yticks Angabe einzeln mit komma getrennt, oder schrittweise mit 1,2,...,5
point meta min=-2, %neu % Beginn Colorbar, beachte yticks min
point meta max=2, %neu % Ende Colorbar, beachte yticks max
yticklabel style={
text width=2.5em, % Abstand yticks zu colorbar
align=right, %
}
}
]
\end{axis}
\end{tikzpicture}
\end{document}
我希望你理解我的問題和我想做什麼。
如果有人可以幫助我將 y-tick 值更改為類似的值2e^{-3}
,我會非常高興。
答案1
實現此目的的一種方法是執行以下操作:
- 使用範圍的實際值
meta
,因此point meta min=-2e-3, point meta max=2e3
- 關閉刻度標籤的整體縮放:
scaled y ticks = false
使用該套件解析和格式化刻度值
siunitx
,這可以非常精細地控制數字格式。加載\usepackage{siunitx}
,然後設定yticklabel={ \num[ scientific-notation = fixed, fixed-exponent = -3, output-exponent-marker = \text{e}, round-integer-to-decimal = true, round-mode = places, round-precision = 1 ]{\tick} }
\documentclass[tikz,border={12pt,12pt}]{standalone}
\usepackage{pgfplots, siunitx}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
hide axis,
scale only axis, %
height=0pt, % Grafik auf größe null
width=0pt, % Grafik auf größe null
colorbar sampled, % Diskrete Stufung
colormap={mymap}{[1pt] rgb(0pt)=(0.68235,0,1);
rgb(9pt)=(0,0.1216, 1);
rgb(17pt)=(0, 0.69412, 1);
rgb(26pt)=(0, 1, 0.6863);
rgb(34pt)=(0, 1, 0.098);
rgb(43pt)=(0.557,1,0);
rgb(51pt)=(1, 0.8353, 0);
rgb(60pt)=(1, 0.2275, 0);
rgb(63pt)=(1,0.02745,0)},
colorbar style={
title={$J$ in $\frac{A}{m^2}$}, % Titel über Colorbar gedreht
samples=9, % Anzahl diskreter Schritte, so viele wie yticks
width=15, % Breite der Colorbar (des farbigen Bereichs)
height=220, % Höhe der Colorbar
%ytick={-2, -1.5, -1,..., 2},% yticks Angabe einzeln mit komma getrennt, oder schrittweise mit 1,2,...,5
point meta min=-2e-3, %neu % Beginn Colorbar, beachte yticks min
point meta max=2e-3, %neu % Ende Colorbar, beachte yticks max
scaled y ticks = false,
yticklabel={
\num[
scientific-notation = fixed,
fixed-exponent = -3,
output-exponent-marker = \text{e},
round-integer-to-decimal = true,
round-mode = places,
round-precision = 1
]{\tick}
},
yticklabel style={
text width=4em, % Abstand yticks zu colorbar
align=right, %
}
}
]
\end{axis}
\end{tikzpicture}
\end{document}
答案2
PSTricks 解決方案。
用 e 表示法:
\documentclass{article}
\usepackage{multido,pstricks}
\definecolor{color1}{rgb}{0.68235,0,1}
\definecolor{color2}{rgb}{0,0.1216,1}
\definecolor{color3}{rgb}{0,0.69412,1}
\definecolor{color4}{rgb}{0,1,0.6863}
\definecolor{color5}{rgb}{0,1,0.098}
\definecolor{color6}{rgb}{0.557,1,0}
\definecolor{color7}{rgb}{1,0.8353,0}
\definecolor{color8}{rgb}{1,0.2275,0}
\definecolor{color9}{rgb}{1,0.02745,0}
\begin{document}
\begin{pspicture}(-0.33,-0.15)(2.3,8.7)
\multido{\i = 1+1}{8}{\psframe[dimen = m, fillstyle = solid, fillcolor = color\i](!0 \i\space 1 sub)(0.5,\i)}
\multido{\iA = 0+2, \iB = -2+1}{5}{\rput[r](1.9,\iA){$\iB\mathrm{e}^{-3}$}}
\multido{\i = 1+2, \r = -1.5+1}{4}{\rput[r](1.9,\i){$\r\mathrm{e}^{-3}$}}
\rput(0.25,8.5){$J$ in $\frac{A}{\textup{m}^{2}}$}
\end{pspicture}
\end{document}
用科學記數法,我認為更好:
\documentclass{article}
\usepackage{multido,pstricks}
\definecolor{color1}{rgb}{0.68235,0,1}
\definecolor{color2}{rgb}{0,0.1216,1}
\definecolor{color3}{rgb}{0,0.69412,1}
\definecolor{color4}{rgb}{0,1,0.6863}
\definecolor{color5}{rgb}{0,1,0.098}
\definecolor{color6}{rgb}{0.557,1,0}
\definecolor{color7}{rgb}{1,0.8353,0}
\definecolor{color8}{rgb}{1,0.2275,0}
\definecolor{color9}{rgb}{1,0.02745,0}
\begin{document}
\begin{pspicture}(-0.33,-0.15)(2.3,8.7)
\multido{\i = 1+1}{8}{\psframe[dimen = m, fillstyle = solid, fillcolor = color\i](!0 \i\space 1 sub)(0.5,\i)}
\multido{\iA = 0+2, \iB = -2+1}{5}{\rput[r](2.3,\iA){$\iB \cdot 10^{-3}$}}
\multido{\i = 1+2, \r = -1.5+1}{4}{\rput[r](2.3,\i){$\r \cdot 10^{-3}$}}
\rput(0.25,8.5){$J$ in $\frac{A}{\textup{m}^{2}}$}
\end{pspicture}
\end{document}