Python學習筆記(matplotlib實戰篇)--函式積分圖
Python學習筆記--極座標
參靠視訊:《 Python資料視覺化分析 matplotlib教程》連結: ofollow,noindex">https://www.bilibili.com/video/av6989413/?p=6
所用的庫及環境:
IDE:Pycharm
Python環境:python3.7
Matplotlib: Matplotlib 1.11
Numpy: Numpy1.15
函式積分圖
- 程式碼及效果圖
1 import matplotlib.pyplot as plt 2 import numpy as np 3 from matplotlib.patches import Polygon 4 5 def func(x): 6return -(x-2)*(x-8)+40 7 8 x = np.linspace(0,10) 9 y = func(x) 10 11 fig,axes = plt.subplots() 12 #繪製曲線 13 plt.plot(x,y,'r',linewidth = 2) 14 a=2 15 b=9 16 17 #座標軸設定 18 axes.set_xticks([a,b]) 19 axes.set_xticklabels(['$a$','$b$']) 20 axes.set_yticks([]) 21 plt.figtext(0.9,0.05,'$x$') 22 plt.figtext(0.1,0.9,'$y$') 23 24 #繪製灰色多邊形 25 ix=np.linspace(a,b) 26 iy=func(ix) 27 ixy = zip(ix,iy) 28 verts=[(a,0)]+list(ixy)+[(b,0)] 29 poly = Polygon(verts,facecolor='0.9',edgecolor='0.5') 30 axes.add_patch(poly) 31 32 #新增數學公式 33 x_math =(a+b)*0.5*0.8 34 y_math = 35 35 plt.text(x_math,y_math,'$\int_a^b(-(x-2)*(x-8)+40)dx$',fontsize=10,horizontalalignment='center') 36 plt.show()
- 相關函式介紹
- linspace:在指定的間隔內返回均勻間隔的數字
- 屬性
- start:序列的起始值
- stop :序列的結束值
- 其他屬性詳見文件: https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html?highlight=linspace#numpy.linspace
- 屬性
- set_xticks:使用刻度列表設定x刻度
- set_xticklabels:使用字串標籤列表設定x-tick標籤。
- set_yticks:使用刻度列表設定y刻度
- 屬性
- ticks:y軸刻度列表
- 更多屬性參加文件: https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_yticks.html?highlight=set_ytick
- 屬性
- figtext:新增文字到圖
- 屬性
- x,y:兩個float值,放置文字的位置。預設情況下,這是圖形座標,浮動在[0,1]中。最右是1最左是0
- 更多屬性詳見文件: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.figtext.html?highlight=figtext#matplotlib.pyplot.figtext
- 屬性
- zip:把兩個陣列打包為一個元組
- 屬性
- iterabl :一個或多個迭代器
- 詳見資料: http://www.runoob.com/python/python-func-zip.html
- 屬性
- text:請參照往期筆記 https://www.cnblogs.com/linblogs/p/9670488.html
- 屬性
- horizontalalignment:文字顯示位置,center是居中顯示
- 屬性
- Polygon:繪製一般的多邊形
- 屬性
- xy:多邊形的點
- facecolor:填充的陰影深度
- edgecolor:填充的邊界深度
- 屬性
- 其他函式請參考往期學習筆記,或參考matplotlib文件 https://matplotlib.org/index.html
- linspace:在指定的間隔內返回均勻間隔的數字
三.結語:
感謝matplotlib,numply提供的文件,感謝麥子學院提供的視訊教學
文章如哪裡有誤請聯絡作者QQ406802063,及時更正,感謝