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

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

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

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

Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

Python

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

Q&A

解決済

1回答

2721閲覧

guiで再描画されたときのカラーバーを消したい

h.yano1120

総合スコア9

Matplotlib

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

Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

Python

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

0グッド

0クリップ

投稿2021/10/07 11:25

編集2021/10/10 05:15

前提・実現したいこと

guiで再描画したときに1回目で描画して残ってしまうcolorbarを消したいです、宜しくお願い致します

発生している問題

2枚目の画像の様に再描画すると画像のカラーバーが残ってしまいます。
1回目の描画
イメージ説明
2回目の描画
イメージ説明
##コード

python

1import tkinter as tk 2from tkinter import ttk 3import matplotlib.pyplot as plt 4import math 5from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg 6import numpy as np 7import matplotlib.gridspec as gridspec 8 9#ファイルのプロット処理 10class c_inputData(ttk.Frame): 11 def __init__(self, inputFrame, **kw): 12 # superクラス呼び出し 13 super().__init__(**kw) 14 # 上部テキスト side:位置 15 tk.Label(inputFrame, text = "logistic, Dimension=").pack(side=tk.LEFT) 16 self.trgFunc = tk.IntVar() 17 # combobox:ドロップダウンを作成 18 list = [2] 19 self.ComboboxTrgFunc = ttk.Combobox(inputFrame, values=list, width=2) 20 # tk.END:文字の後にカーソル, 第2引数:初期値 21 self.ComboboxTrgFunc.insert(tk.END, 2) 22 self.ComboboxTrgFunc.pack(side=tk.LEFT) 23 tk.Label(inputFrame, text="τ=").pack(side=tk.LEFT) 24 self.trgFunc2 = tk.IntVar() 25 # ドロップダウン内容:Values=[sin,cos,tan] 26 list2 = [1, 2, 3] 27 # combobox:ドロップダウンを作成 28 self.ComboboxTrgFunc2 = ttk.Combobox(inputFrame, values=list2, width=2) 29 # tk.END:文字の後にカーソル, 第2引数:初期値 30 self.ComboboxTrgFunc2.insert(tk.END, 1) 31 self.ComboboxTrgFunc2.pack(side=tk.LEFT) 32 def sub(*args): 33 n = [] 34 t = 0 35 sum = 0 36 for i in args: 37 n.append(i) 38 t += 1 39 for i in range(t): 40 if i == 0 or i % 2 == 0: 41 sum += (n[i] - n[i+1]) ** 2 42 return sum 43 # ノルムを返す 44 def norm(*x): 45 return math.sqrt(c_inputData.sub(*x)) 46 47 def attractor_RP(x, timeX, timeY, timeZ, D, T): 48 thr = 0.1 49 if D == 2: 50 for i in range(100 + T * D): 51 for t in range(100 + T * D): 52 if i < 100 and t <= 100: 53 timeX[i][t] = c_inputData.norm(x[i], x[t], x[i+T], x[t+T]) 54 55 if c_inputData.norm(x[i], x[t], x[i+T], x[t+T]) < thr: 56 timeY.append(i) 57 timeZ.append(t) 58 else: 59 timeY.append(-1) 60 timeZ.append(-1) 61 62 def plot_sct(self, canvas, ax1, ax2, ax3): 63 D = int(self.ComboboxTrgFunc.get()) 64 T = int(self.ComboboxTrgFunc2.get()) 65 ax1.cla() #前の描画データの削除 66 ax2.cla() #前の描画データの削除 67 ax3.cla() #前の描画データの削除 68 # カラーマップ 69 cm = plt.cm.get_cmap('RdYlBu') 70 x = [0.101] 71 for i in range(110): 72 if i < 109: 73 x.append(4 * x[i] * (1 - x[i])) 74 75 ax1.plot(x) 76 timeX = [[0]*len(x) for i in range(len(x))] 77 timeY = [] 78 timeZ = [] 79 c_inputData.attractor_RP(x, timeX, timeY, timeZ, D, T) 80 ax2.scatter(timeY, timeZ, c="k", s=2) 81 img = ax3.pcolormesh(timeX, cmap='RdYlBu') 82 cax = plt.colorbar(img) 83 canvas.draw() #Canvasを更新 84 85if __name__ == "__main__": 86 ### rootオブジェクト生成 ### 87 root = tk.Tk() 88 root.title("Recurrence plot") 89 root.geometry("400x400") 90 ### フレームの作成 ### 91 inputFrame = ttk.Frame(root) # 入力エリア 92 buttonFrame = ttk.Frame(root) # ボタンを表示するエリア 93 graphFrame = ttk.Frame(root) # グラフを描画するエリア 94 ### inputFrame ### 95 inputData = c_inputData(inputFrame) 96 # 配置 97 inputFrame.pack() 98 ### buttonFrame ### 99 #Plotボタン 100 ButtonWidth = 15 101 UpdateButton = tk.Button(buttonFrame, text="Plot", width=ButtonWidth, 102 command = lambda:inputData.plot_sct(Canvas, ax1, ax2, ax3)) 103 UpdateButton.grid(row = 0, column = 0) 104 buttonFrame.pack() 105 #グラフの初期化 106 fig,ax = plt.subplots() 107 gs = gridspec.GridSpec(2,2) 108 ax1 = plt.subplot(gs[0,0:]) 109 ax2 = plt.subplot(gs[1,0], aspect="equal") 110 ax3 = plt.subplot(gs[1,1], aspect="equal") 111 #cax = plt.colorbar() 112 Canvas = FigureCanvasTkAgg(fig, master = graphFrame) #Canvasにfigを追加 113 Canvas.get_tk_widget().pack() 114 graphFrame.pack() 115 #描画を継続 116 root.mainloop()

該当箇所

python

1 cax = plt.colorbar(img) 2

補足情報(FW/ツールのバージョンなど)

python 3.8.8
jupyter Notebook

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

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

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

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

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

guest

回答1

0

ベストアンサー

元の画像の clorbar を2枚目の画像の colorbar にアップデートしたいということですね。

Python

1 #cax = plt.colorbar(img) 2 if hasattr(self, 'cax'): 3 self.cax.update_normal(img) 4 else: 5 self.cax = plt.colorbar(img)

投稿2021/10/09 13:21

lehshell

総合スコア1147

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

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

h.yano1120

2021/10/10 03:02

ありがとうございます。hasttrという便利な関数があることを知りませんでした。加えて、先程update_normalというのをmatplotlibの公式ドキュメントで知ることが出来ました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問