matplotlib 填充两个x值之间的曲线下面积[重复]

zysjyyx4  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(105)

此问题已在此处有答案

fill_between with matplotlib and a where condition of two lists(1个答案)
Matplotlib fill_between edge effect with where argument(1个答案)
Fill between x and baseline x position in Matplotlib(1个答案)
How to avoid gaps with matplotlib.fill_between and where(1个答案)
关闭1年前。
我正在绘制一条黑体曲线,并想在曲线下填充3到5微米的区域。但是,我不确定如何在这里使用fill_betweenfill_betweenxplt命令

import numpy as np
import matplotlib.pyplot as plt

from astropy import units as u
from astropy.modeling import models
from astropy.modeling.models import BlackBody
from astropy.visualization import quantity_support

bb = BlackBody(temperature=308.15*u.K)
wav = np.arange(1.0, 50.0) * u.micron
flux = bb(wav)

with quantity_support():
    plt.figure()
    plt.plot(wav, flux, lw=4.0)
    plt.fill_between(wav,flux, min(flux), color = 'red')
    plt.show()

这将在整个曲线下绘制填充,但仅需要填充3- 5微米的部分。x1c 0d1x

qpgpyjmq

qpgpyjmq1#

举例说明:

import numpy as np                                                                
import matplotlib.pyplot as plt                                                   
x = np.linspace(0, 2, 100)  # Sample data.                                        
                                                                                  
# Note that even in the OO-style, we use `.pyplot.figure` to create the figure.   
fig, ax = plt.subplots()  # Create a figure and an axes.                          
print(x)                                                                          
ax.plot(x, x, label='linear')  # Plot some data on the axes.                      
ax.set_xlabel('x label')  # Add an x-label to the axes.                           
ax.set_ylabel('y label')  # Add a y-label to the axes.                            
ax.set_title("Simple Plot")  # Add a title to the axes.                           
ax.legend()  # Add a legend.                                                      
plt.fill_between(x[:5],x[:5])                                                     
plt.show()

你可以改变5的值,并与它玩,你会很快理解。第一个参数是Y位置,第二个是X位置。
fill_betweenx也是一样的,但它会以相反的方式填充。
edit:如注解中所述,最好使用plt.fill_between(x,x,where =(x>0)&(x<0.2))。两种方法都有效,第二种方法更显式。

相关问题