简体   繁体   English

从折线图中提取 (x,y) 值

[英]Extract (x,y) values from a line graph

Suppose I have 2 sets of data and I use plt.plot to plot the graph.假设我有 2 组数据,我使用 plt.plot 绘制图形。

import matplotlib.pyplot as plt
import numpy as np

x=range(5,46,5)
y=[1.60,1.56,1.54,1.53,1.53,1.58,1.70,1.97,2.68]


plt.plot(x,y)

How can I get the x,y values of the line graph created by plt.plot?如何获得由 plt.plot 创建的折线图的 x,y 值?

Edit for clarity: What I want to do is getting the coordinates of the line that is created by plt.plot, not getting the data that is used to create the graph.为清楚起见进行编辑:我想要做的是获取由 plt.plot 创建的线的坐标,而不是获取用于创建图形的数据。

Edit for even more clarity: I want to get the coordinates of the points in between each of my (x,y) pairs in the line plotted by pyplot.编辑更清晰:我想获得 pyplot 绘制的线中每个 (x,y) 对之间点的坐标。

You can access the Lines2D object from the axis.您可以从轴访问Lines2D对象。 Then line data can be accessed with the Lines2D.get_data() function returning a tuple of x and y array values.然后可以使用 Lines2D.get_data() 函数访问行数据,返回 x 和 y 数组值的元组。

x=range(5,46,5)
y=[1.60,1.56,1.54,1.53,1.53,1.58,1.70,1.97,2.68]
fig, ax = plt.subplots(figsize=(10,6))
ax.plot(x,y)

ax.lines[0].get_data()

>>> (array([ 5, 10, 15, 20, 25, 30, 35, 40, 45]),
 array([1.6 , 1.56, 1.54, 1.53, 1.53, 1.58, 1.7 , 1.97, 2.68]))

An Axes object has a property transData . Axes对象具有属性transData This transformation can be used to translate data coordinates into display coordinates:转换可用于将数据坐标转换为显示坐标:

x=range(5,46,5)
y=[1.60,1.56,1.54,1.53,1.53,1.58,1.70,1.97,2.68]

plt.gca().set_xlim(5,46)
plt.gca().set_ylim(1.5,3)
plt.plot(x,y)

print(plt.gca().transData.transform(list(zip(x,y))))

The last line prints the array of data points expressed in display coordinates:最后一行打印以显示坐标表示的数据点数组:

[[ 54.          50.496     ]
 [ 94.82926829  44.6976    ]
 [135.65853659  41.7984    ]
 [176.48780488  40.3488    ]
 [217.31707317  40.3488    ]
 [258.14634146  47.5968    ]
 [298.97560976  64.992     ]
 [339.80487805 104.1312    ]
 [380.63414634 207.0528    ]]

This output means that the first datapoint (5, 1.60) is displayed at (54.0, 50.495) and the last datapoint (45, 2.69) is displayed at (380.634, 207.052) .此输出意味着第一个数据点(5, 1.60)显示在(54.0, 50.495) ,最后一个数据点(45, 2.69)显示在(380.634, 207.052)

Edit : The remaining points between two datapoints from this list can be calculated using interp1d :编辑:可以使用interp1d计算此列表中两个数据点之间的剩余点:

display_coords = plt.gca().transData.transform(list(zip(x,y)))
from scipy.interpolate import interp1d
f = interp1d(display_coords[:,0], display_coords[:,1])
display_x = np.linspace(min(display_coords[:,0]), max(display_coords[:,0]), num=100, endpoint=True)
list(zip(display_x, f(display_x)))

Result:结果:

[(54.0, 50.49599999999998),
 (57.29933481152993, 50.0274424242424),
 (60.59866962305987, 49.55888484848483),
 (63.8980044345898, 49.09032727272725),
 (67.19733924611974, 48.62176969696967),
...
 (367.43680709534374, 173.78521212121217),
 (370.7361419068736, 182.102109090909),
 (374.0354767184036, 190.41900606060602),
 (377.3348115299335, 198.735903030303),
 (380.63414634146346, 207.0528)]

The actual values depend on the display, settings etc and might vary.实际值取决于显示、设置等,并且可能会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM