TikZ/PGFPlots を使用してカラーバー内の定義された位置に ytick を設定する (軸なし)

TikZ/PGFPlots を使用してカラーバー内の定義された位置に ytick を設定する (軸なし)

昨日、私は最初の質問を投稿しました(TikZ/PGFPlots を使用して軸なしでカラーバー内に ytick を設定する際の問題) と問い合わせたところ、本当に素晴らしい回答をいただきました。これで、期待通りに動作するようになりました。しかし、まだ小さな問題が残っており、どうやら私はこの問題を解決するにはあまりにも愚かだったようです。登録 (今は動作します!) とフォーラムのステータスに問題があり、最後の投稿にコメントできません。

さて、私の問題は、軸なしで TikZ でカラーバーを作成したいということです。これは、この優れたフォーラムが提供するヘルプを使用して、これまでに得たものです。

カラーバーの実際の状態

しかし、図 2 に示すように、バーの最小値 7.50e6、次に 8.75e6、1e7、... とバーの最大値 (1.75e7) まで、定義された位置に yticks を設定したいと考えています。図 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 より大きい) を処理する必要がある場合、科学的形式で数値を表示しようとすると、 が機能しなくなります。その場合は、 ほど強力ではありませんが、かなりのカスタマイズが可能なsiunitxPGF 数値パーサーにフォールバックできます。siunitx

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

関連情報