{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"# [グループワーク] グラフの装飾・出力 + フィッティング\n",
"Matplotlibを使って様々な装飾の課題にチャレンジしてみましょう。\n",
"\n",
"時間が余ったらフィッティングについてもチャレンジしてみましょう。\n",
"\n",
"# 事前準備\n",
"本講義で使用するファイルをダウンロードしておきましょう。次のコードを実行してください。"
],
"metadata": {
"id": "3PL8Qk_4trNI"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "V81YWjQetpki"
},
"outputs": [],
"source": [
"# gitからデータを取得する。\n",
"%cd /content/\n",
"!rm -rf python_intermediate_2022\n",
"!git clone https://github.com/tendo-sms/python_intermediate_2022\n",
"%cd /content/python_intermediate_2022/05_GW_pandas"
]
},
{
"cell_type": "markdown",
"source": [
"# グループワークその2\n",
"## (Matplotlibを使ったグラフの装飾・出力を行う課題にチャレンジ)\n",
"\n",
"これまでに学んできた知識を参考に、とあるスペクトルデータをExcelファイルからデータを読み出し、データの図化に取り組んでみましょう。\n",
"\n",
"ここではグラフの装飾関連に着目した課題に取り組んでいただきます。\n",
"\n",
"- [pandasでExcelファイルを読み込んでみよう](#pandasでExcelファイルを読み込んでみよう)\n",
"- [折れ線グラフ](#データを数値型にして欠測値の削除/補完をしてみよう)\n",
"- [棒グラフ](#データを数値型にして欠測値の削除/補完をしてみよう)\n",
"- [ヒートマップ](#結果を可視化してみよう)\n",
"- [フィッティング](#結果を可視化してみよう)\n"
],
"metadata": {
"id": "qsbrZehOJCM5"
}
},
{
"cell_type": "markdown",
"source": [
"\n",
"## pandasでCSVファイルを読み込んで準備しよう\n",
"\n",
"**[目標1]**\n",
"\n",
"2つのCSVファイルを読み込みましょう。"
],
"metadata": {
"id": "NlhJAnImMa4n"
}
},
{
"cell_type": "markdown",
"source": [
"### CSVデータを読み込もう!\n",
"\n",
"**[課題1-1]**\n",
"\n",
"CSVファイルを読み込んで、以下の変数に格納しましょう。\n",
"\n",
"**df_data1**に「sample_1.csv」\n",
"\n",
"**df_data2**に「sample_2.csv」\n",
"\n",
"読み込みができたら、次のコマンドで確認してください。\n",
"\n",
"`print(df_data1.head(5))`\n",
"\n",
"`print(df_data2.head(5))`\n",
"\n",
"ヒント:read_csv"
],
"metadata": {
"id": "qWJsJENbM6xu"
}
},
{
"cell_type": "code",
"source": [
"import pandas as pd\n",
"\n",
"# CSVファイルの読み込み\n",
"df_data1 = pd.read_csv(\"sample_1.csv\")\n",
"df_data2 = pd.read_csv(\"sample_2.csv\")\n",
"\n",
"# 先頭5行を表示する。\n",
"print(df_data1.head(5))\n",
"print(df_data2.head(5))"
],
"metadata": {
"id": "DWZcspPPDhOh"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"\n",
"## ARIM製の自作モジュールのfigsettingインポートしてみよう\n",
"\n",
"**[目標2]**\n",
"\n",
"**figsetting**をインポートして、中身を見てみましょう。\n"
],
"metadata": {
"id": "-8NcfnY-Pxfu"
}
},
{
"cell_type": "markdown",
"source": [
"### figsettingをインポートしよう!\n",
"\n",
"**[学習2-1]**\n",
"\n",
"ARIM製の自作モジュールのfigsettingをインポートして中身をみてみよう。\n",
"\n",
"\n",
"ヒント:inspect"
],
"metadata": {
"id": "m6J2ysbuS3cv"
}
},
{
"cell_type": "code",
"source": [
"import inspect\n",
"import figsetting as fs\n",
"\n",
"# figsettingの中身を表示\n",
"print(inspect.getsource(fs))"
],
"metadata": {
"id": "w4imU7UrQWZ5"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"\n",
"## 折れ線グラフを描いてみよう\n",
"\n",
"**[目標3]**\n",
"\n",
"matplotlibで折れ線グラフを描いてみましょう。"
],
"metadata": {
"id": "dRmV9hjDQ7H-"
}
},
{
"cell_type": "markdown",
"source": [
"### 折れ線グラフを描いてみよう!\n",
"\n",
"**[課題3-1]**\n",
"\n",
"折れ線グラフを描くときのパラメータはfigsettingの定義を使ってみましょう。\n",
"\n",
"**df_data1**のデータを使ってみましょう。\n",
"\n",
"結果はplt.show()で表示させてみましょう。\n",
"\n",
"X軸:2theta\n",
"\n",
"Y軸:Intensity"
],
"metadata": {
"id": "NYu8HHRdT0dH"
}
},
{
"cell_type": "code",
"source": [
"import figsetting as fs\n",
"from matplotlib import pyplot as plt\n",
"\n",
"CONFIG = fs.CONFIG\n",
"plt.rcParams.update(CONFIG)\n",
"FONT_LABEL = fs.FONT_LABEL\n",
"FONT_TITLE = fs.FONT_TITLE\n",
"\n",
"# 図の設定\n",
"fig, ax = plt.subplots(1,1, figsize = fs.FIGSIZE)\n",
"\n",
"# 可視化パラメータ\n",
"X = df_data1[\"2theta\"]\n",
"Y = df_data1[\"Intensity\"]\n",
"LABEL_X = \"2theta\"\n",
"LABEL_Y = \"Intensity\"\n",
"GRAPH_TITLE = \"data1\"\n",
"\n",
"# 可視化\n",
"ax.plot(X, Y, color = fs.LINE_COLOR)\n",
"\n",
"# X軸ラベル設定\n",
"ax.set_xlabel(LABEL_X, **FONT_LABEL)\n",
"\n",
"# Y軸ラベル設定\n",
"ax.set_ylabel(LABEL_Y, **FONT_LABEL)\n",
"\n",
"# 軸の設定\n",
"ax.minorticks_on()\n",
"ax.tick_params(direction=\"in\", which=\"both\", length=5, labelsize=16)\n",
"ax.grid(which=\"major\", axis=\"both\", color=\"gray\", linestyle=\"--\", linewidth=0.6)\n",
"ax.set_xlim([25, 60])\n",
"ax.set_ylim([0, 4200])\n",
"\n",
"# グラフタイトルの設定\n",
"ax.set_title(GRAPH_TITLE, **FONT_TITLE)\n",
"\n",
"# 表示\n",
"plt.show()"
],
"metadata": {
"id": "sCJsneZyQ8M-"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### 折れ線を少し縦方向にずらして重ね合ねて描いてみよう!\n",
"\n",
"**[課題3-2]**\n",
"\n",
"**df_data1**のデータの上に、**df_data2**を重ねて描いてみよう。\n",
"\n",
"結果はplt.show()で表示させてみましょう。\n",
"\n",
"X軸:2theta\n",
"\n",
"Y軸:Intensity\n",
"\n",
"ヒント:Y値にプラス・・・"
],
"metadata": {
"id": "QVcYQf3lXE_v"
}
},
{
"cell_type": "code",
"source": [
"# 図の設定\n",
"fig, ax = plt.subplots(1,1, figsize = fs.FIGSIZE)\n",
"\n",
"# 可視化パラメータ\n",
"X1 = df_data1[\"2theta\"]\n",
"Y1 = df_data1[\"Intensity\"]\n",
"X2 = df_data2[\"2theta\"]\n",
"Y2 = df_data2[\"Intensity\"]\n",
"LABEL_X = \"2theta\"\n",
"LABEL_Y = \"Intensity\"\n",
"GRAPH_TITLE = \"data1とdata2\"\n",
"DISTANCE = 500\n",
"\n",
"# 可視化\n",
"ax.plot(X, Y, color = fs.LINE_COLOR)\n",
"ax.plot(X, Y+DISTANCE, color = fs.LINE_COLOR2)\n",
"\n",
"# X軸ラベル設定\n",
"ax.set_xlabel(LABEL_X, **FONT_LABEL)\n",
"\n",
"# Y軸ラベル設定\n",
"ax.set_ylabel(LABEL_Y, **FONT_LABEL)\n",
"\n",
"# 各線の右側にテキストを挿入\n",
"ax.text(60.3, 0*DISTANCE, \"data1\", **FONT_LABEL)\n",
"ax.text(60.3, 1*DISTANCE, \"data2\", **FONT_LABEL)\n",
"\n",
"# 軸の設定\n",
"ax.minorticks_on()\n",
"ax.tick_params(direction=\"in\", which=\"both\", length=5, labelsize=16)\n",
"ax.grid(which=\"major\", axis=\"both\", color=\"gray\", linestyle=\"--\", linewidth=0.6)\n",
"ax.set_xlim([25, 60])\n",
"ax.set_ylim([0, 5200])\n",
"\n",
"# グラフタイトルの設定\n",
"ax.set_title(GRAPH_TITLE, **FONT_TITLE)\n",
"\n",
"# 表示\n",
"plt.show()"
],
"metadata": {
"id": "U-Wp6DO0Xh4m"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### 折れ線の下側を塗りつぶしてみよう!\n",
"\n",
"**[課題3-3]**\n",
"\n",
"**df_data1**のデータの折れ線の下側を薄い赤色で塗りつぶしてみましょう。\n",
"\n",
"結果はplt.show()で表示させてみましょう。\n",
"\n",
"X軸:2theta\n",
"\n",
"Y軸:Intensity\n",
"\n",
"ヒント:fill_between"
],
"metadata": {
"id": "06gOyC9eaQhN"
}
},
{
"cell_type": "code",
"source": [
"# 図の設定\n",
"fig, ax = plt.subplots(1,1, figsize = fs.FIGSIZE)\n",
"\n",
"# 可視化パラメータ\n",
"X = df_data1[\"2theta\"]\n",
"Y = df_data1[\"Intensity\"]\n",
"LABEL_X = \"2theta\"\n",
"LABEL_Y = \"Intensity\"\n",
"GRAPH_TITLE = \"data1\"\n",
"\n",
"# 可視化\n",
"ax.fill_between(X, Y, color=\"red\", alpha=0.5)\n",
"ax.plot(X, Y, color = fs.LINE_COLOR)\n",
"\n",
"# X軸ラベル設定\n",
"ax.set_xlabel(LABEL_X, **FONT_LABEL)\n",
"\n",
"# Y軸ラベル設定\n",
"ax.set_ylabel(LABEL_Y, **FONT_LABEL)\n",
"\n",
"# 軸の設定\n",
"ax.minorticks_on()\n",
"ax.tick_params(direction=\"in\", which=\"both\", length=5, labelsize=16)\n",
"ax.grid(which=\"major\", axis=\"both\", color=\"gray\", linestyle=\"--\", linewidth=0.6)\n",
"ax.set_xlim([27, 30])\n",
"ax.set_ylim([0, 4500])\n",
"\n",
"# グラフタイトルの設定\n",
"ax.set_title(GRAPH_TITLE, **FONT_TITLE)\n",
"\n",
"# 表示\n",
"plt.show()"
],
"metadata": {
"id": "oHizmfcobQCQ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### フィッティングしてみよう!\n",
"\n",
"**[スペシャル課題3-3]**\n",
"\n",
"**df_data1**の2thetaの区間27~30をフィッティングしてみよう。\n",
"\n",
"結果はplt.show()で表示させてみましょう。\n",
"\n",
"X軸:2theta\n",
"\n",
"Y軸:Intensity\n",
"\n",
"ヒント:fill_between"
],
"metadata": {
"id": "2vZiEY8Dam6e"
}
},
{
"cell_type": "code",
"source": [
"import numpy as np\n",
"from scipy.stats import norm\n",
"from scipy.optimize import curve_fit\n",
"\n",
"# ガウシアンフィッティング用の関数を準備する\n",
"def gauss(x, a, mu, sigma):\n",
" return a*np.exp(-(x-mu)**2/(2*sigma**2))\n",
"\n",
"# 対象範囲を絞り込む\n",
"mask = (df_data1[\"2theta\"]>=27) & (df_data1[\"2theta\"]<=30)\n",
"X = df_data1[mask][\"2theta\"]\n",
"Y = df_data1[mask][\"Intensity\"]\n",
"\n",
"# ガウシアンフィッティングを実行する\n",
"param_ini = [4000, 28.4, 0.1]\n",
"popt, pcov = curve_fit(gauss, X, Y, p0=param_ini)\n",
"fitting = gauss(X, popt[0],popt[1],popt[2])\n",
"\n",
"# 図の設定\n",
"fig, ax = plt.subplots(1,1, figsize = fs.FIGSIZE)\n",
"\n",
"# 可視化パラメータ\n",
"LABEL_X = \"2theta\"\n",
"LABEL_Y = \"Intensity\"\n",
"GRAPH_TITLE = \"data1 and fitting\"\n",
"\n",
"# 可視化\n",
"ax.fill_between(X, Y, color=\"red\", alpha=0.5)\n",
"ax.plot(X, Y, color = fs.LINE_COLOR, label=\"data1\")\n",
"ax.plot(X, fitting, color = \"blue\", linewidth=2, label=\"fitting\")\n",
"\n",
"# X軸ラベル設定\n",
"ax.set_xlabel(LABEL_X, **FONT_LABEL)\n",
"\n",
"# Y軸ラベル設定\n",
"ax.set_ylabel(LABEL_Y, **FONT_LABEL)\n",
"\n",
"# 軸の設定\n",
"ax.minorticks_on()\n",
"ax.tick_params(direction=\"in\", which=\"both\", length=5, labelsize=16)\n",
"ax.grid(which=\"major\", axis=\"both\", color=\"gray\", linestyle=\"--\", linewidth=0.6)\n",
"ax.set_xlim([27, 30])\n",
"ax.set_ylim([0, 4500])\n",
"\n",
"# 凡例の設定\n",
"ax.legend(fontsize=18) \n",
"\n",
"# グラフタイトルの設定\n",
"ax.set_title(GRAPH_TITLE, **FONT_TITLE)\n",
"\n",
"# 表示\n",
"plt.show()"
],
"metadata": {
"id": "qwWrqnbVf3o_"
},
"execution_count": null,
"outputs": []
}
]
}