実験で得られた値をグラフに描画しようとしています。
3つの軸を1つのグラフに描画したいのですが、なぜが1つしか描画されません。
3軸一つ一つ個別に描画することは可能でした。また、実験データをCSVファイルに保存し、pandasのread_CSVを用いて読み込ませていますが、きちんと読み込まれているようです。
環境
- Python 3.10.2
- pandas 1.4.3
- matplotlib 3.5.3
ソースコード
python
1import pandas as pd 2import numpy as np 3import matplotlib.pyplot as plt 4from scipy.signal import savgol_filter 5from scipy.interpolate import interp1d 6 7# ファイルパス 8CSV_PATH = "./cell_I-V_1-3.csv" 9FIG_PATH = "./graph.png" 10 11# CSV読み込み 12df = pd.read_csv(CSV_PATH) 13 14def spline_interp(in_x, in_y): 15 out_x = np.linspace(np.min(in_x), np.max(in_x), np.size(in_x)*100) # もとのxの個数より多いxを用意 16 func_spline = interp1d(in_x, in_y, kind='cubic') # cubicは3次のスプライン曲線 17 out_y = func_spline(out_x) # func_splineはscipyオリジナルの型 18 19 return out_x, out_y 20 21def yhat(in_y): 22 yhat = savgol_filter(df[in_y], 25, 1) 23 return yhat 24 25def plot_graph(): 26 # デフォルト設定 27 config = { 28 "font.family":"Noto Sans JP", 29 "xtick.direction":"in", 30 "ytick.direction":"in", 31 "xtick.minor.visible":True, 32 "ytick.minor.visible":True, 33 "legend.loc":"best", 34 } 35 plt.rcParams.update(config) 36 37 # グラフのキャンバス生成 38 fig, ax1 = plt.subplots(figsize=(20, 10)) 39 40 # タイトル表示 41 ax1.set_title("太陽電池セルシュミレーションIV", loc="center", y=-0.13, fontsize=20) 42 43 44 # axXX番代は近似曲線用 45 ax11 = ax1.twinx() 46 ax2 = ax1.twinx() 47 ax22 = ax1.twinx() 48 ax3 = ax1.twinx() 49 ax33 = ax1.twinx() 50 51 # plot y1(I0) 52 ax11.plot(df["V"], df["I1"], alpha=0.7) # 近似曲線描画 53 ax11.axis("off") # 近似曲線の軸を削除 54 ax1.scatter(df["V"], df["I1"], marker=".", s=40, label="$Situation 1$") 55 56 ax22.plot(df["V"], df["I2"], alpha=0.7) 57 ax22.axis("off") 58 ax2.scatter(df["V"], df["I2"], marker="^", s=40, label="$Situation 2$") 59 ax2.spines["right"].set_position(("outward", 0)) 60 61 ax33.plot(df["V"], df["I3"], alpha=0.7) 62 ax33.axis("off") 63 ax3.scatter(df["V"], df["I3"], marker="s", s=40, label="$Situation 3$") 64 ax3.spines["right"].set_position(("outward", 60)) 65 66 # 凡例のためにハンドルして表示 67 handler1, label1 = ax1.get_legend_handles_labels() 68 handler2, label2 = ax2.get_legend_handles_labels() 69 handler3, label3 = ax3.get_legend_handles_labels() 70 ax1.legend(handler1 + handler2 + handler3, label1 + label2 + label3, loc=4, borderaxespad=0) 71 72 # X軸の範囲 手動設定推奨 73 ax1.set_xlim() 74 75 # 軸ラベル フォントファミリーを適切に設定すれば日本語可能 76 ax1.set_xlabel("セル電圧 $[V]$") 77 ax1.set_ylabel("セル電流条件1 $[A]$") 78 ax2.set_ylabel("セル電流条件2 $[A]$") 79 ax3.set_ylabel("セル電流条件3 $[A]$") 80 81 # グリッドの表示 82 ax1.grid(which="both") 83 84 # 画像を保存 85 fig.savefig(FIG_PATH) 86 87 print(df) 88 89def main(): 90 plot_graph() 91 92if __name__ == "__main__": 93 main() 94
CSVの中身
csv
1 V I1 I2 I3 20 0.00 1.700495 1.020495 0.340495 31 0.01 1.700395 1.020395 0.340395 42 0.02 1.700295 1.020295 0.340295 53 0.03 1.700195 1.020195 0.340195 64 0.04 1.700095 1.020095 0.340095 7.. ... ... ... ... 860 0.60 1.609406 0.929406 0.249406 961 0.61 1.580916 0.900916 0.220916 1062 0.62 1.542955 0.862955 0.182955 1163 0.63 1.492360 0.812360 0.132360 1264 0.64 1.424919 0.744919 0.064919 13 14[65 rows x 4 columns]

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。