Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib Twinx()光标值_Python_Python 2.7_Matplotlib_Scipy - Fatal编程技术网

Python Matplotlib Twinx()光标值

Python Matplotlib Twinx()光标值,python,python-2.7,matplotlib,scipy,Python,Python 2.7,Matplotlib,Scipy,我想使用光标(x,y值显示在图的左下角)测量两点之间的y和x距离,但这仅适用于绘制在第二个轴上的数据 有没有办法在第二个轴和第一个y轴之间来回切换 请注意:我不希望使用编程方式获取点之间的距离,只是在查看图形图中的数据时使用光标 不确定这是否有帮助,但我的代码实际上就是从matplotlib页面打印两个轴的示例: fig, ax1 = plt.subplots() ax1.plot(sensor1, 'b-') ax1.set_xlabel('(time)') #

我想使用光标(x,y值显示在图的左下角)测量两点之间的y和x距离,但这仅适用于绘制在第二个轴上的数据

有没有办法在第二个轴和第一个y轴之间来回切换

请注意:我不希望使用编程方式获取点之间的距离,只是在查看图形图中的数据时使用光标

不确定这是否有帮助,但我的代码实际上就是从matplotlib页面打印两个轴的示例:

    fig, ax1 = plt.subplots()
    ax1.plot(sensor1, 'b-')
    ax1.set_xlabel('(time)')
    # Make the y-axis label and tick labels match the line color.
    ax1.set_ylabel('Sensor 1', color='b')
    for tl in ax1.get_yticklabels():
        tl.set_color('b')


    ax2 = ax1.twinx()
    ax2.plot(sensor2, 'r.')
    ax2.set_ylabel('Sensor 2', color='r')
    for tl in ax2.get_yticklabels():
        tl.set_color('r')
    plt.show()

您可以使用最佳答案同时显示两个坐标。为了获得两点之间的距离,您可以将此想法与ginput结合起来,从一个点映射到另一个点,并将结果添加为标题

import matplotlib.pyplot as plt
import numpy as np

#Provide other axis
def get_othercoords(x,y,current,other):

    display_coord = current.transData.transform((x,y))
    inv = other.transData.inverted()
    ax_coord = inv.transform(display_coord)
    return ax_coord

#Plot the data
fig, ax1 = plt.subplots()
t = np.linspace(0,2*np.pi,100)
ax1.plot(t, np.sin(t),'b-')
ax1.set_xlabel('(time)')
ax1.set_ylabel('Sensor 1', color='b')
for tl in ax1.get_yticklabels():
    tl.set_color('b')

ax2 = ax1.twinx()
ax2.plot(t,3.*np.cos(t),'r-')
ax2.set_ylabel('Sensor 2', color='r')
for tl in ax2.get_yticklabels():
    tl.set_color('r')

#Get user input
out = plt.ginput(2)

#2nd axis from input
x2b, x2t = out[0][0], out[1][0]
y2b, y2t = out[0][1], out[1][1]

#Draw line
ax2.plot([x2b, x2t],[y2b, y2t],'k-',lw=3)

#1st axis from transform
x1b, y1b = get_othercoords(x2b,y2b,ax2,ax1)
x1t, y1t = get_othercoords(x2t,y2t,ax2,ax1)
plt.title("Distance x1 = " + str(x1t-x1b) + " y1 = " + str(y1t-y1b) + "\n"
          "Distance x2 = " + str(x2t-x2b) + " y2 = " + str(y2t-y2b))
plt.draw()
plt.show()
这就产生了