!wget https://github.com/tendo-sms/python_beginner_2023/raw/main/files_5/files.zip .
!unzip files.zip
!mv files/* .
--2023-10-20 05:21:03-- https://github.com/tendo-sms/python_beginner_2023/raw/main/files_5/files.zip Resolving github.com (github.com)... 140.82.112.3 Connecting to github.com (github.com)|140.82.112.3|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://raw.githubusercontent.com/tendo-sms/python_beginner_2023/main/files_5/files.zip [following] --2023-10-20 05:21:03-- https://raw.githubusercontent.com/tendo-sms/python_beginner_2023/main/files_5/files.zip Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.111.133, 185.199.109.133, 185.199.110.133, ... Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.111.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 4209221 (4.0M) [application/zip] Saving to: ‘files.zip’ files.zip 100%[===================>] 4.01M --.-KB/s in 0.02s 2023-10-20 05:21:03 (231 MB/s) - ‘files.zip’ saved [4209221/4209221] --2023-10-20 05:21:03-- http://./ Resolving . (.)... failed: No address associated with hostname. wget: unable to resolve host address ‘.’ FINISHED --2023-10-20 05:21:03-- Total wall clock time: 0.08s Downloaded: 1 files, 4.0M in 0.02s (231 MB/s) Archive: files.zip creating: files/ inflating: files/heatmap.csv inflating: files/hist.csv inflating: files/NotoSansJP-Medium.otf inflating: files/scatter.csv inflating: files/spectrum1.csv inflating: files/spectrum2.csv inflating: files/spectrum3.csv
次のような内容のCSVファイル(spectrum3.csv)を読み込んで、1つのグラフ内に3種類のスペクトルを描画するグラフを作成してみましょう。
Wavelength | Intensity1 | Intensity2 | Intensity3 |
---|---|---|---|
300 | 409 | 298 | 542 |
310 | 447 | 349 | 553 |
320 | 383 | 393 | 552 |
330 | 563 | 480 | 613 |
以下略 | 以下略 | 以下略 | 以下略 |
グラフのタイトル、軸のラベル、および凡例を次のとおり付けてください。
import matplotlib.pyplot as plt
import pandas as pd
# CSVファイルをデータフレームに読み込む
df = pd.read_csv("spectrum3.csv")
# プログラムを作成してみましょう
# Figureの作成
fig = plt.figure()
# Axesの作成
ax = fig.subplots()
# 折れ線グラフのプロット
# ax.plot(df["Wavelength"], df["Intensity1"], label="Int1")
# ax.plot(df["Wavelength"], df["Intensity2"], label="Int2")
# ax.plot(df["Wavelength"], df["Intensity3"], label="Int3")
# for文を使ってプロット
for num in range(3):
ax.plot(df["Wavelength"], df["Intensity"+str(num+1)], label="Int"+str(num+1))
# グラフのタイトルを設定
ax.set_title("Practice Graph")
# 軸のラベルを設定
ax.set_xlabel("WL")
ax.set_ylabel("Int")
# 凡例を設定
ax.legend()
# グラフの画面表示
plt.show()
# グラフのクローズ
plt.close(fig)
import matplotlib.pyplot as plt
import pandas as pd
# CSVファイルをデータフレームに読み込む
df1 = pd.read_csv("scatter.csv")
df2 = pd.read_csv("hist.csv")
# プログラムを作成してみましょう
# Figureの作成
fig = plt.figure()
# Axesの作成
ax = fig.subplots(1, 2)
# 散布図のプロット
ax[0].scatter(df1["Content"], df1["Hardness"])
ax[1].hist(df2["Particle size"], bins=10)
# グラフのタイトルを設定
ax[0].set_title("Scatter")
ax[1].set_title("Histgram")
# 軸のラベルを設定
ax[0].set_xlabel("Content")
ax[0].set_ylabel("Hardness")
ax[1].set_xlabel("Particle size (nm)")
ax[1].set_ylabel("Frequency")
# グラフの画面表示
plt.show()
# グラフのクローズ
plt.close(fig)