.png)
で気候図 (気候図) を作成したいのですpgfplots
が、これらのグラフには軸のスケーリングに関する特別な規則 (約 50 年前に Walter と Lieth によって定義) があり、pgfplots
2 番目の y 軸のスケーリングは の間で変更でき、両方の y 軸のスケーリングは互いに結合されているため、 でこれを実現する方法がまったくわかりません。以下に例を示します。
ルール: 左の y 軸 (温度 T) は、零度以下の温度が存在しない場合は零度から 10°C ずつ増減します。零度以下の温度が存在する場合は、零度以下 10°C ずつ増減します。これはまだ簡単な部分です (私にはできます)。次は難しい部分です: 右の y 軸 (降雨量) は、300mm までは T の値の 2 倍で増減します。ただし、300mm を超える降雨がある場合は、100mm から 200mm ずつ増減します。右の軸は零度を下回ることはありません。
私の質問は、入力値に応じて軸のスケーリングを動的に変更し、軸を互いに結合するために使用する必要があるコマンドは何ですか? {x,y}{min,max}
、tick
および友人だけでは確かに十分ではありません。 pgfplots を使用したプロットの基本は知っていますが、このような動的なスケーリング アプローチをどこから開始すればよいのか、またはこれをどれだけ自分で計算する必要があるのか (独自のコードを使用)、どれだけがpgfplots
私のために実行できるのかがまったくわかりません。 たとえば、明らかに最大値を見つけるために何らかの方法で入力データを解析する必要がありますが、 でそれを実行する方法がわかりませんpgfplots
。
作業に役立てるために、最初の例のデータを以下に示します。
#bombay.txt
#M T/°C N/mm
1 23.9 3
2 23.9 3
3 26.1 3
4 28.1 2
5 29.7 18
6 28.9 485
7 27.2 617
8 27.0 340
9 27.0 264
10 28.1 64
11 27.2 13
12 25.6 3
コード的にはこうなりましたが、今はどう進めていけばいいのかわかりません (色、線のスタイル、グリッドなどは今のところ重要ではありません。自分でできますが、軸のスケーリング/結合だけが問題です)。
\documentclass{standalone}
\usepackage{pgfplots} \pgfplotsset{compat=newest}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}
\def\filename{bombay.txt}
\def\monthnames{{"J","F","Mar","A","May","Jun","Jul","A","S","O","N","D"}}
\begin{axis}[
ylabel={Temperature in \si{\celsius}},
tick pos=left,
xticklabel={\pgfmathparse{\monthnames[Mod(\tick-1,12)]}\pgfmathresult},
xmin=1, xmax=12,
ymin=0, ytick={0,10,20,30,40,50}]
\addplot+[red, mark=none] table [x index=0, y index=1] {\filename};
\end{axis}
\begin{axis}[
ylabel={Rain in \si{\milli\metre}},
axis y line*=right,
hide x axis,
xmin=1, xmax=12, ymin=0]
\addplot+[blue, mark=none] table [x index=0, y index=2] {\filename};
\end{axis}
\end{tikzpicture}
\end{document}
例1: 雨軸の変化のスケーリング(> 300mmのため)、T > 0のみ
例 2: 雨量軸は常に 300mm 未満なので、スケーリングは変更されません。T < 0。
答え1
必要なのは、スタイル設定で可能になります。
- 雨軸の非線形変換を設定します
- 両方のy軸に適した(結合された)軸限界を計算する
- 両方のy軸に適した(結合された)単位を計算する
- 両方の軸の開始/終了位置を決定するには、手動入力が必要です。使用例から私が理解しているところによると、この入力は最低気温と最大雨量で構成されます (他の制限は常に暗黙的に制限されます)。
私は次のような高度なプロトタイプに到達しました。
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
% The following \pgfplotsset macro defines a *manual* wheather
% visualization system which couples temperature and rain axis.
\pgfplotsset{
rain trafo/.style={
y coord trafo/.code={%
\pgfmathparse{##1 > 100 ? 100 + 0.1*(##1 - 100) : ##1}%
},%
y coord inv trafo/.code={%
\pgfmathparse{##1 > 100 ? 100 + 10*(##1 - 100) : ##1}%
},%
%
% configure the allowed tick positions for the rain
% (UNTRANSFORMED!):
ytick={0,20,...,100,300,500,...,1000},
},
%
/wheather/temp min/.initial=,
/wheather/rain max/.initial=,
%
% USAGE: \pgfkeys{/wheather/set up={temp min=<min temperature>, rain max=<max rain axis value>}
% It will set up two styles "temp axis" and "rain axis". These
% styles will contain suitable axis limits and unit scales.
/wheather/set up/.code={%
\pgfkeysalso{/wheather/.cd,#1}%
%
% these values are supposed to be given:
\pgfkeysgetvalue{/wheather/rain max}\rainMax
\pgfkeysgetvalue{/wheather/temp min}\temperatureMin
%
% make sure they are given!
\ifx\rainMax\empty
\PackageError{wheather}{Please provide 'rain max=MAX RAIN VALUE IN AXIS'}{}%
\fi
\ifx\temperatureMin\empty
\PackageError{wheather}{Please provide 'temp min=MIN TEMPERATURE VALUE IN AXIS'}{}%
\fi
%
% now, compute the MISSING values. These are rain min and
% temperature max....
\begingroup
% do this in a group such that 'rain trafo' is only applied
% within the group...
%
% store it - just in case 'y coord trafo' is empty.
\let\pgfmathresult=\rainMax
%
\pgfplotsset{rain trafo,y coord trafo={\rainMax}}%
\global\let\rainMaxTransformed=\pgfmathresult
\pgfmathparse{\pgfmathresult /2}%
\global\let\temperatureMax=\pgfmathresult
\endgroup
%
\pgfmathparse{2*(\pgfkeysvalueof{/wheather/temp min})}%
\let\rainMin=\pgfmathresult
%
% now we want to compute a suitable axis scale for both axes.
% To this end, we scale them such that they fit into the value
% of \axisdefaultheight :
\pgfmathparse{\axisdefaultheight/(\temperatureMax-\temperatureMin)}%
\let\temperatureUnit=\pgfmathresult
%
\pgfmathparse{\axisdefaultheight/(\rainMaxTransformed-\rainMin)}%
\let\rainUnit=\pgfmathresult
%
% OK, compute the result:
\pgfplotsset{%
set layers,
%
% this is to be used for the temperature axis:
temp axis/.style={
y=\temperatureUnit,
ymin=\temperatureMin,
ymax=\temperatureMax,
%
% configure the allowed tick positions for a temperature:
ytick={-40,-30,-20,...,140},
% ... but only show the *label* for "small"
% temperatures:
yticklabel={%
\ifdim\tick pt<45pt
\pgfmathprintnumber\tick
\fi
},
ylabel={Temperature},
%tick pos=left,% seems to fail for 'axis x line=middle'!?
%
% configure the x axis:
axis x line*=middle,
xmin=1, xmax=12,
xtick={1,2,...,12},
xticklabels={J,F,M,A,M,J,J,A,S,O,N,D},
x tick label as interval,
grid=major,
},
%
% ... and this is to be used for the rain axis:
rain axis/.style={
y=\rainUnit,
ymin=\rainMin,
ymax=\rainMax,
% this range will be overwritten by 'rain trafo' if it
% is active:
ytick={0,20,...,400},
rain trafo,
yticklabel style={/pgf/number format/precision=0},
ylabel={Rain},
axis y line*=right,
hide x axis,
xmin=1, xmax=12,
},
}%
},
}
\begin{filecontents}{bombay.txt}
#bombay.txt
#M T/°C N/mm
1 23.9 3
2 23.9 3
3 26.1 3
4 28.1 2
5 29.7 18
6 28.9 485
7 27.2 617
8 27.0 340
9 27.0 264
10 28.1 64
11 27.2 13
12 25.6 3
\end{filecontents}
\begin{filecontents}{moskow.txt}
#M T/°C N/mm
1 -10.9 30
2 -12.9 23
3 -6.1 30
4 3.1 40
5 6.7 50
6 15.9 60
7 20.2 61
8 22.0 50
9 17.0 45
10 10.1 20
11 3.2 22
12 -9.6 25
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{/wheather/set up={temp min=0, rain max=800}}
\def\filename{bombay.txt}
\begin{axis}[
temp axis,
]
\addplot+[red, mark=none] table [x index=0, y index=1] {\filename};
\end{axis}
\begin{axis}[
rain axis,
]
\addplot+[blue, mark=none] table [x index=0, y index=2] {\filename};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\pgfplotsset{
*clear* the nonlinear rain trafo. We do not want it here:
rain trafo/.style=,
/wheather/set up={temp min=-20, rain max=160},
}
\def\filename{moskow.txt}
\begin{axis}[
temp axis,
]
\addplot+[red, mark=none] table [x index=0, y index=1] {\filename};
\end{axis}
\begin{axis}[
rain axis,
]
\addplot+[blue, mark=none] table [x index=0, y index=2] {\filename};
\end{axis}
\end{tikzpicture}
\end{document}
まだいくつかの項目(主に書式設定と塗りつぶしパス)が残っていますが、すでにかなり優れていると思います。
また、pgfplots に実際に「欠けている機能」である未解決の問題が 1 つあります。 と書き込むとすぐにaxis x line=middle
、pgfplots は最初と最後の目盛りラベルを表示しません。このアプリケーションにとっては非常に残念なことです。これを pgfplots の todo リストに入れます。おそらく、axis x line=middle
これを修正するには元に戻す必要があります。間隔として x 目盛りを表示するということは、n 目盛りの位置に対して n-1 個の目盛りラベルがあることを意味することに注意してください。つまり、1 月のデータ ポイントを何らかの方法で複製する必要がある可能性があります。