2

所以我使用 numpy.ma.masked 方法在特定条件下绘制线条,但我想连接所有连续的线条。例如,使用以下代码:

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

我得到以下结果:在此处输入图像描述 那么连接线的聪明方法是什么,所以有一条连续的线会改变它的颜色?

4

1 回答 1

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.1,而不是0.10101。您还需要_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')

在此处输入图像描述

于 2017-02-06T11:59:46.063 回答