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

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

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

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

Python

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

Q&A

解決済

1回答

1199閲覧

matplotlib.animationでグラフの曲線だけでなく、目盛もアニメーションさせたい

hayaya88

総合スコア7

Matplotlib

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

Python

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

0グッド

0クリップ

投稿2020/04/30 23:08

matplotlib.animationで定期的にやってくるデータを逐次更新して描画するソフトを作っています。データを表す曲線の更新はうまく行くのですが、時系列の数値や目盛(下図の赤丸で囲った箇所)が更新されないので、こちらも更新させたいです。
イメージ説明

調べたところ、

python

1 for i in range( cd.y_data.shape[0]): 2 cd.lines[i].set_data( cd.x_data, cd.y_data[i]) 3 4 print( ii) 5 #fig.canvas.draw() 6 return cd.lines[0], cd.lines[1], cd.lines[2], cd.lines[3], cd.lines[4], cd.lines[5]

という感じにすると、cd.lines[]にはグラフのデータだけが入っているため、目盛や数値が更新されないとのことです。
そのため、これらのデータもcd.lines[]に統合すればいいとのことなのですが、これの書き方が分かりません。
具体的にどのようにすればいいのでしょうか?
以下が、全コードになります。

python

1import sys 2import time 3import numpy as np 4import tkinter 5import random 6from tkinter import ttk 7import matplotlib as mpl 8import matplotlib.pyplot as plt 9import matplotlib.animation as animation 10from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg 11 12class ChartData(): 13 def __init__( self, series:int): 14 self.MAT_SIZE = 100 # matrix size 15 16 self.count = 0 # packet counter 17 self.x_data = np.arange( -100, 0, 1) # x-axis data 18 self.y_data = np.zeros( ( series, self.MAT_SIZE)) # y-axis data 19 self.lines = [] # chart lines 20 self.axes = [] # chart axes 21 22# @brief update combobox 23# @param event event handler 24def UpdatePortCombo( event): 25 print( cb_port_val.get()) 26 27# @brief quit program 28def Quit(): 29 # terminate serial receive thread 30 root.destroy() 31 32# @brief open serial port 33def OpenPort(): 34 return True 35 36# @brief close serial port 37def ClosePort(): 38 return True 39 40# @brief main function 41if __name__ == '__main__': 42 series = 6 # data series 43 packet_size = 4 * ( 6 + 1) # packet size 44 45 # initialize GUI object 46 root = tkinter.Tk() 47 root.title( 'CQ ARM First Telemetory') 48 49 cb_port_val = tkinter.StringVar() 50 cd = ChartData( series = series) 51 52 """ GUI parts draw part """ 53 dbg = ['aaa', 'bbb'] 54 # serial port select combobox 55 cb_port = ttk.Combobox( values = dbg, textvariable = cb_port_val, width = 7) 56 cb_port.bind( '<<ComboboxSelected>>', UpdatePortCombo) 57 cb_port.current( 0) 58 cb_port.grid( row = 0, column = 1, columnspan = 10) 59 60 # open serial port & monitor start button 61 btn_open = tkinter.Button( text = 'Open Port', width = 10, command = OpenPort) 62 btn_open.grid( row = 1, column = 1, columnspan = 1) 63 64 # close serial port & monitor stop button 65 btn_close = tkinter.Button( text = 'Close Port', width = 10, command = ClosePort) 66 btn_close.grid( row = 3, column = 1, columnspan = 1) 67 68 # quit app button 69 btn_quit = tkinter.Button( text = 'Quit', width = 10, command = Quit) 70 btn_quit.grid( row = 4, column = 1, columnspan = 1) 71 72 """ chart draw part """ 73 # create chart base 74 fig, cd.axes = plt.subplots( 2, 1) 75 canvas = FigureCanvasTkAgg( fig, master = root) 76 canvas.get_tk_widget().grid( row = 0, column = 0, rowspan = 100,) 77 78 cd.axes[0].set_xlabel( 'time') 79 cd.axes[0].set_ylabel( 'acceleration[mm/s^2]') 80 cd.axes[0].set_title( 'time - acceleration chart') 81 cd.axes[0].grid() 82 cd.axes[0].xaxis.set_major_locator( mpl.ticker.AutoLocator()) 83 cd.axes[0].yaxis.set_major_locator( mpl.ticker.AutoLocator()) 84 cd.axes[0].set_ylim( 0.0, 1.0) 85 line_x, = cd.axes[0].plot( cd.x_data, cd.y_data[0], color = "red", label = 'x') 86 line_y, = cd.axes[0].plot( cd.x_data, cd.y_data[1], color = "green", label = 'y') 87 line_z, = cd.axes[0].plot( cd.x_data, cd.y_data[2], color = "blue", label = 'z') 88 cd.lines.append( line_x) 89 cd.lines.append( line_y) 90 cd.lines.append( line_z) 91 92 cd.axes[1].set_xlabel( 'time') 93 cd.axes[1].set_ylabel( 'angular velocity[mdeg/s]') 94 cd.axes[1].set_title( 'time - angular velocity chart') 95 cd.axes[1].grid() 96 cd.axes[1].set_ylim( 0.0, 1.0) 97 cd.axes[1].xaxis.set_major_locator( mpl.ticker.AutoLocator()) 98 cd.axes[1].yaxis.set_major_locator( mpl.ticker.AutoLocator()) 99 line_x, = cd.axes[1].plot( cd.x_data, cd.y_data[3], color = "red", label = 'x') 100 line_y, = cd.axes[1].plot( cd.x_data, cd.y_data[4], color = "green", label = 'y') 101 line_z, = cd.axes[1].plot( cd.x_data, cd.y_data[5], color = "blue", label = 'z') 102 cd.lines.append( line_x) 103 cd.lines.append( line_y) 104 cd.lines.append( line_z) 105 106 fig.canvas.draw() 107 108 # @biref run chart 109 def run( ii): 110 xx_data = [ii] 111 yy_data = [[random.random(),random.random(),random.random(),random.random(),random.random(),random.random()]] 112 cd.x_data = np.roll( cd.x_data, shift = -1) 113 cd.y_data = np.roll( cd.y_data, shift = -1, axis = 1) 114 cd.x_data[-1] = xx_data[0] 115 cd.y_data[:,-1] = yy_data[0] 116 117 for ax in cd.axes: 118 ax.set_xlim( cd.x_data[0], cd.x_data[-1]) 119 120 for i in range( cd.y_data.shape[0]): 121 cd.lines[i].set_data( cd.x_data, cd.y_data[i]) 122 123 print( ii) 124 #fig.canvas.draw() 125 return cd.lines[0], cd.lines[1], cd.lines[2], cd.lines[3], cd.lines[4], cd.lines[5] 126 127 anime = animation.FuncAnimation( fig, run, interval = 500, blit = True) 128 129 # app start 130 root.mainloop() 131 132 exit(0) 133

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

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

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

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

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

guest

回答1

0

ベストアンサー

python

1anime = animation.FuncAnimation( fig, run, interval = 500, blit = False)

で動きました。

イメージ説明


参考

blitを設定すると更新されないとの報告有。(未closeのissue)

blit=True で xlim の描画を更新が出来ている例。
Jupyter上&動画としてファイルへ書きだしてるので、
tkinter上ではこのアプローチでの解決は難しいと思います。

投稿2020/05/01 01:18

teamikl

総合スコア8664

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

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

hayaya88

2020/05/01 03:03

ありがとうございます。無事、目盛も移動するようになりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問