python plot 畫圓

通過plot畫圓的各種智障錯誤和方法

1.如果有圓外線需要設定兩個x

x = np.linspace(-10, 10.0, 100)
x1 = np.linspace(-2, 2, 100)
y1 = b + np.sqrt(r**2 - (x1 - a)**2)

這個是圓

y2 = -2x + 10
y3 = 1 + (x - 1) ** 2
y4 = -2x1 + 10
之所以要在設定一條線是因爲在圓內的x1自變量範圍裏,
後面用fill_between的時候
如果將y1和y2進行填充會因爲自變量範圍不一樣造成奇怪的錯誤,
所以這裏用一條y4將圓內自變量範圍裏的線填充一下

這個是其他的線

# Make plot
plt.plot(x1, y1, label=r'$x_{1}^{2}+x_{2}^{2} \geq 4$')
plt.plot(x1, -y1, label=r'$x_{1}^{2}+x_{2}^{2} \geq 4$')
plt.plot(x, y2, label=r'$2 x_{1}+x_{2} \leq 10$')
# plt.plot(x, y3, label=r'$ -x_{1}-x_{2} \leq -10x_{2}$')
plt.vlines(1, -10, 10, color="red")  # 豎線

plt.xlim((-10, 10.000))
plt.ylim((-10, 10.000))
plt.xlabel(r'$x1$')
plt.ylabel(r'$x2$')

這裏注意要兩條線,y1和-y1不然圓只有正半個

# plt.fill_between(x, y2, y3, where=x <= -2, color='grey', alpha=0.5)
# plt.fill_between(x1, y4, y1, where=(y4>y1), color='grey', alpha=0.5)
# plt.fill_between(x, y2, y3, where=(x >= 2) & (x <= 90/19), color='grey', alpha=0.5)

 


plt.grid(True, linestyle='-.')
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

# plt.savefig('ordering_constraints.eps',dpi=600,bbox_inches='tight')
plt.show()

這樣問題就解決了

 

畫無限大上限的函數陰影時候使用

plt.fill_between(X,1,Y1,color='blue',alpha=.25) # 這裏的1是圖紙的上限就可以

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章