填充两个x值之间的曲线下方的区域[重复]

问题描述 投票:0回答:1

我正在绘制一条黑体曲线,并希望填充曲线下方 3 到 5 微米范围内的区域。但是,我不确定如何在此处使用

fill_between
fill_betweenx
plt
命令

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 微米部分。

python matplotlib fill curve
1个回答
0
投票

示例:

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 是一样的,但它会以相反的方式填充。

编辑:正如评论中所说,最好使用 plt.fill_ Between(x,x, where = (x>0)&(x<0.2)). Both works, second solution is more explicit.

© www.soinside.com 2019 - 2024. All rights reserved.