# gitからデータを取得する。 %cd /content/ !rm -rf python_intermediate_2022 !git clone https://github.com/tendo-sms/python_intermediate_2022 %cd /content/python_intermediate_2022/05_GW_pandas import pandas as pd # CSVファイルの読み込み df_data1 = pd.read_csv("sample_1.csv") df_data2 = pd.read_csv("sample_2.csv") # 先頭5行を表示する。 print(df_data1.head(5)) print(df_data2.head(5)) import inspect import figsetting as fs # figsettingの中身を表示 print(inspect.getsource(fs)) import figsetting as fs from matplotlib import pyplot as plt CONFIG = fs.CONFIG plt.rcParams.update(CONFIG) FONT_LABEL = fs.FONT_LABEL FONT_TITLE = fs.FONT_TITLE # 図の設定 fig, ax = plt.subplots(1,1, figsize = fs.FIGSIZE) # 可視化パラメータ X = df_data1["2theta"] Y = df_data1["Intensity"] LABEL_X = "2theta" LABEL_Y = "Intensity" GRAPH_TITLE = "data1" # 可視化 ax.plot(X, Y, color = fs.LINE_COLOR) # X軸ラベル設定 ax.set_xlabel(LABEL_X, **FONT_LABEL) # Y軸ラベル設定 ax.set_ylabel(LABEL_Y, **FONT_LABEL) # 軸の設定 ax.minorticks_on() ax.tick_params(direction="in", which="both", length=5, labelsize=16) ax.grid(which="major", axis="both", color="gray", linestyle="--", linewidth=0.6) ax.set_xlim([25, 60]) ax.set_ylim([0, 4200]) # グラフタイトルの設定 ax.set_title(GRAPH_TITLE, **FONT_TITLE) # 表示 plt.show() # 図の設定 fig, ax = plt.subplots(1,1, figsize = fs.FIGSIZE) # 可視化パラメータ X1 = df_data1["2theta"] Y1 = df_data1["Intensity"] X2 = df_data2["2theta"] Y2 = df_data2["Intensity"] LABEL_X = "2theta" LABEL_Y = "Intensity" GRAPH_TITLE = "data1とdata2" DISTANCE = 500 # 可視化 ax.plot(X, Y, color = fs.LINE_COLOR) ax.plot(X, Y+DISTANCE, color = fs.LINE_COLOR2) # X軸ラベル設定 ax.set_xlabel(LABEL_X, **FONT_LABEL) # Y軸ラベル設定 ax.set_ylabel(LABEL_Y, **FONT_LABEL) # 各線の右側にテキストを挿入 ax.text(60.3, 0*DISTANCE, "data1", **FONT_LABEL) ax.text(60.3, 1*DISTANCE, "data2", **FONT_LABEL) # 軸の設定 ax.minorticks_on() ax.tick_params(direction="in", which="both", length=5, labelsize=16) ax.grid(which="major", axis="both", color="gray", linestyle="--", linewidth=0.6) ax.set_xlim([25, 60]) ax.set_ylim([0, 5200]) # グラフタイトルの設定 ax.set_title(GRAPH_TITLE, **FONT_TITLE) # 表示 plt.show() # 図の設定 fig, ax = plt.subplots(1,1, figsize = fs.FIGSIZE) # 可視化パラメータ X = df_data1["2theta"] Y = df_data1["Intensity"] LABEL_X = "2theta" LABEL_Y = "Intensity" GRAPH_TITLE = "data1" # 可視化 ax.fill_between(X, Y, color="red", alpha=0.5) ax.plot(X, Y, color = fs.LINE_COLOR) # X軸ラベル設定 ax.set_xlabel(LABEL_X, **FONT_LABEL) # Y軸ラベル設定 ax.set_ylabel(LABEL_Y, **FONT_LABEL) # 軸の設定 ax.minorticks_on() ax.tick_params(direction="in", which="both", length=5, labelsize=16) ax.grid(which="major", axis="both", color="gray", linestyle="--", linewidth=0.6) ax.set_xlim([27, 30]) ax.set_ylim([0, 4500]) # グラフタイトルの設定 ax.set_title(GRAPH_TITLE, **FONT_TITLE) # 表示 plt.show() import numpy as np from scipy.stats import norm from scipy.optimize import curve_fit # ガウシアンフィッティング用の関数を準備する def gauss(x, a, mu, sigma): return a*np.exp(-(x-mu)**2/(2*sigma**2)) # 対象範囲を絞り込む mask = (df_data1["2theta"]>=27) & (df_data1["2theta"]<=30) X = df_data1[mask]["2theta"] Y = df_data1[mask]["Intensity"] # ガウシアンフィッティングを実行する param_ini = [4000, 28.4, 0.1] popt, pcov = curve_fit(gauss, X, Y, p0=param_ini) fitting = gauss(X, popt[0],popt[1],popt[2]) # 図の設定 fig, ax = plt.subplots(1,1, figsize = fs.FIGSIZE) # 可視化パラメータ LABEL_X = "2theta" LABEL_Y = "Intensity" GRAPH_TITLE = "data1 and fitting" # 可視化 ax.fill_between(X, Y, color="red", alpha=0.5) ax.plot(X, Y, color = fs.LINE_COLOR, label="data1") ax.plot(X, fitting, color = "blue", linewidth=2, label="fitting") # X軸ラベル設定 ax.set_xlabel(LABEL_X, **FONT_LABEL) # Y軸ラベル設定 ax.set_ylabel(LABEL_Y, **FONT_LABEL) # 軸の設定 ax.minorticks_on() ax.tick_params(direction="in", which="both", length=5, labelsize=16) ax.grid(which="major", axis="both", color="gray", linestyle="--", linewidth=0.6) ax.set_xlim([27, 30]) ax.set_ylim([0, 4500]) # 凡例の設定 ax.legend(fontsize=18) # グラフタイトルの設定 ax.set_title(GRAPH_TITLE, **FONT_TITLE) # 表示 plt.show()