gpt4 book ai didi

python - Matplotlib - 使用 ma.masked.where 方法连接线

转载 作者:太空宇宙 更新时间:2023-11-04 05:14:43 24 4
gpt4 key购买 nike

所以我使用 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')

我得到以下结果:enter image description here那么连接线条的聪明方法是什么,所以有一条连续的线会改变它的颜色?

最佳答案

查看您的 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')

enter image description here

关于python - Matplotlib - 使用 ma.masked.where 方法连接线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42065174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com