Python之Numpy:二元函数绘制/三维数据可视化/3D
生活随笔
收集整理的這篇文章主要介紹了
Python之Numpy:二元函数绘制/三维数据可视化/3D
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
意義
在機器學習任務(wù)中選擇計算模型或者學習數(shù)學時,可視化有助于研究函數(shù)值的變化趨勢(觀察收斂、分布、幾何形狀等),帶來直觀的感受。
源碼
# 繪制二元函數(shù)
# 參考文獻
# + python畫二元函數(shù)的圖像(3D) https://blog.csdn.net/your_answer/article/details/79135076
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from matplotlib import pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
# x=np.arange(-2*np.pi,2*np.pi,0.1) # np.range(startValue,endValue, stepSize)
# y=np.arange(-2*np.pi,2*np.pi,0.1)
# x = np.random.rand(100) # np.random.rand(4) # 生成一維數(shù)組 形如: array([ 0.69804514, 0.48808425, 0.79440667, 0.66959075]);
# y = np.random.rand(100)
# x = np.arange(1,100,1) # np.random.rand(4) # 生成一維數(shù)組 形如: array([ 0.69804514, 0.48808425, 0.79440667, 0.66959075]);
# y = np.arange(1,100,1)
x = np.random.randint(100,size=100) # np.random.randint(20,size=10) 形如: array([4, 1, 4, 3, 8, 2, 8, 5, 8, 19])
y = np.random.randint(100,size=100)
X, Y = np.meshgrid(x, y) # [important] 創(chuàng)建網(wǎng)格 np.meshgrid(xnums,ynums)
# Z = np.sin(X)*np.cos(Y) # 創(chuàng)建二元函數(shù)關(guān)系
Z = 1 / (np.log(X)*np.log(Y));
plt.xlabel('x')
plt.ylabel('y')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow')
plt.show()
繪制曲線圖/一元函數(shù)
示例一
# 繪制曲線圖
import matplotlib.pyplot as plt
def plotLineChart():
fig = plt.figure()
ax = fig.add_subplot(1,1,1) # numrows, numcols, fignum ; fignum標識了該子圖的順序,其范圍從1到numrows*numcols
ax.set_title("axes title");
ax.set_xlabel("x label")
ax.set_ylabel("y label")
ax.plot([1,2,3,4],[2,3,4,5])
plt.show()
pass;
plotDemo();
示例二(進階)
# encoding=utf-8
import matplotlib.pyplot as plt
from pylab import * #支持中文
mpl.rcParams['font.sans-serif'] = ['SimHei']
names = ['5', '10', '15', '20', '25']
x = range(len(names))
y1 = [0.855, 0.84, 0.835, 0.815, 0.81]
y2=[0.86,0.85,0.853,0.849,0.83]
#plt.plot(x, y1, 'ro-')
#plt.plot(x, y2, 'bo-')
#pl.xlim(-1, 11) # 限定橫軸的范圍
#pl.ylim(-1, 110) # 限定縱軸的范圍
plt.plot(x, y1, marker='o', mec='r', mfc='w',label=u'y=x^2曲線圖')
plt.plot(x, y2, marker='*', ms=10,label=u'y=x^3曲線圖')
plt.legend() # 讓圖例生效
plt.xticks(x, names, rotation=45)
plt.margins(0)
plt.subplots_adjust(bottom=0.15)
plt.xlabel(u"time(s)鄰居") #X軸標簽
plt.ylabel("RMSE") #Y軸標簽
plt.title("A simple plot") #標題
plt.show()
參考文獻
python畫二元函數(shù)的圖像(3D
推薦工具
在線繪制一元函數(shù)圖像(Web) : 1
在線繪制一元函數(shù)圖像(Web) : 2
總結(jié)
以上是生活随笔為你收集整理的Python之Numpy:二元函数绘制/三维数据可视化/3D的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 元梦之星家园装修技巧与方法
- 下一篇: 《斗罗大陆》武魂觉醒向阳而生第三章详解