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

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

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

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

Q&A

解決済

3回答

880閲覧

matplotlib の描画に関して

AN3000

総合スコア37

Python

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

0グッド

0クリップ

投稿2021/12/09 12:00

matplotlibでクリックをした座標にマーカーを打つプログラムですが、凡例に表示されているクリック回数とマーカーをひとつ前に戻るような機能をもたせることは可能でしょうか。(例えば100回プロットしなくてはいけない業務があったとして、ひとつでも相違の位置にプロットしてしまうと始めからやり直す手間がある)

ご存じの方、参考になるようなサイトを知っている方はご教授願います。よろしくお願いします。

python

1import matplotlib.pyplot as plt 2plt.rcParams['font.family'] = 'MS Gothic' 3markers = list('ov^<>12348sp*hH+xD') 4colors = list('bgrcmyk') 5def motion(event): 6 global i, j, cnt 7 x, y = event.xdata, event.ydata 8 plt.plot( 9 [x], [y], color=colors[j], marker=markers[i], markersize=12) 10 i, j = (i + 1) % len(markers), (j + 1) % len(colors) 11 cnt += 1 12 leg = plt.legend([f'クリック回数 {cnt}'], markerscale=0, handletextpad=-2.0, loc='upper right') 13 leg.legendHandles[0].set_visible(False) 14 plt.draw() 15 16fig, ax = plt.subplots() 17ax.set_xlim(0, 1) 18ax.set_ylim(0, 1) 19i = j = cnt = 0 20plt.connect('button_press_event', motion) 21plt.show()

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

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

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

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

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

1T2R3M4

2021/12/09 12:05

コピペで済ましているからわからないのでは。
AN3000

2021/12/09 12:27

わからない原因は私の経験、スキル不足のためです。 私が書いているコードはこの質問には不要な部分が多いため、以前の回答者様のコードをそのまま載せています。
guest

回答3

0

参考になるようなサイト

Event handling and picking - Matplotlib 3.1.0 documentation が参考になるかと思います。ここに記載されているサンプルプログラムを元に全体を書き直してみたものが以下になります(右クリックで最後にプロットした図形オブジェクトを削除)。

python

1import matplotlib.pyplot as plt 2from itertools import cycle 3 4plt.rcParams['font.family'] = 'MS Gothic' 5 6class PlaceObjects: 7 def __init__(self, fig): 8 self.markers = cycle('ov^<>12348sp*hH+xD') 9 self.colors = cycle('bgrcmyk') 10 self.fig = fig 11 self.ax = self.fig.axes[0] 12 self.fig.canvas.mpl_connect('button_press_event', self.on_press) 13 14 def on_press(self, event): 15 x, y = event.xdata, event.ydata 16 if event.button == 1: # left click 17 self.add_object(x, y) 18 elif event.button == 3: # right click 19 self.remove_object() 20 # update legend 21 n = len(self.ax.lines) 22 leg = self.ax.legend( 23 [f'オブジェクト数 {n}'], handletextpad=-2.0, loc='upper right') 24 if n: leg.legendHandles[0].set_visible(False) 25 self.fig.canvas.draw() 26 27 def add_object(self, x, y): 28 self.ax.plot([x], [y], markersize=12, 29 color=next(self.colors), marker=next(self.markers)) 30 31 def remove_object(self): 32 if self.ax.lines: 33 self.ax.lines[-1].remove() 34 35if __name__ == '__main__': 36 fig, ax = plt.subplots() 37 ax.set_xlim(0, 1) 38 ax.set_ylim(0, 1) 39 40 _ = PlaceObjects(fig) 41 42 plt.show()

投稿2021/12/10 00:52

melian

総合スコア19618

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

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

AN3000

2021/12/11 01:52

参考サイトまで提示してくださり、ありがとうございます!無事に自身のコードに組み込み、動作確認できました。グーグル翻訳などを使ってみていこうと思います!ありがとうございました!!
guest

0

python

1import matplotlib.pyplot as plt 2plt.rcParams['font.family'] = 'MS Gothic' 3markers = list('ov^<>12348sp*hH+xD') 4colors = list('bgrcmyk') 5ln = [] # artist記憶用 6 7def motion(event): 8 global i, j, cnt 9 if event.button == 3: # 右クリック->1つ前を削除 10 if len(ln) > 0: 11 ln[-1].pop(0).remove() 12 ln.pop() 13 cnt -= 1 14 i, j = (i - 1) % len(markers), (j - 1) % len(colors) 15 if len(ln) == 0: # 座標数が0になったら凡例非表示 16 ax.get_legend().remove() 17 else: 18 leg = ax.legend([f'クリック回数 {cnt}'], markerscale=0, handletextpad=-2.0, loc='upper right') 19 leg.legendHandles[0].set_visible(False) 20 fig.canvas.draw() 21 else: # それ以外->登録 22 x, y = event.xdata, event.ydata 23 ln.append(ax.plot([x], [y], color=colors[j], marker=markers[i], markersize=12)) 24 i, j = (i + 1) % len(markers), (j + 1) % len(colors) 25 cnt += 1 26 leg = ax.legend([f'クリック回数 {cnt}'], markerscale=0, handletextpad=-2.0, loc='upper right') 27 leg.legendHandles[0].set_visible(False) 28 fig.canvas.draw() 29 30fig, ax = plt.subplots() 31ax.set_xlim(0, 1) 32ax.set_ylim(0, 1) 33i = j = cnt = 0 34fig.canvas.mpl_connect('button_press_event', motion) 35fig.show()

こんな感じでしょうか?
button_press_event のうち、event.button == 3(=右クリック)の時のみ最新の座標点を削除するようにしてあります。
また、補足になりますが、plt.subplots()で figure と axes を作成していながら plt 操作でしたので、figure と axes での操作をメインにしておきました。

投稿2021/12/10 00:45

HRCo4

総合スコア140

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

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

AN3000

2021/12/11 01:49

補足と解説までしてくださりありがとうございます。ベストアンサーは別の方を選ばせていただきましたが、すごくわかりやすく感じました。ほんとうにありがとうございました。
guest

0

ベストアンサー

KB 操作で戻すでよろしければ

Python

1import matplotlib.pyplot as plt 2plt.rcParams['font.family'] = 'MS Gothic' 3markers = list('ov^<>12348sp*hH+xD') 4colors = list('bgrcmyk') 5dots = [] 6def motion(event): 7 global i, j, cnt 8 x, y = event.xdata, event.ydata 9 dot, = plt.plot([x], [y], color=colors[j], marker=markers[i], markersize=12) 10 dots.append(dot) 11 i, j = (i + 1) % len(markers), (j + 1) % len(colors) 12 cnt += 1 13 leg = plt.legend([f'クリック回数 {cnt}'], markerscale=0, handletextpad=-2.0, loc='upper right') 14 leg.legendHandles[0].set_visible(False) 15 plt.draw() 16 17def key_press(event): 18 global cnt 19 if dots and cnt > 1: 20 dots.pop().remove() 21 cnt -= 1 22 leg = plt.legend([f'クリック回数 {cnt}'], markerscale=0, handletextpad=-2.0, loc='upper right') 23 leg.legendHandles[0].set_visible(False) 24 plt.draw() 25 26fig, ax = plt.subplots() 27ax.set_xlim(0, 1) 28ax.set_ylim(0, 1) 29i = j = cnt = 0 30plt.connect('button_press_event', motion) 31plt.connect('key_press_event', key_press) 32plt.show()

右クリック版追加しておきます。

Python

1import matplotlib.pyplot as plt 2plt.rcParams['font.family'] = 'MS Gothic' 3markers = list('ov^<>12348sp*hH+xD') 4colors = list('bgrcmyk') 5dots = [] 6def motion(event): 7 global i, j, cnt 8 if event.button == 3: # 右クリック 9 if dots and cnt > 0: 10 dots.pop().remove() 11 cnt -= 1 12 else: 13 x, y = event.xdata, event.ydata 14 dot, = plt.plot([x], [y], color=colors[j], marker=markers[i], markersize=12) 15 dots.append(dot) 16 i, j = (i + 1) % len(markers), (j + 1) % len(colors) 17 cnt += 1 18 leg = plt.legend([f'クリック回数 {cnt}'], markerscale=0, handletextpad=-2.0, loc='upper right') 19 if leg.legendHandles: 20 leg.legendHandles[0].set_visible(False) 21 plt.draw() 22 23plt.xlim(0, 1) 24plt.ylim(0, 1) 25i = j = cnt = 0 26plt.connect('button_press_event', motion) 27plt.show()

投稿2021/12/10 00:39

編集2021/12/10 02:16
lehshell

総合スコア1147

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

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

AN3000

2021/12/11 01:43

kb操作とクリック操作両方教えていただきありがとうございます。無事、自身のコードに組み込むことができました!本当にありがとうございます!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問