2013-05-11 2 views
1

내 플롯 아래 공간을 채우려 고 시도합니다. 플롯은 y = x이므로 45도 각도의 직선입니다. 나는 x = 1에서 x = 10까지 커브 아래의 영역을 채우려고 시도한다. fill_between를 사용하여 어떻게 할 것인가?matplotlib fill_between 인수 x와 함께

답변

2

키워드의 의미는 where입니다. 예를 들어

where : If None, default to fill between everywhere. If not None, it is an N-length numpy boolean array and the fill will only happen over the regions where where==True.

:

>>> import numpy as np 
>>> import matplotlib.pyplot as plt 
>>> fig, ax = plt.subplots() 
>>> x = np.linspace(0, 10, 50) 
>>> y = x**2 
>>> ax.plot(x, y, 'r-') 
[<matplotlib.lines.Line2D object at 0x1e91250>] 
>>> wh = (x>1) & (x<10)  
>>> ax.fill_between(x, y, where=wh, alpha=0.2) 
<matplotlib.collections.PolyCollection object at 0x24dd210> 
>>> plt.show() 
+0

내 경우에는 내가 목록을 사용하지만, 그 차이는 없습니다. ValueError ("인수 차원이 호환되지 않습니다")를 발생시킵니다. x와 y는 동일한 dimmension이므로 plot을 볼 수 있습니다./ – nykon

+1

@nykon 실제로 변수 x에 대한 목록을 사용하면 차이가 있습니다. 'wh = (x> 1) & (x <10)'을 사용할 때'x'는'numpy.ndarray'이고,'wh'는'bool'의 numpy.ndarray입니다. 'x'가'list' 일 때,'wh'는 하나의'bool'을 할당받습니다. @Zhenya가 제공 한 답에서 알 수 있듯이'where'는'None' 또는 부울'numpy.ndarray' 만 인수로 취합니다. 하나의'bool'을 인수로 전달하면 ValueError가 발생합니다. – hooy

+0

그래, 내가이 부분에 대해 생각하지 않은 점이다. 고맙습니다 :) – nykon

관련 문제