Read the coordinates of the plot on the graph with Python-matplotlib (super beginner)

Introduction

Last time, I introduced a program up to "reading the coordinates of an image", but this time, based on that program, I will read the numerical values on the graph (convert the coordinates of the image to the numerical values on the graph). I tried it.

The input accuracy of the coordinate axes is low, and it is only an approximate value, so it is not a substitute that can be used in practice, but it seems that it has become a substitute that you can imagine "I want to do this kind of thing".

As mentioned above, I think the challenges are to improve the input accuracy of the coordinate axes of the graph and to improve the accuracy when clicking points. From here onward it seems to be quite difficult.

Conversion formula to the value on the graph

x=x_{min}+(x_{max}-x_{min})\cdot \cfrac{k_{xx}-k_{xi}}{k_{xf}-k_{xi}}
y=y_{min}-(y_{max}-y_{min})\cdot \cfrac{k_{yy}-k_{yf}}{k_{yf}-k_{yi}}

program

py_img_test1.py


from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
import warnings;warnings.filterwarnings('ignore')

im=Image.open('fig_PSHQ1.png')
im_list=np.asarray(im)
plt.imshow(im_list)

#click left-top and right-bottom
nn=2
a=plt.ginput(n=nn)
xi=a[0][0]
yi=a[0][1]
xf=a[1][0]
yf=a[1][1]
del a

#click on the graph
nn=5
a=plt.ginput(n=nn)
b=np.array(a)
vec_x=b[:,0]
vec_y=b[:,1]
plt.plot( vec_x,vec_y, 'ro' )

# range of the graph
#(xmin,xmax)=(0,12000)
#(ymin,ymax)=(60,90)

xmin,xmax=map(float,input('xmin xmax= ').split())
ymin,ymax=map(float,input('ymin ymax= ').split())

# calculation of the values
xx=xmin+(xmax-xmin)*(vec_x-xi)/(xf-xi)
yy=ymin-(ymax-ymin)*(vec_y-yf)/(yf-yi)
for x,y in zip(xx,yy):
    print(x,y)

plt.savefig('fig_test.png', bbox_inches="tight", pad_inches=0.2)
plt.show()

How to use

  1. An image is displayed when the program is started
  2. Click the upper left and lower right of the graph. As a result, $ (k_ {xi}, k_ {yi}) $ and $ (k_ {xf}, k_ {yf}) $ are input and defined.
  3. Click any 5 points
  4. You will be asked for the minimum and maximum of the x-axis on the console, so enter it. As a result, $ x_ {min} $ and $ x_ {max} $ are input and defined.
  5. You will be asked for the minimum and maximum of the y-axis on the console, so enter it. As a result, $ y_ {min} $ and $ y_ {max} $ are input and defined.
  6. The values on the graph of the 5 clicked points are displayed.
  7. Close the display and exit

Console input / output

$ python3 py_img_test1.py
xmin xmax= 0 12000
ymin ymax= 60 90
-31.25 62.1212121212
1937.5 70.202020202
3968.75 74.0404040404
6000.0 77.1717171717
9968.75 82.2222222222

Output image

fig_test.png

that's all

Recommended Posts

Read the coordinates of the plot on the graph with Python-matplotlib (super beginner)
Read the graph image with OpenCV and get the coordinates of the final point of the graph
Read image coordinates with Python-matplotlib
Read the csv file with jupyter notebook and write the graph on top of it
Display the graph of tensorBoard on jupyter
Increase the font size of the graph with matplotlib
The basis of graph theory with matplotlib animation
Coordinates of the right end of Label made with tkinter
Play with the UI implementation of Pythonista3 [Super Super Introduction]
I want to plot the location information of GTFS Realtime on Jupyter! (With balloon)
Drawing on Jupyter using the plot function of pandas
Read the power of the smart meter with M5StickC (BP35C0-J11-T01)
[In 3 lines] Plot the population pyramid (bar graph of age group / gender) with Pandas alone
How to plot a lot of legends by changing the color of the graph continuously with matplotlib
read the tag assigned to you on ec2 with boto3
Let's execute the command on time with the bot of discord
Read the GRIB2 file of the Japan Meteorological Agency with pygrib
Reformat the timeline of the pandas time series plot with matplotlib
[Python] Calculate the angle consisting of three points on the coordinates
Until the start of the django tutorial with pycharm on Windows
Get the host name of the host PC with Docker on Linux
Connected components of the graph
Interactive plot of 3D graph
[Statistics] Grasp the image of the central limit theorem with a graph
I studied with Kaggle Start Book on the subject of kaggle [Part 1]
Count the maximum concatenated part of a random graph with NetworkX
I checked the image of Science University on Twitter with Word2Vec.
A super beginner who does not know the basics of Python tried to graph the stock price of GAFA
Plot the spread of the new coronavirus
Output the call graph with PyCallGraph
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
Read a Python # .txt file for a super beginner in Python with a working .py
[Image recognition] How to read the result of automatic annotation with VoTT
Edit the file of the SSH connection destination server on the server with VS Code
Play the comment of Nico Nico Douga on the terminal in conjunction with the video
I tried with the top 100 PyPI packages> I tried to graph the packages installed on Python
You can see the transition of points in the J League on the graph!
Read the file with python and delete the line breaks [Notes on reading the file]
Prepare the environment of Chainer on EC2 spot instance with AWS Lambda
The story of trying to push SSH_AUTH_SOCK obsolete on screen with LD_PRELOAD