質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

1回答

658閲覧

python matplolib グラフが1つしか描画されない

datsuka-qwerty

総合スコア0

Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

1グッド

0クリップ

投稿2022/11/01 01:07

実験で得られた値をグラフに描画しようとしています。
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]
melian😍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

set_ylim() メソッドで y値の表示範囲を変更してみると、全て表示されていることが判るかと思います。つまり、I1, I2, I3 は重なって表示されていた、ということです。

python

1 # plot y1(I0) 2 ax11.plot(df["V"], df["I1"], alpha=0.7) # 近似曲線描画 3 ax11.set_ylim(1.4, 1.8) 4 ax11.axis("off") # 近似曲線の軸を削除 5 ax1.scatter(df["V"], df["I1"], marker=".", s=40, label="$Situation 1$") 6 ax1.set_ylim(1.4, 1.8) 7 8 ax22.plot(df["V"], df["I2"], alpha=0.7) 9 ax22.set_ylim(0.5, 1.5) 10 ax22.axis("off") 11 ax2.scatter(df["V"], df["I2"], marker="^", s=40, label="$Situation 2$") 12 ax2.spines["right"].set_position(("outward", 0)) 13 ax2.set_ylim(0.5, 1.5)

イメージ説明

投稿2022/11/01 02:24

melian

総合スコア19618

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問