2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python matplotlibでグラフを作る(超初心者向け)-3(グラフの座標値を取得)

Last updated at Posted at 2018-05-21
実行した環境

Ubuntu Stdio 18.04LTS
Python 3.6.5

グラフ上をマウスを動かすと、右下のx,y座標値が表示されるのは知っていましたが、座標値を取得できるようなのでやってみました。

最初はエラーで、グラフは表示され”+”マークも表示されるのですが、エラーが出て座標値は表示されませんでした。下記参照。
matplotlibでグラフ上で座標値を取得する

Python Project さん
座標値取得(ginput)
http://seesaawiki.jp/python-project/d/%ba%c2%c9%b8%c3%cd%bc%e8%c6%c0%28ginput%29

サンプルプログラム

# !/usr/bin/python3
# coding: UTF-8

# http://seesaawiki.jp/python-project/d/%ba%c2%c9%b8%c3%cd%bc%e8%c6%c0%28ginput%29

import matplotlib.pyplot as plt

x=[0,1,2,3,4,5,6,7,8,9,10]
y=[1,2,3,4,5,6,7,8,9,10,11]
plt.plot(x,y)
a=plt.ginput(n=1)[0]
print (a)
plt.show()

実行してグラフ上をクリックすると、赤い”+”マークが表示され、座標値が表示される。

端末の座標値とグラフの座標値が違いますが、別々でスクショをやっているので違っています。

image.png

エラーの情報をネットで解決策を探していましたが、見つかりません。
他のサイトのサンプルプログラムもエラーで動作しません。

$ ./グラフ上の座標を取得する1.py
Traceback (most recent call last):
  File "/home/ty/.local/lib/python3.6/site-packages/matplotlib/blocking_input.py", line 115, in __call__
    self.fig.canvas.start_event_loop(timeout=timeout)
  File "/home/ty/.local/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 2460, in start_event_loop
    self.flush_events()
  File "/home/ty/.local/lib/python3.6/site-packages/matplotlib/backends/_backend_tk.py", line 452, in flush_events
    self._master.update()
  File "/usr/lib/python3.6/tkinter/__init__.py", line 1174, in update
    self.tk.call('update')
_tkinter.TclError: can't invoke "update" command: application has been destroyed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ty/.local/lib/python3.6/site-packages/matplotlib/backends/tkagg.py", line 26, in blit
    dataptr, colormode, bboxptr)
_tkinter.TclError: this isn't a Tk application

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./グラフ上の座標を取得する1.py", line 8, in <module>
    a=plt.ginput(n=3)[0]
  File "/home/ty/.local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 725, in ginput
    return gcf().ginput(*args, **kwargs)
  File "/home/ty/.local/lib/python3.6/site-packages/matplotlib/figure.py", line 2153, in ginput
    show_clicks=show_clicks)
  File "/home/ty/.local/lib/python3.6/site-packages/matplotlib/blocking_input.py", line 291, in __call__
    BlockingInput.__call__(self, n=n, timeout=timeout)
  File "/home/ty/.local/lib/python3.6/site-packages/matplotlib/blocking_input.py", line 118, in __call__
    self.cleanup()
  File "/home/ty/.local/lib/python3.6/site-packages/matplotlib/blocking_input.py", line 278, in cleanup
    self.fig.canvas.draw()
  File "/home/ty/.local/lib/python3.6/site-packages/matplotlib/backends/backend_tkagg.py", line 13, in draw
    tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2)
  File "/home/ty/.local/lib/python3.6/site-packages/matplotlib/backends/tkagg.py", line 34, in blit
    dataptr, colormode, bboxptr)
_tkinter.TclError: this isn't a Tk application

ところが、海外の質問サイトのコードを実行すると、ちゃんと実行されるのです。
https://stackoverflow.com/questions/24002000/python-ginput-not-allowing-new-points-to-be-plotted

# !/usr/bin/python3
# coding: UTF-8

import time
import numpy as np
import matplotlib.pyplot as plt

def tellme(s):
    print(s)
    plt.title(s,fontsize=16, **font)
    plt.draw()

font = {'family':'IPAGothic'} #日本語Fontを指定

##################################################
# 3点をクリックして三角形を定義する
##################################################
plt.clf()
plt.axis([-1.,1.,-1.,1.])
plt.setp(plt.gca(),autoscale_on=False)

tellme('あなたは三角形を定義し、クリックして開始します')

plt.waitforbuttonpress()

happy = False
while not happy:
    pts = []
    while len(pts) < 3:
        tellme('マウスで3つのコーナーを選択')
        pts = np.asarray( plt.ginput(3,timeout=-1) )
        if len(pts) < 3:
            tellme('開始点が少なすぎる')
            time.sleep(1) # Wait a second

    ph = plt.fill( pts[:,0], pts[:,1], 'r', lw=2 )

    tellme('ハッピー? キーをクリックするとはい、マウスをクリックしていいえ')

    happy = plt.waitforbuttonpress()

    # Get rid of fill
    if not happy:
        for p in ph: p.remove()

試しに、先程のコードを実行してもエラーが出なくなりました。
原因不明????

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?