matplotlib 使用pyplot的着色函数,Python中的fill_between()

6mw9ycah  于 2023-05-01  发布在  Python
关注(0)|答案(1)|浏览(140)

我想要一个阴影图,但我不知道怎么做

plt.rcParams["figure.figsize"] = [7.50, 3.50]
 plt.rcParams["figure.autolayout"] = True

 def g(x):
     return np.sqrt(x/4) 

 def f(x):
    return 0.5
 
  x = np.linspace(0, 1.0, 1000)
  y = np.linspace(0, 0.5, 1000)
  plt.xlim([0, 1.08])
  plt.ylim([0, 0.55])
  plt.plot(x, g(x),  color='blue')
  
  plt.fill_between(x, g(x), color='green', step="pre", alpha=0.9)
  plt.fill_between(x,f(x), color='pink', step="pre", alpha=0.6)

  plt.xlabel('$\omega_q N$',  fontsize=14, weight='bold') 
  plt.ylabel('$g$', fontsize=14, weight='bold') 

  plt.show()

我得到这张照片

但我想要这个

x8goxv8g

x8goxv8g1#

您可以使用更新后的代码创建您提到的绘图。有多处变更,请参阅变更备注。..您可以调整颜色或线条大小等。让它变成你想要的样子

plt.rcParams["figure.figsize"] = [7.50, 4.50]  ## Increased hieght a little
plt.rcParams["figure.autolayout"] = True

def g(x):
     return np.sqrt(x/4) 

def f(x):
    return 0.5

x = np.linspace(0, 1.0, 1000)
y = np.linspace(0, 0.5, 1000)
plt.xlim([0, 1.08])
plt.ylim([0, 0.55])
plt.plot(x, g(x),  color='blue')

#Color the orange section - note alpha=1
plt.fill_between(x, g(x), color='#AEDBAE', step="pre", alpha=1)
#Color the green section - note alpha=1
plt.fill_between(x,g(x),f(x), color='#F18860', step="pre", alpha=1)

#Draw blue horizontal lines - solid and dashes
plt.axhline(y=0.5, xmin=0, xmax=1/1.08, linewidth=2, color='blue')
plt.axhline(y=0.5, xmin=1/1.08, xmax=1, linewidth=2, color='blue', ls='--')

#Draw grey dotted vertical lines
plt.axvline(x=1, ymin=0, ymax=0.5/0.55, linewidth=1.5, color='darkgray', ls=':')
#Draw the single gray dot where the lines intersect -> zorder is to make sure the gray dot is on top
plt.scatter(1, 0.5, color="darkgray", s=50, zorder=10) 

import matplotlib.patches as patches
#Add the slanted black lines - called hatches
p = patches.Rectangle((0,0.5), 1.08, 0.05, linewidth=0, fill=None, hatch='/')
plt.gca().add_patch(p)

#Add the fading / gradient green to white on the right end of the plot from 1.0 to 1.1
from matplotlib import cm
from matplotlib.colors import LinearSegmentedColormap
cm = LinearSegmentedColormap.from_list("Custom", ["#AEDBAE","#FFFFFF"], N=20)
mat = np.indices((10,10))[1]
plt.imshow(mat, cmap = cm, extent=[1, 1.1, 0, 0.5], alpha = 1, aspect = "auto")

#Remove plot top line (despine) 
plt.gca().spines['top'].set_visible(False)

#Add text in each section
plt.text(0.2, 0.3, 'Superradiant \n Phase', ha='center', fontsize=20, color='white')
plt.text(0.6, 0.15, 'Normal \n phase', ha='center', fontsize=16, color='black')
plt.text(0.5, 0.55, 'Unbound region', ha='center', fontsize=22, color='black')

plt.xlabel('$\omega_q N$',  fontsize=14, weight='bold') 
plt.ylabel('$g$', fontsize=14, weight='bold') 
plt.show()

相关问题