import matplotlib.pyplot as plt # モジュールをインポートする T = [0.0] * 12 R = [0.0] * 12 # リストを宣言する # データを読みこむ print ("Input file?") ifile = input () # ファイル名を入力する f = open (ifile, "r") # ファイルを開く m = 1 while m <= 12: # 同じ処理を12回繰り返す a, b, c = f.readline().split() # データをファイルから読みこむ T[m-1] = float (b) # 気温と降水量の値を R[m-1] = float (c) # リストに代入する m = m + 1 # カウントをひとつ進める f.close () # ファイルを閉じる # 入力データを書き出す print ("Month Temp. Prec.") m = 1 while m <= 12: # 同じ処理を12回繰り返す print (" %4d %5.1f %6.1f" % (m, T[m-1], R[m-1])) # データを書き出す m = m + 1 # カウントをひとつ進める # グラフを描く print ("Title?") title = input () # タイトルを入力する fig = plt.figure () # 描画領域全体のオブジェクトを # 作成する ax1 = fig.subplots () # 気温を作図するための # オブジェクトを作成する ax2 = ax1.twinx () # 降水量を作図するための # オブジェクトを作成する month = list (range (1, 13)) # 1〜12のリストを定義する plt.title (title) # タイトルを付ける plt.xlim (0.5, 12.5) # x軸の範囲を指定する plt.xticks (month) # x軸の目盛りを指定する ax1.set_ylim (-40.0, 40.0) # y軸(気温)の範囲を指定する ax1.set_ylabel ("Temperature [℃]") # y軸(気温)にラベルを付ける ax1.grid (axis="y") # y軸(気温)に目盛り線を付ける ax1.plot (month, T, color="#FF0000") # 気温の折れ線を描く ax2.set_ylim (0.0, 400.0) # y軸(降水量)の範囲を指定する ax2.set_ylabel ("Precipitation [mm]") # y軸(降水量)にラベルを付ける ax2.bar (month, R, align="center", color="#0000FF") # 降水量の棒を描く plt.show ()