在matplotlib中扩展线段 [英] Extending a line segment in matplotlib
问题描述
matplotlib
中是否有类似于 MATLAB 的 扩展?
Is there a function in matplotlib
similar to MATLAB's Extensions?
我基本上是在寻找一种将线段扩展到绘图的方法.我目前的情节是这样的.
I am basically looking for a way to extend a line segment to a plot. My current plot looks like this.
在查看了另一个问题并应用了公式后,我可以将其显示到此处,但是它看起来仍然很凌乱.
After looking at another question and applying the formula, I was able to get it to here, but it still looks messy.
有人在这里有魔术公式吗?
Does anyone have the magic formula here?
推荐答案
尝试自己编写,因为我认为 matplotlib 中不存在这种情况.这是一个开始,您可以通过添加半无限等来改进
Have a go to write your own as I don't think this exists in matplotlib. This is a start, you could improve by adding the semiinfinite etc
import matplotlib.pylab as plt
import numpy as np
def extended(ax, x, y, **args):
xlim = ax.get_xlim()
ylim = ax.get_ylim()
x_ext = np.linspace(xlim[0], xlim[1], 100)
p = np.polyfit(x, y , deg=1)
y_ext = np.poly1d(p)(x_ext)
ax.plot(x_ext, y_ext, **args)
ax.set_xlim(xlim)
ax.set_ylim(ylim)
return ax
ax = plt.subplot(111)
ax.scatter(np.linspace(0, 1, 100), np.random.random(100))
x_short = np.linspace(0.2, 0.7)
y_short = 0.2* x_short
ax = extended(ax, x_short, y_short, color="r", lw=2, label="extended")
ax.plot(x_short, y_short, color="g", lw=4, label="short")
ax.legend()
plt.show()
我刚刚意识到您的绘图上有一些红点,这些重要吗?无论如何,我认为到目前为止您缺少的解决方案的主要要点是将绘图限制设置为之前存在的限制,如您所知,它们得到了扩展.
I just realised you have some red dots on your plots, are those important? Anyway the main point I think you solution so far is missing is to set the plot limits to those that existed before otherwise, as you have found, they get extended.
这篇关于在matplotlib中扩展线段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!