0

I'm plotting a blackbody curve and would like to fill in the area under the curve in the range of between 3 and 5 micron. However, I'm not sure how to use the fill_between or fill_betweenx plt commands here

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

This plots a fill under the whole curve, but only the 3-5micron part is desired to be filled. enter image description here

npross
  • 1,756
  • 6
  • 19
  • 38
  • 1
    Just fill a curve with x-values (and related y-values) only between 3 and 5 micron; then overplot the full curve. Essentially what you have now, just limit `wav` and `flux` to the relevant section in the `fill_between` part. – 9769953 Dec 08 '21 at 15:37
  • `plt.fill_between(wav,flux, min(flux), where=(wav>=3)&(wav<=5),color = 'blue')`. – JohanC Dec 08 '21 at 15:56

1 Answers1

0

example:

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

                                                                 
                                                                         
            

You can change the value 5 and play with it, you'll understand quickly. first parameter is Y positions , second is X positions.

fill_betweenx is just the same, but it will fill the other way around.

edit: As said in comments, it is better to use plt.fill_between(x,x, where = (x>0)&(x<0.2)). Both works, second solution is more explicit.

LittlePanic404
  • 130
  • 1
  • 1
  • 7