LoginSignup
0
0

More than 1 year has passed since last update.

【Python】デバッグ用のグラフ描画関数

Posted at

背景

Anacondaの商用有償化でVSCodeで実装およびデバッグを行っているが、デバッグ時にグラフを書くためのコードの記述が面倒

対策

pyplotのimport、グラフの描画、メモリの解放までやってくれる関数を作成する。

コード

def dbplot(*args):
    import matplotlib.pyplot as plt
    # 可変長引数を一つの文字列に結合する
    argument = ""
    for i,val in enumerate(args):
        if i==0:
            argument += "args[{}]".format(i)
        else:
            argument += ",args[{}]".format(i)
    # 文字列をplotの引数として実行
    exec("plt.plot({})".format(argument))
    # グラフ表示
    plt.show()
    # グラフエリアをクリア
    plt.clf()
    # グラフのメモリ解放
    plt.close()

if __name__ == "__main__":
    # グラフ描画用に適当な配列を準備
    x = [1,2,3,4,5]
    y = [1,2,3,4,5]
    z = [2,4,6,8,10]

    # デバッグ用プロット関数実行
    dbplot(x,y,x,z)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0