Matplotlib:使用离散点在两条曲线之间填充

时间:2018-03-27 21:10:17

标签: python matplotlib

我正在尝试重新创建以下图片:

enter image description here

到目前为止,我已经编写了这段代码:

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

yLow,yHigh = (-75,200)
xLow,xHigh = (-5.5,2.5)

plt.figure(figsize=(15,10))
ax = plt.gca()

x = np.linspace(xLow,xHigh,1000)
y = x**4
yG = -20*x +20

plt.plot(x,y,linewidth=1,linestyle='--',color='black');
plt.plot(x,yG,linewidth=3,);

plt.xlim(xLow,xHigh);
plt.ylim(yLow,yHigh);
# plt.xticks([], []);
# plt.yticks([], []);

plt.fill_between(x,yG,yHigh,alpha=0.3,color='green');
plt.fill_between(x,yG,yLow,alpha=0.3,color='green');
plt.fill_between(x,y,yG,color='red');

产生:

enter image description here

现在,我想绘制各个点的网格,并将其颜色设置为原始图像中的绿色或红色。我基本上想要用离散点填充而不是完全着色。这可能吗?

1 个答案:

答案 0 :(得分:0)

为了将来参考,我能够使用散点图和meshgrid完成此操作:

X,Y = np.meshgrid(x,np.linspace(yLow,yHigh,numPoints));
Z = ( (Y<X**4) & (Y>-20*X +20) ) | ( (Y>X**4) & (Y<-20*X +20) );
plt.scatter(X,Y,c=Z,cmap=ListedColormap(['green','red']),alpha=0.5);