2017-02-06 37 views
2

所以我使用numpy.ma.masked方法在一定條件下繪製線條,但我想連接所有連續的線。例如,使用此代碼:Matplotlib - 連接使用ma.masked.where方法的線

import pylab as plt 
import numpy as np 
x = np.linspace(0,10,100) 
y = -1.0 + 0.2*x 
plt.plot(x,np.ma.masked_greater_equal(y,0)) 
plt.plot(x,np.ma.masked_less_equal(y,0),'r') 

我得到以下結果:enter image description here 那麼,什麼是連接的線,所以有一個連續的線,其顏色的聰明的方式?

回答

1

看看你的值y。它看起來像這樣:

array([-1.  , -0.97979798, -0.95959596, -0.93939394, -0.91919192, 
     -0.8989899 , -0.87878788, -0.85858586, -0.83838384, -0.81818182, 
     -0.7979798 , -0.77777778, -0.75757576, -0.73737374, -0.71717172, 
     -0.6969697 , -0.67676768, -0.65656566, -0.63636364, -0.61616162, 
     -0.5959596 , -0.57575758, -0.55555556, -0.53535354, -0.51515152, 
     -0.49494949, -0.47474747, -0.45454545, -0.43434343, -0.41414141, 
     -0.39393939, -0.37373737, -0.35353535, -0.33333333, -0.31313131, 
     -0.29292929, -0.27272727, -0.25252525, -0.23232323, -0.21212121, 
     -0.19191919, -0.17171717, -0.15151515, -0.13131313, -0.11111111, 
     -0.09090909, -0.07070707, -0.05050505, -0.03030303, -0.01010101, 
     0.01010101, 0.03030303, 0.05050505, 0.07070707, 0.09090909, 
     0.11111111, 0.13131313, 0.15151515, 0.17171717, 0.19191919, 
     0.21212121, 0.23232323, 0.25252525, 0.27272727, 0.29292929, 
     0.31313131, 0.33333333, 0.35353535, 0.37373737, 0.39393939, 
     0.41414141, 0.43434343, 0.45454545, 0.47474747, 0.49494949, 
     0.51515152, 0.53535354, 0.55555556, 0.57575758, 0.5959596 , 
     0.61616162, 0.63636364, 0.65656566, 0.67676768, 0.6969697 , 
     0.71717172, 0.73737374, 0.75757576, 0.77777778, 0.7979798 , 
     0.81818182, 0.83838384, 0.85858586, 0.87878788, 0.8989899 , 
     0.91919192, 0.93939394, 0.95959596, 0.97979798, 1.  ]) 

你會發現,沒有任何的0.0值,因此兩條線將永遠無法觸摸。

你可以通過添加一個更多的價值,你的x陣列(即101值解決這個問題,所以你必須,而不是0.101010.1的間距,你還需要刪除從口罩_equal,否則他們將永遠不會碰(您目前屏蔽掉在y=0.在這兩種情況下的值)。

import pylab as plt 
import numpy as np 
x = np.linspace(0.,10.,101) 
y = -1.0 + 0.2*x 
plt.plot(x,np.ma.masked_greater(y,0.)) 
plt.plot(x,np.ma.masked_less(y,0.),'r') 

enter image description here

+0

但?在我的情況下,他們是,如果我無法控制,我給出的陣列什麼的計算的輸出不同的工具(不是Python) – Ohm

+2

那麼你將不得不在你的條件中包括一些重疊。例如'masked_greater(y,0.1)'和'masked_less(y,-0.1)' – tom