Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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:可以从一组轴到另一组轴绘制直线吗?_Python_Matplotlib_Plot - Fatal编程技术网

Python Matplotlib:可以从一组轴到另一组轴绘制直线吗?

Python Matplotlib:可以从一组轴到另一组轴绘制直线吗?,python,matplotlib,plot,Python,Matplotlib,Plot,我希望在[100100]处绘制标记“x”,然后在[20%,30%]处绘制“o”(不同的轴,相同的图),并用一条线连接它们。我可以在相同的轴上做类似的事情(使用相同的单位),一次调用绘制直线,另一次调用绘制“x”,最后一次调用绘制“o” 然而,我如何才能使线从一组轴转到另一组轴?我不确定我是否完全理解了你的问题,但把这些放在一起,看看这是否是你想要的 import pylab #generate array of data for example import numpy as np x = n

我希望在[100100]处绘制标记“x”,然后在[20%,30%]处绘制“o”(不同的轴,相同的图),并用一条线连接它们。我可以在相同的轴上做类似的事情(使用相同的单位),一次调用绘制直线,另一次调用绘制“x”,最后一次调用绘制“o”


然而,我如何才能使线从一组轴转到另一组轴?

我不确定我是否完全理解了你的问题,但把这些放在一起,看看这是否是你想要的

import pylab

#generate array of data for example
import numpy as np
x = np.arange(1,250,1)
y = np.arange(1,250,1)

#find marker for your 'x' points
x_marker_location = 100
x_marker_x = x[np.where(x==x_marker_location)]  # np.where looks for location in your data where array equals a value. Alternatively, x_marker_x and y would just be a coordinate value.
x_marker_y = y[np.where(y==x_marker_location)]

#create scaling factors
o_marker_scale_x = 0.2
o_marker_scale_y = 0.3
#find marker for your 'o' points
o_marker_x = x[np.where(x==x_marker_location*o_marker_scale_x)]
o_marker_y = y[np.where(y==x_marker_location*o_marker_scale_y)]

#draw line of all data
pylab.plot(x,y,"-",color='black')
#draw points interested in
pylab.scatter(x_marker_x, x_marker_y, marker='x')
pylab.scatter(o_marker_x, o_marker_y, marker='o')
#draw connecting line - answer to question?
pylab.plot([x_marker_x,o_marker_x],[x_marker_y,o_marker_y],'-',color='red')

#show plot
pylab.show()

我不确定我是否完全理解了你的问题,但是把这些放在一起看看这是否是你想要的

import pylab

#generate array of data for example
import numpy as np
x = np.arange(1,250,1)
y = np.arange(1,250,1)

#find marker for your 'x' points
x_marker_location = 100
x_marker_x = x[np.where(x==x_marker_location)]  # np.where looks for location in your data where array equals a value. Alternatively, x_marker_x and y would just be a coordinate value.
x_marker_y = y[np.where(y==x_marker_location)]

#create scaling factors
o_marker_scale_x = 0.2
o_marker_scale_y = 0.3
#find marker for your 'o' points
o_marker_x = x[np.where(x==x_marker_location*o_marker_scale_x)]
o_marker_y = y[np.where(y==x_marker_location*o_marker_scale_y)]

#draw line of all data
pylab.plot(x,y,"-",color='black')
#draw points interested in
pylab.scatter(x_marker_x, x_marker_y, marker='x')
pylab.scatter(o_marker_x, o_marker_y, marker='o')
#draw connecting line - answer to question?
pylab.plot([x_marker_x,o_marker_x],[x_marker_y,o_marker_y],'-',color='red')

#show plot
pylab.show()

您可以使用
annotate
绘制单线:

ax1 = plt.subplot(121)
ax2 = plt.subplot(122)

x = [[1, 2], [3,4]]
y = [[5, 6], [6,4]]

ax1.scatter(x[0], y[0])
ax2.scatter(x[1], y[1])


ax1.annotate('', xy=(x[0][0], y[0][0]), xytext=(x[1][0], y[1][0]), xycoords=ax1.transData, 
         textcoords=ax2.transData, 
         arrowprops=dict(facecolor='black', arrowstyle='-',, clip_on=False))
ax2.annotate('', xy=(x[0][0], y[0][0]), xytext=(x[1][0], y[1][0]), xycoords=ax1.transData, 
         textcoords=ax2.transData, 
         arrowprops=dict(facecolor='black', arrowstyle='-'))
产生这个结果的原因是:


您可以使用
注释
绘制单线:

ax1 = plt.subplot(121)
ax2 = plt.subplot(122)

x = [[1, 2], [3,4]]
y = [[5, 6], [6,4]]

ax1.scatter(x[0], y[0])
ax2.scatter(x[1], y[1])


ax1.annotate('', xy=(x[0][0], y[0][0]), xytext=(x[1][0], y[1][0]), xycoords=ax1.transData, 
         textcoords=ax2.transData, 
         arrowprops=dict(facecolor='black', arrowstyle='-',, clip_on=False))
ax2.annotate('', xy=(x[0][0], y[0][0]), xytext=(x[1][0], y[1][0]), xycoords=ax1.transData, 
         textcoords=ax2.transData, 
         arrowprops=dict(facecolor='black', arrowstyle='-'))
产生这个结果的原因是:


您应该包含更多的代码,因为很难准确理解您的意思。如果没有更多的上下文,我们可能只能猜测您是如何创建轴的。您应该包含更多的代码,因为很难准确理解您的意思。如果没有更多的上下文,我们可能只能猜测您是如何创建轴的。