! git clone https://github.com/tendo-sms/python_intermediate_2022 ! ls import matplotlib.pyplot as plt # Figureオブジェクトの作成 fig = plt.figure() # Axesオブジェクトの作成 ax = fig.subplots() # グラフの作成 ax.plot([1, 2, 3, 4, 5, 6, 7], [0, 0.1, 0.25, 0.7, 1, 0.8, 0.2]) # グラフの表示 fig.show() # 終了時はcloseを実行するようにしましょう。環境によってはメモリを圧迫することがあります。 # closeを実行してしまうと表示が消えてしまうので、今回はコメントアウトしています。 # plt.close(fig) import matplotlib.pyplot as plt # グラフの作成 plt.plot([1, 2, 3, 4, 5, 6, 7], [0, 0.1, 0.25, 0.7, 1, 0.8, 0.2]) # グラフの表示 plt.show() # plt.close() import matplotlib.pyplot as plt fig = plt.figure() ax = fig.subplots() # X軸データ data_x = [1, 2, 3, 4, 5, 6, 7] # Y軸データ data_y = [0, 0.1, 0.25, 0.7, 1, 0.8, 0.2] # グラフの作成 ax.plot(data_x, data_y, linestyle="--", linewidth=2.0, color="b", marker="s", label="legend1") ax.legend(labels=["legend1"], loc="upper left", fontsize=12) # 凡例表示 fig.show() # plt.close(fig) import matplotlib.pyplot as plt fig = plt.figure() ax = fig.subplots() data_x = [1, 2, 3, 4, 5, 6, 7] data_y = [0, 0.1, 0.25, 0.7, 1, 0.8, 0.2] # 表示範囲の指定 ax.set_xlim(3, 6) # いくつ以上、いくつ以下の範囲をプロットするか ax.plot(data_x, data_y, linestyle="--", linewidth=2.0, color="b", marker="s") fig.show() # plt.close(fig) import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax = fig.subplots() # X軸のデータ data_x = np.arange(0.1, 20.0, 0.1) # Y軸のデータ(指数関数) data_y = np.exp(data_x) # 対数軸に設定 ax.set_yscale("log") ax.plot(data_x, data_y) fig.show() # plt.close(fig) import numpy as np import matplotlib.pyplot as plt from matplotlib import ticker fig = plt.figure() ax = fig.subplots() data_x = np.arange(0.1, 20.0, 0.1) data_y = np.exp(data_x) # メジャーロケータ(ラベル付きのメモリ)を4刻みで設定 ax.xaxis.set_major_locator(ticker.MultipleLocator(4)) # マイナーロケータ(ラベル無しの細かいメモリ)を2刻みで設定 ax.xaxis.set_minor_locator(ticker.MultipleLocator(2)) # Y軸をログスケールに設定 ax.set_yscale("log") # 縦軸の表記を10^3ではなく1.0e+3という指数表記に統一 ax.yaxis.set_major_formatter(ticker.StrMethodFormatter("{x:.1e}")) ax.plot(data_x, data_y) fig.show() # plt.close(fig) import datetime import matplotlib.pyplot as plt from matplotlib.dates import DateFormatter fig = plt.figure() ax = fig.subplots() # X軸データをdatetime形式で用意 data_x = [ datetime.datetime(2022, 12, 1, 14, 00), datetime.datetime(2022, 12, 1, 15, 00), datetime.datetime(2022, 12, 1, 16, 00), datetime.datetime(2022, 12, 1, 17, 00) ] data_y = [0, 0.5, 1, 0.2] # メモリの表示位置を設定 ax.set_xticks(data_x) # メモリの表記を設定(datetimeのstrftimeの形式で設定する) ax.xaxis.set_major_formatter(DateFormatter("%Y/%m/%d\n%H:%M")) ax.plot(data_x, data_y) fig.show() # plt.close(fig) import matplotlib.pyplot as plt fig = plt.figure() ax = fig.subplots() data_x = [1, 2, 3, 4, 5, 6, 7] data_y = [0, 0.1, 0.25, 0.7, 1, 0.8, 0.2] # タイトルを設定 ax.set_title("タイトル") # X軸のラベル設定 ax.set_xlabel("X軸のラベル") # Y軸のラベル設定 ax.set_ylabel("Y軸のラベル") # グリッド線を表示 ax.grid(True) ax.plot(data_x, data_y) fig.show() # plt.close(fig) from pathlib import Path import matplotlib.pyplot as plt from matplotlib import font_manager # フォントファイルをMatplotlibに認識させる fp = font_manager.FontProperties(fname=Path.cwd().joinpath("python_intermediate_2022", "04_matplotlib", "NotoSansJP-Medium.otf")) fig = plt.figure() ax = fig.subplots() # フォントの設定 hfont = {"fontproperties": fp, "size": 15} ax.set_title("グラフのタイトル", **hfont) # X軸のラベル設定 ax.set_xlabel("X軸のラベル", **hfont) # Y軸のラベル設定 ax.set_ylabel("Y軸のラベル", **hfont) # グリッド線を表示 ax.grid() ax.plot([1, 2, 3, 4], [0, 0.5, 1, 0.2]) fig.show() # plt.close(fig) import matplotlib.font_manager as fm font_list = [f.name for f in fm.fontManager.ttflist] font_list import matplotlib.pyplot as plt fig = plt.figure() ax = fig.subplots() # X軸データ x = [1, 2, 3, 4, 5] # グラフの高さデータ height = [100, 200, 300, 400, 500] # 棒グラフの作成 ax.bar(x, height, width=1.0, linewidth=2.0, edgecolor="k", facecolor="c", align="edge") fig.show() # plt.close(fig) import matplotlib.pyplot as plt fig = plt.figure() ax = fig.subplots() x = [1, 2, 3, 4, 5] height = [100, 200, 300, 400, 500] # X軸をログスケールに設定 ax.set_xscale("log") # Y軸をログスケールに設定 ax.set_yscale("log") ax.bar(x, height, width=1.0, linewidth=2.0, edgecolor="k", facecolor="c", align="edge") fig.show() # plt.close(fig) import matplotlib.pyplot as plt fig = plt.figure() ax = fig.subplots() # ヒートマップの2次元Arrayの作成 data = [ [0.1, 0.2, 0.3, 0.4, 0.5], [1, 1.3, 1.6, 2, 2.3], [2.5, 2.7, 10, 2.7, 2.5], [2.3, 2, 1.6, 1.3, 1], [0.5, 0.4, 0.3, 0.2, 0.1] ] # ヒートマップの作成 im = ax.imshow(data) # カラーバーの作成 fig.colorbar(im) # 目盛りの削除 ax.set_axis_off() fig.show() # plt.close(fig) import matplotlib.pyplot as plt from matplotlib.colors import LogNorm fig = plt.figure() ax = fig.subplots() data = [ [0.1, 0.2, 0.3, 0.4, 0.5], [1, 1.3, 1.6, 2, 2.3], [2.5, 2.7, 10, 2.7, 2.5], [2.3, 2, 1.6, 1.3, 1], [0.5, 0.4, 0.3, 0.2, 0.1] ] # 規格化方法を指定して、ヒートマップを作成 im = ax.imshow(data, norm=LogNorm()) fig.colorbar(im) ax.set_axis_off() fig.show() # plt.close(fig) import matplotlib.pyplot as plt fig = plt.figure() ax = fig.subplots() data = [ [0.1, 0.2, 0.3, 0.4, 0.5], [1, 1.3, 1.6, 2, 2.3], [2.5, 2.7, 10, 2.7, 2.5], [2.3, 2, 1.6, 1.3, 1], [0.5, 0.4, 0.3, 0.2, 0.1] ] # vmin、vmaxでカラーバーの最小値、最大値を指定 im = ax.imshow(data, vmin = 0, vmax = 3) fig.colorbar(im, extend="max") # extend="max"で最大値を超える場合があることを表現 ax.set_axis_off() fig.show() # plt.close(fig) import matplotlib.pyplot as plt from matplotlib.pyplot import cm fig = plt.figure() ax = fig.subplots() data = [ [0.1, 0.2, 0.3, 0.4, 0.5], [1, 1.3, 1.6, 2, 2.3], [2.5, 2.7, 10, 2.7, 2.5], [2.3, 2, 1.6, 1.3, 1], [0.5, 0.4, 0.3, 0.2, 0.1] ] im = ax.imshow(data, cmap=cm.jet) # カラーマップをjetに設定 fig.colorbar(im) ax.set_axis_off() fig.show() # plt.close(fig) import matplotlib.pyplot as plt fig = plt.figure() ax = fig.subplots() data = [ [0.1, 0.2, 0.3, 0.4, 0.5], [1, 1.3, 1.6, 2, 2.3], [2.5, 2.7, 10, 2.7, 2.5], [2.3, 2, 1.6, 1.3, 1], [0.5, 0.4, 0.3, 0.2, 0.1] ] im = ax.imshow(data, vmin = 0, vmax = 3) fig.colorbar(im) # 余白あり fig.savefig("loose.png", dpi=100, format="png") # 余白を削除 fig.savefig("tight.png", dpi=100, format="png", bbox_inches="tight") # plt.close(fig) import datetime import matplotlib.pyplot as plt from matplotlib.dates import DateFormatter data_x = [ datetime.datetime(2022, 12, 1, 14, 00), datetime.datetime(2022, 12, 1, 15, 00), datetime.datetime(2022, 12, 1, 16, 00), datetime.datetime(2022, 12, 1, 17, 00) ] oresen_data_y = [1, 1.5, 2, 2.2] bou_data_y = [0.2, 0.5, 1, 0.8] ####### ここから回答例 ####### fp = font_manager.FontProperties(fname=Path.cwd().joinpath("python_intermediate_2022", "04_matplotlib", "NotoSansJP-Medium.otf")) fig = plt.figure() ax = fig.subplots() ax.set_xticks(data_x) ax.set_xticklabels(ax.get_xticklabels(), rotation=45) hfont = {"fontproperties": fp, "size": 15} ax.set_title("演習問題", **hfont) ax.set_xlabel("X軸のラベル", **hfont) ax.set_ylabel("Y軸のラベル", **hfont) ax.xaxis.set_major_formatter(DateFormatter("%Y/%m/%d %H:%M")) ax.plot(data_x, oresen_data_y, linestyle="-.", linewidth=2.0, color="b", marker="D", label="折れ線") ax.bar(data_x, bou_data_y, width=datetime.timedelta(hours=1)*0.8, linewidth=2.0, edgecolor="k", facecolor="c", label="棒") ax.legend(prop=fp) ax.grid() fig.show() fig.savefig("practice.jpg", dpi=300, format="jpg") # fig.close()