摂氏、華氏の換算
華氏はアメリカで使われていますね。
ですが実はアメリカ以外の国では今やほとんど使われていないようです(華氏, wikipedia, 10.27.2017)
華氏の考案者はドイツの物理学者Fahrenheitさんなのですが
ドイツでは華氏ではなく摂氏が使用されているみたいです。
華氏は真水の凝固点を32度(32°F)、沸点を212度(212°F)としていて、摂氏0-100度を180等分しています。
つまり摂氏と華氏の1度の幅は異なっていて、その関係(比)は
100/180 = 5/9
だから、華氏が1度上がると摂氏は5/9度増えて、摂氏が1度上がると華氏は9/5度増えるわけですね。
では摂氏(C)→華氏(F)、華氏(F)→摂氏(C)の換算式を考えてみます。
これでいいでしょうか? 華氏は摂氏0度のとき32度から始まっているので、上の式にその要素を加えてあげないといけませんでした。
換算式が完成しました。
※32度は華氏の値なので二つ目の式では括弧内で先に引く必要があります。
それではpythonで摂氏⇄華氏換算するコードを以下に書いて見ました。
※コメントアウトした部分はお好みで
def convertCtoF(degrees_c): f= 9/5*degrees_c + 32 #f= str(f) + ' °F' return f def convertFtoC(degrees_f): c= 5/9*(degrees_f -32) #c= round(c, 1) #c= str(c) + ' °C' return c
convertCtoF(14.5)
58.1
convertFtoC(80)
26.666666666666668
次に、摂氏と華氏換算グラフを作成してみます。
import numpy as np list_c = np.linspace(0, 100, num=101) list_f = convertCtoF(list_c) list_tmp = np.vstack((list_c, list_f))
import pandas as pd import matplotlib.pyplot as plt %matplotlib inline df_tmp = pd.DataFrame(list_tmp.T) plt.figure(figsize=(4,6)) plt.plot(list_c, list_f) list_idx_c = [20, 40, 60, 80] plt.vlines(list_idx_c, ymin=min(list_f), ymax=[df_tmp.iloc[20, 1], df_tmp.iloc[40, 1], df_tmp.iloc[60, 1], df_tmp.iloc[80, 1]], linestyles='dashed', alpha=0.7) plt.hlines([df_tmp.iloc[20, 1], df_tmp.iloc[40, 1], df_tmp.iloc[60, 1], df_tmp.iloc[80, 1]], xmin=0, xmax=list_idx_c, linestyles='dashed', alpha=0.7) plt.xlim(min(list_c), max(list_c)) plt.ylim(min(list_f), max(list_f)) plt.xlabel('°C', fontsize=15) plt.ylabel('°F', fontsize=15)
早見表的な感じでグラフを作成しましたが、分かりやすいのだろうか。。
以上になります。