2013-10-11 154 views
2

matplotlib中是否有類似於MATLAB的Extensions的函數?在matplotlib中擴展線段

我基本上尋找一種方法來將線段延伸到一個圖。我目前的情節是這樣的。

enter image description here

看着另一個問題和應用公式後,我能得到它在這裏,但它仍然看起來凌亂。

enter image description here

有誰有神奇的公式在這裏?

+0

請在downvoting之前發表評論。這個問題對社區沒有任何用處? – user2869668

+0

請定義凌亂 – gg349

回答

1

有一個去寫自己的,因爲我不認爲這存在於matplotlib。這是一個開始,你可以通過添加半無限等

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() 

enter image description here

我才意識到你有你的陰謀一些紅點,是那些重要的改進?無論如何,我認爲你迄今爲止解決問題的主要觀點是將繪圖限制設置爲以前存在的繪圖限制,正如你發現的那樣,它們會得到擴展。