如何在兩條曲線之間上色?

時間 2021-06-09 06:54:58

1樓:

看了一下上面的答案,這裡補充一下Python的matplotlib模組中「fill_between」不但可以實現曲線之間的色彩填充,還能夠直接指定在哪些地方進行填充。

樓上關於「fill_between( )」的運用是預先設定「boolean」下標來指定具體在哪些地方填充,實際上「fill_between( )」本身設定了「where」引數來實現該功能。

其具體呼叫格式為:

fill_between(x

,y1,y2

=0,where

=None

,interpolate

=False

,hold

=None,**

kwargs

)Make

filled

polygons

between

twocurves

.Call

signature

::fill_between(x

,y1,y2

=0,where

=None,**

kwargs

)

下面簡單看下在sin(x)與cos(x)之間位於cos(x) > sin(x)處的填充例項:

from

matplotlib

import

pyplot

asplt

import

numpy

asnp

# 匯入相應模組x=

np.linspace(-

4*np.

pi,4*

np.pi,

1024)y1

=np.sin(x

)y2=np

.cos(x

)# 簡單畫出兩條曲線

plt.

plot(x

,y1,'g-'

,linewidth

=2.0

,label

='$sin(x)$'

)plt

.plot(x

,y2,'b-'

,linewidth

=2.0

,label

='$cos(x)$'

)# 這裡不需要預先利用boolean下標來設定填充區域,而直接通過y2>y1等價實現

plt.

fill_between(x

,y2,y1

,y2>y1,

color

='#ff0000'

,alpha

=0.3

)# 對圖表進行簡單修飾

plt.

xlim(-

4*np.

pi,4*

np.pi)

plt.

xticks(np

.arange(-

4*np.

pi,5*

np.pi,

np.pi),['$

%d\pi$'%n

forn

inrange(-

4,5)])

plt.

ylim(-

2.0,

2.0)

plt.

legend

()plt

.grid

(True

)plt

.show()

2樓:

這種效果嗎?

d1 = Import["D:/data.txt", "Table"];

d2 = d1amp; /@ , };

ListLinePlot[d2, Filling -> }, Frame -> 1]

ListLinePlot[d2, Frame -> 1, Filling -> }]

3樓:

如果用 Python 的話。用 fill_between 函式。

下邊是我寫的乙個例子,有點長,不過其實重點只有 fill_between 一句而已,其他都是些為了讓例子跑起來的輔助語句。

from

matplotlib

import

collections

ascoll

,pyplot

asplt

import

numpy

asnp

# 構建四個函式

deffun1(x

):return(2

*x-x

**2)**

0.5def

fun2(x

):return1-

(2*x

-x**2

)**0.5def

fun3(x

):return

(1.0+(

1-4*

x*x)

**0.5)/

2def

fun4(x

):return

(1.0-(

1-4*

x*x)

**0.5)/

2# 生成離散的 x 座標從 0~1.01 精度 0.002x=np.

arange(0

,1.01

,0.002

)# 取得離散的函式值y1=

fun1(x

)y2=fun2(x

)y3=fun3(x

)y4=fun4(x

)# 畫出曲線

plt.

plot(x

,y1,alpha

=0.8

)plt

.plot(x

,y2,alpha

=0.8

)plt

.plot(x

,y3,alpha

=0.8

)plt

.plot(x

,y4,alpha

=0.8

)# 根據大致的函式圖象取出要填充部分的最大值和最小值。

y_up=np

.minimum(y1

,y3)y_down=np

.maximum(y2

,y4)# 取出要填充部分的 x 座標(即就是所有 y_up > y_down 處的 x 座標,下同)x=

x[y_up

>y_down]yu

=y_up

[y_up

>y_down]yd

=y_down

[y_up

>y_down

]# 值得注意的是此處 x 和 yu, yd 都是長度一致的陣列,每次取對應處的值

# 填充 x 處的從 yu 到 yd 的部分.

plt.

fill_between(x

,yu,yd

,alpha

=0.3

)# 顯示出來

plt.

axis

('scaled'

)plt

.show()

4樓:曹洪洋

Mathematica中用Filling命令就行了x=

Range[0

,2Pi,

0.1];y=

;ListPlot[y

,Filling

->},Joined

->#] & /@ ;%//

Column

陰影填充,要改變細節可以調整MeshFunctions和MeshStyle,如MeshFunctions -> 為水平陰影線

data = Table[, }, ];

plt = ListLinePlot[Transpose@data, Filling -> }];

poly = Cases[Normal@plt, Polygon[p_] :> p, -1];

(* 適用Mathematica10之前的版本 *)

RegionPlot[Graphics`Mesh`InPolygonQ[#, ] & /@ poly // Evaluate,

, , Mesh -> 50, PlotPoints -> 50,

MeshFunctions -> , MeshShading -> ,

BoundaryStyle -> Black] // Quiet

(* 適用Mathematica10及之後的版本 *)

RegionPlot[True, ∈ Polygon[poly], Mesh -> 50,

PlotPoints -> 50, MeshFunctions -> , MeshShading -> ,

BoundaryStyle -> Black, AspectRatio -> Automatic]

如何判斷兩條軌跡(或曲線)的相似度?

coding utf 8 import cv2def orb detect image a image b feature match orb cv2.ORB create kp1 des1 orb detectAndCompute image a None kp2 des2 orb detectA...

如何在這兩條人生路中做選擇?

威廉姆Z 如果家裡沒有很大的負擔的話,自己對自己有信心的話,年輕的時候可以膽子大一點,拼一拼試試,不行再回來。我覺得也是有機會的。 Lee 我不知道這個假定如何而來。北上廣年薪25w,也需要努力,不是都可以達到。二線老家25w年薪,我還真就沒聽說過有誰30歲能達到。對前途滿意那就不換,25w年薪二線...

能否用二次曲線系證明,過極點的兩條割線與曲線交得的四個點組成的四邊形對角線在極點對應的極線上?

Sliark 可以 以下使用齊次座標,並用線性代數符號縮寫過程 設過點 的直線 與二次曲線 交於四個點 交於點 設 方程為 則 退化二次曲線 和 的標準方程可寫作 和 在 和 組成的二次曲線系上,故 的方程可表示為 而 在 關於 的極線上 這件事等價於 這是顯然的 用線性代數符號使得這個問題變得過於...