!wget https://github.com/tendo-sms/python_beginner_2023/raw/main/files_5/files.zip . !unzip files.zip !mv files/* . import matplotlib.pyplot as plt # Figureの作成 fig = plt.figure() # Axesの作成 ax = fig.subplots() # 折れ線グラフのプロット ax.plot([1, 2, 3], [10, 100, 1000]) # グラフの画面表示 plt.show() # グラフのクローズ plt.close(fig) import matplotlib.pyplot as plt import pandas as pd # CSVファイルをデータフレームに読み込む df = pd.read_csv("spectrum1.csv") # Figureの作成 fig = plt.figure() # Axesの作成 ax = fig.subplots() # 折れ線グラフのプロット # 引数にはpandasのSeriesを指定する ax.plot(df["Wavelength"], df["Intensity"]) # グラフの画面表示 plt.show() # グラフのクローズ plt.close(fig) import matplotlib.pyplot as plt import pandas as pd # CSVファイルをデータフレームに読み込む ★読み込むファイルを変更 df = pd.read_csv("spectrum2.csv") # Figureの作成 fig = plt.figure() # Axesの作成 ax = fig.subplots() # 折れ線グラフのプロット ★変更 ax.plot(df["Wavelength"], df["Intensity1"]) ax.plot(df["Wavelength"], df["Intensity2"]) # グラフの画面表示 plt.show() # グラフのクローズ plt.close(fig) import matplotlib.pyplot as plt import pandas as pd # CSVファイルをデータフレームに読み込む df = pd.read_csv("spectrum2.csv") # Figureの作成 fig = plt.figure() # Axesの作成 ★引数を変更 ax = fig.subplots(2, 1) # 折れ線グラフのプロット ★2つのプロットに変更 ax[0].plot(df["Wavelength"], df["Intensity1"]) ax[1].plot(df["Wavelength"], df["Intensity2"]) # グラフの画面表示 plt.show() # グラフのクローズ plt.close(fig) import matplotlib.pyplot as plt import pandas as pd # CSVファイルをデータフレームに読み込む df = pd.read_csv("spectrum2.csv") # Figureの作成 fig = plt.figure() # Axesの作成 ax = fig.subplots() # 折れ線グラフのプロット ★引数を追加 ax.plot(df["Wavelength"], df["Intensity1"], color="red") ax.plot(df["Wavelength"], df["Intensity2"], linestyle="--") # グラフの画面表示 plt.show() # グラフのクローズ plt.close(fig) import matplotlib.pyplot as plt import pandas as pd # CSVファイルをデータフレームに読み込む df = pd.read_csv("spectrum2.csv") # Figureの作成 fig = plt.figure() # Axesの作成 ax = fig.subplots() # 折れ線グラフのプロット ax.plot(df["Wavelength"], df["Intensity1"], color="red") ax.plot(df["Wavelength"], df["Intensity2"], linestyle="--") # グラフのタイトルを設定 ★追加 ax.set_title("Spectrum Graph") # グラフの画面表示 plt.show() # グラフのクローズ plt.close(fig) import matplotlib.pyplot as plt import pandas as pd # CSVファイルをデータフレームに読み込む df = pd.read_csv("spectrum2.csv") # Figureの作成 fig = plt.figure() # Axesの作成 ax = fig.subplots() # グラフのタイトルを設定 ★位置を変更 ax.set_title("Spectrum Graph") # 折れ線グラフのプロット ax.plot(df["Wavelength"], df["Intensity1"], color="red") ax.plot(df["Wavelength"], df["Intensity2"], linestyle="--") # グラフの画面表示 plt.show() # グラフのクローズ plt.close(fig) import matplotlib.pyplot as plt import pandas as pd # CSVファイルをデータフレームに読み込む df = pd.read_csv("spectrum2.csv") # Figureの作成 fig = plt.figure() # Axesの作成 ax = fig.subplots() # 折れ線グラフのプロット ax.plot(df["Wavelength"], df["Intensity1"], color="red") ax.plot(df["Wavelength"], df["Intensity2"], linestyle="--") # グラフのタイトルを設定 ax.set_title("Spectrum Graph") # 軸のラベルを設定 ★追加 ax.set_xlabel("Wavelength") ax.set_ylabel("Intensity") # グラフの画面表示 plt.show() # グラフのクローズ plt.close(fig) import matplotlib.pyplot as plt import pandas as pd # CSVファイルをデータフレームに読み込む df = pd.read_csv("spectrum2.csv") # Figureの作成 fig = plt.figure() # Axesの作成 ax = fig.subplots() # 折れ線グラフのプロット ★引数を変更 ax.plot(df["Wavelength"], df["Intensity1"], color="red", label="Intensity1") ax.plot(df["Wavelength"], df["Intensity2"], linestyle="--", label="Intensity2") # グラフのタイトルを設定 ax.set_title("Spectrum Graph") # 軸のラベルを設定 ax.set_xlabel("Wavelength") ax.set_ylabel("Intensity") # 凡例を設定 ★追加 ax.legend() # グラフの画面表示 plt.show() # グラフのクローズ plt.close(fig) import matplotlib.pyplot as plt import pandas as pd # CSVファイルをデータフレームに読み込む df = pd.read_csv("spectrum2.csv") # Figureの作成 fig = plt.figure() # Axesの作成 ax = fig.subplots() # 折れ線グラフのプロット ax.plot(df["Wavelength"], df["Intensity1"], color="red", label="Intensity1") ax.plot(df["Wavelength"], df["Intensity2"], linestyle="--", label="Intensity2") # グラフのタイトルを設定 ax.set_title("Spectrum Graph") # 軸のラベルを設定 ax.set_xlabel("Wavelength") ax.set_ylabel("Intensity") # 凡例を設定 ax.legend() # 補助線を設定 ★ ax.grid() # グラフの画面表示 plt.show() # グラフのクローズ plt.close(fig) import matplotlib.pyplot as plt import pandas as pd # フォントマネージャーのインポート ★追加 from matplotlib import font_manager # フォントファイルをMatplotlibに認識させる ★追加 fp = font_manager.FontProperties(fname="NotoSansJP-Medium.otf") # CSVファイルをデータフレームに読み込む df = pd.read_csv("spectrum2.csv") # Figureの作成 fig = plt.figure() # Axesの作成 ax = fig.subplots() # 折れ線グラフのプロット ax.plot(df["Wavelength"], df["Intensity1"], color="red", label="Intensity1") ax.plot(df["Wavelength"], df["Intensity2"], linestyle="--", label="Intensity2") # フォントの設定 ★ hfont = {"fontproperties": fp, "size": 15} # グラフのタイトルを設定 ax.set_title("スペクトルのグラフ", **hfont) # 軸のラベルを設定 ax.set_xlabel("波長", **hfont) ax.set_ylabel("強度", **hfont) # 凡例を設定 ax.legend() # 補助線を設定 ax.grid() # グラフの画面表示 plt.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 import pandas as pd # CSVファイルをデータフレームに読み込む df = pd.read_csv("spectrum2.csv") # Figureの作成 fig = plt.figure() # Axesの作成 ax = fig.subplots() # 折れ線グラフのプロット ax.plot(df["Wavelength"], df["Intensity1"], color="red", label="Intensity1") ax.plot(df["Wavelength"], df["Intensity2"], linestyle="--", label="Intensity2") # グラフのタイトルを設定 ax.set_title("Spectrum Graph") # 軸のラベルを設定 ax.set_xlabel("Wavelength") ax.set_ylabel("Intensity") # 凡例を設定 ax.legend() # 補助線を設定 ax.grid() # グラフの画面表示 plt.show() # グラフの保存 ★追加 fig.savefig("spectrum_graph.jpg", dpi=100, format="jpg") # グラフのクローズ plt.close(fig) import matplotlib.pyplot as plt import pandas as pd # CSVファイルをデータフレームに読み込む df = pd.read_csv("scatter.csv") # Figureの作成 fig = plt.figure() # Axesの作成 ax = fig.subplots() # 散布図のプロット ★線グラフのplotをscatterに変更 ax.scatter(df["Content"], df["Hardness"]) # グラフのタイトルを設定 ax.set_title("Scatter") # 軸のラベルを設定 ax.set_xlabel("Content") ax.set_ylabel("Hardness") # グラフの画面表示 plt.show() # グラフのクローズ plt.close(fig) import matplotlib.pyplot as plt import pandas as pd # CSVファイルをデータフレームに読み込む df = pd.read_csv("hist.csv") # Figureの作成 fig = plt.figure() # Axesの作成 ax = fig.subplots() # ヒストグラムのプロット ★線グラフのplotをhistに変更 ax.hist(df["Particle size"], bins=10) # グラフのタイトルを設定 ax.set_title("Histgram") # 軸のラベルを設定 ax.set_xlabel("Particle size (nm)") ax.set_ylabel("Frequency") # グラフの画面表示 plt.show() # グラフのクローズ plt.close(fig) import matplotlib.pyplot as plt import pandas as pd # CSVファイルをpandasのデータフレームへ読み込む df = pd.read_csv("heatmap.csv") # Figureの作成 fig = plt.figure() # Axesの作成 ax = fig.subplots() # ヒートマップの作成 im = ax.imshow(df) # カラーバーの作成 fig.colorbar(im) # グラフのタイトルを設定 ax.set_title("Heatmap") # グラフの画面表示 plt.show() # グラフのクローズ plt.close(fig) import matplotlib.pyplot as plt import matplotlib.cm as cm import pandas as pd # CSVファイルをpandasのデータフレームへ読み込む df = pd.read_csv("heatmap.csv") fig = plt.figure() ax = fig.subplots() # ヒートマップの作成 ★変更 im = ax.imshow(df, cmap="autumn") # カラーバーの作成 fig.colorbar(im) # グラフのタイトルを設定 ax.set_title("Heatmap") # グラフの画面表示 plt.show() # グラフのクローズ plt.close(fig) import matplotlib.pyplot as plt # Figureの作成 fig = plt.figure() # Axesの作成 ax = fig.subplots() # 折れ線グラフのプロット ax.plot([1, 2, 3], [10, 100, 1000]) # 軸のラベルを設定 ax.set_xlabel("X-Axis") ax.set_ylabel("Y-Axis") # グラフの画面表示 plt.show() # グラフのクローズ plt.close(fig) import matplotlib.pyplot as plt # 折れ線グラフのプロット plt.plot([1, 2, 3], [10, 100, 1000]) # 軸のラベルを設定 plt.xlabel("X-Axis") plt.ylabel("Y-Axis") # グラフの画面表示 plt.show() # グラフのクローズ plt.close() import matplotlib.pyplot as plt # ★Axesインタフェース★で折れ線グラフを作成 # fig1 = plt.figure() # ax1 = fig1.subplots() # ax1.plot([0, 1, 2], [10, 100, 1000]) # ★Axesインタフェース★で散布図を作成 fig2 = plt.figure() ax2 = fig2.subplots() ax2.scatter([0, 1, 2, 3, 4], [50, 20, 30, 10, 20]) # ★Axesインタフェース★で折れ線グラフを作成 fig1 = plt.figure() ax1 = fig1.subplots() ax1.plot([0, 1, 2], [10, 100, 1000]) # ★pyplotインタフェース★で軸にラベルを付与 plt.xlabel("X-Axis") plt.ylabel("Y-Axis") # グラフの画面表示 plt.show() # グラフのクローズ plt.close(fig1) plt.close(fig2) import pandas as pd df = pd.read_csv("spectrum1.csv") df.plot("Wavelength", "Intensity", linestyle="--") import seaborn as sns iris = sns.load_dataset("iris") sns.pairplot(iris) import plotly.graph_objs as go import numpy as np # プロットデータの作成 t = np.linspace(0, 10*np.pi, 1000) x = np.cos(t) y = np.sin(t) z = t # グラフを作成 fig = go.Figure(data=go.Scatter3d(x=x, y=y, z=z, mode='lines')) fig.show()