使用 TikZ/PGFPlots 在色條內的定義位置設定 ytick,無軸

使用 TikZ/PGFPlots 在色條內的定義位置設定 ytick,無軸

昨天我發布了我的第一個問題(使用 TikZ/PGFPlots 在色條內設定 ytick 時出現問題,無軸)我得到了非常棒的答案。現在它按照我想要的方式工作了。但我還有一個小問題,顯然我太笨了,無法解決這個問題。由於註冊(現在可以使用了!)和我的論壇狀態出現一些問題,我無法評論我的上一篇文章。

現在,我的問題是:我想在 TikZ 中做一個色條,旁邊沒有任何軸。這是我到目前為止透過使用這個超級論壇提供的幫助所得到的。

色條的實際狀態

但我想在定義的位置設定 yticks,如圖 2 所示,在長條圖的最小值 7.50e6 處,然後是 8.75e6、1e7,...,直到長條圖的最大值 (1.75e7)。圖 2 顯示了我想要的 yticks 的解決方案。

在此輸入影像描述

我該如何自訂 yticks?

到目前為止,這是我的程式碼:

\documentclass[12pt,a4paper,oneside]{report}
\usepackage{tikz}
\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}{\si{\square\m}}$},     % Titel über Colorbar gedreht si unit m^2
        %title={$J$ in $\frac{A}{m^{2}}$},              % Titel über Colorbar gedreht
        samples=75,                  % Anzahl diskreter Schritte, so viele wie yticks
        width=15,                   % Breite der Colorbar (des farbigen Bereichs)
        height=220,                 % Höhe der Colorbar
        point meta min=7500000, %neu     % Beginn Colorbar, beachte yticks min
        point meta max=17500000, %neu      % Ende Colorbar, beachte yticks max
        scaled y ticks = false,
        yticklabel={
            \num[
                %scientific-notation = fixed,
                scientific-notation = true,
                %fixed-exponent = 5,
                exponent-product=\cdot,
                %output-exponent-marker = \text{e},
                round-integer-to-decimal = true,
                round-mode = figures,
                round-precision = 3,
            ]{\tick}
        },
        yticklabel style={
            text width=4em,       % Abstand yticks zu colorbar
            align=right,            %          
        }
    }
]
\end{axis}
\end{tikzpicture}

\end{document}

我希望你可以幫助我。預先非常感謝!

答案1

就我個人而言,我不認為這些是非常有用的刻度位置,但您可以透過以下方式獲得它們。

最簡單的方法是「暴力法」:設定ytick = {7.5e6, 8.75e6, 1e7, 1.125e7, 1.25e7, 1.375e7, 1.5e7, 1.625e7, 1.75e7}為手動指定所有刻度位置。

但因為這不是很靈活,您可能想使用foreach的語法ytick = {7.5e6, 8.75e6, ..., 1.75e7}。但是,這會失敗並出現dimension too large錯誤,因為該機制使用基本的 TeX 算術,無法處理如此大的數字。所以我們必須使用一些中間步驟:

  • 與我之前告訴您的相反,以縮放單位指定軸限制:

    point meta min=7.5,
    point meta max=17.5
    
  • 這允許您使用以下語法指定刻度位置foreach

    ytick = {7.5, 8.75, ..., 17.5}
    
  • 在刻度標籤內,您可以按比例縮小刻度值:

    yticklabel={
    \pgfkeys{/pgf/fpu=true, /pgf/fpu/output format=fixed} % Necessary for handling large values
    \pgfmathparse{\tick*1e6} % Scale the tick values
        \num[
            scientific-notation = true,
            exponent-product=\cdot,
            round-integer-to-decimal = true,
            round-mode = places,
            round-precision = 3,
        ]{\pgfmathresult}
    

\documentclass[12pt,a4paper,oneside]{report}
\usepackage{tikz}
\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}{\si{\square\m}}$},     % Titel über Colorbar gedreht si unit m^2
        %title={$J$ in $\frac{A}{m^{2}}$},              % Titel über Colorbar gedreht
        samples=75,                  % Anzahl diskreter Schritte, so viele wie yticks
        width=15,                   % Breite der Colorbar (des farbigen Bereichs)
        height=220,                 % Höhe der Colorbar
        point meta min=7.5, %neu     % Beginn Colorbar, beachte yticks min
        point meta max=17.5, %neu      % Ende Colorbar, beachte yticks max
        scaled y ticks = false,
       ytick = {7.5, 8.75, ..., 17.5},
        yticklabel={
    \pgfkeys{/pgf/fpu=true, /pgf/fpu/output format=fixed}
    \pgfmathparse{\tick*1e6}
            \num[
                %scientific-notation = fixed,
                scientific-notation = true,
                %fixed-exponent = 5,
                exponent-product=\cdot,
                %output-exponent-marker = \text{e},
                round-integer-to-decimal = true,
                round-mode = places,
                round-precision = 3,
            ]{\pgfmathresult}
        },
        yticklabel style={
            text width=4em,       % Abstand yticks zu colorbar
            align=right,            %          
        }
    }
]
\end{axis}
\end{tikzpicture}

\end{document}

如果您需要處理龐大的數字(大於 1E10),siunitx當您嘗試使用科學格式顯示數字時會感到窒息。在這種情況下,您可以退回到 PGF 數位解析器,它雖然不如 那樣強大siunitx,但也允許相當多的自訂:

\pgfmathparse{\tick*1e6}
\pgfmathprintnumber{\pgfmathresult}

相關內容