简体   繁体   English

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

[英]Matplotlib: fill_between two curves using discrete points

I am trying to recreate the following image: 我正在尝试重新创建以下图像:

在此处输入图片说明

So far I have written this code: 到目前为止,我已经编写了以下代码:

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

Which produces: 产生:

在此处输入图片说明

Now, I want to plot the grid of individual points and color them green or red as in the original image. 现在,我想绘制单个点的网格,并像原始图像一样将它们着色为绿色或红色。 I essentially want to fill_between with discrete points instead of complete shading. 我本质上想用离散点而不是完整的阴影填充。 Is this possible? 这可能吗?

For future reference, I was able to accomplish this with a scatter plot and a meshgrid as: 供以后参考,我能够通过散点图和网格网格实现以下目的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM