Python matplotlib畫曲線例題解析
這篇文章主要介紹了Python matplotlib畫曲線例題解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
初學者,練習以下片段:
代碼1:用 一元一次函數(shù) 畫直線
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-2, 2, 50)
print(x)
y = 2*x + 1
plt.plot(x, y)
ax = plt.gca()
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
plt.show()

代碼2:用 一元二次函數(shù) 畫拋物線
import matplotlib.pyplot as plt import numpy as np x = np.linspace(-4, 6, 50) print(type(x)) print(x) y = x**2 - x*2+1 plt.plot(x, y) plt.show()

代碼3:用反比例函數(shù) 畫曲線
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-11, 11, 110)
print(x)
y =10/x
plt.plot(x, y)
ax = plt.gca()
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
plt.show()

代碼4:子坐標系的應(yīng)用
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(224)
ax1.set(xlim=[0.5, 4.5], ylim=[-2, 8], title='Axes Show',
ylabel='Y', xlabel='X')
plt.show()

代碼5:子坐標系的應(yīng)用
import matplotlib.pyplot as plt
fig = plt.figure()
fig,axes=plt.subplots(nrows=2, ncols=2)
axes[0,0].set(xlim=[0.5, 4.5], ylim=[-2, 8], title='Axes Show',
ylabel='Y', xlabel='X')
axes[0,1].set(title='Upper Right')
axes[1,0].set(title='Lower Left')
axes[1,1].set(title='Lower Right')
代碼6:用已知少量坐標值畫曲線
import matplotlib.pyplot as plt plt.plot([1, 2, 3, 4], [10, 20, 25, 30], color='lightblue', linewidth=3) plt.xlim(0.5, 4.5) plt.show()

代碼7:畫離散點
mport matplotlib.pyplot as plt import numpy as np x = np.arange(10) y = np.random.randn(10) print(y) plt.scatter(x, y, color='red', marker='+') plt.show()

代碼8:畫出正弦曲線
import numpy as np
import matplotlib.pyplot as plt
x=np.arange(-2*np.pi,2*np.pi,0.01)
y=np.sin(x)
plt.plot(x,y)
ax = plt.gca()
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
plt.xticks([-np.pi*2,-np.pi*3/2,-np.pi, -np.pi/2, 0, np.pi/2, np.pi,np.pi*3/2,np.pi*2],[r'$-2\pi$',r'$-\frac{3}{2} \pi$',r'$-\pi$',r'$-\frac{1}{2}\pi$', r'0', r'$\frac{1}{2}\pi$','$\pi$',r'$\frac{3}{2}\pi$',r'$2 \pi$'])
plt.show()

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 利用python中的matplotlib打印混淆矩陣實例
- Matplotlib繪制混淆矩陣的實現(xiàn)
- Python使用matplotlib繪制正弦和余弦曲線的方法示例
- Python matplotlib繪制圖形實例(包括點,曲線,注釋和箭頭)
- matplotlib 曲線圖 和 折線圖 plt.plot()實例
- Python matplotlib 繪制雙Y軸曲線圖的示例代碼
- 使用matplotlib動態(tài)刷新指定曲線實例
- Python使用matplotlib繪制三維參數(shù)曲線操作示例
- Python使用matplotlib繪制Logistic曲線操作示例
- matplotlib畫混淆矩陣與正確率曲線的實例代碼
相關(guān)文章
pycharm使用Translation插件實現(xiàn)翻譯功能
PyCharm是一款很流行的Python編輯器,經(jīng)常遇到在PyCharm中把中文翻譯成英文的需求,下面這篇文章主要給大家介紹了關(guān)于pycharm使用Translation插件實現(xiàn)翻譯功能的相關(guān)資料,需要的朋友可以參考下2023-05-05
Django中QuerySet查詢優(yōu)化之prefetch_related詳解
prefetch_related()和select_related()的設(shè)計目的很相似,都是為了減少SQL查詢的數(shù)量,但是實現(xiàn)的方式不一樣,下面這篇文章主要給大家介紹了關(guān)于Django中QuerySet查詢優(yōu)化之prefetch_related的相關(guān)資料,需要的朋友可以參考下2022-11-11
python 內(nèi)置函數(shù)-range()+zip()+sorted()+map()+reduce()+filte
這篇文章主要介紹了python 內(nèi)置函數(shù)-range()+zip()+sorted()+map()+reduce()+filter(),想具體了解函數(shù)具體用法的小伙伴可以參考一下下面的介紹,希望對你有所幫助2021-12-12
python神經(jīng)網(wǎng)絡(luò)編程之手寫數(shù)字識別
這篇文章主要介紹了python神經(jīng)網(wǎng)絡(luò)編程之手寫數(shù)字識別,文中有非常詳細的代碼示例,對正在學習python神經(jīng)網(wǎng)絡(luò)編程的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05
Python如何利用Har文件進行遍歷指定字典替換提交的數(shù)據(jù)詳解
這篇文章主要給大家介紹了關(guān)于Python如何利用Har文件進行遍歷指定字典替換提交的數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11

