1. 程式人生 > >matplotlib繪圖系列----3D曲面圖與散點圖

matplotlib繪圖系列----3D曲面圖與散點圖

使用mpl_toolkits.mplot3d下Axes3D繪製3D圖形

Axes3D:可以在2D matplotlib圖形上繪製3D物件

1.曲面圖

plot_surface()–建立一個曲面圖。引數:
這裡寫圖片描述

我們先看引數:畫三維圖形需要三個座標 xyz
rstride: Array row stride (step size) row步長
cstride: Array column stride (step size) column 步長
cmap:A colormap for the surface patches. 簡單的說就是曲面圖的配色

程式碼:

#-*- coding: utf-8 -*-

#加入中文顯示
import  matplotlib.font_manager as fm
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D  #繪製三D圖形

fig=plt.figure() #建立一張圖片
ax3d=Axes3D(fig)
#三個軸  x y z
x=np.arange(-2,2.0,0.1)
y=np.arange(-2,2.0,0.1)
#z  隨機生成
def f(x,y):
    return
(1-y**5+x**5)*np.exp(-x**2-y**2) x,y=np.meshgrid(x,y) ax3d.plot_surface(x,y,f(x,y),rstride=1,cstride=1,cmap=plt.cm.spring)#cmap還可以是summer autumn winter也可以自己配置 #rstride Array row stride (step size) 步長 #cstride Array column stride (step size) plt.show()

這裡寫圖片描述

2.散點圖(scatter)

#-*- coding: utf-8 -*-
#加入中文顯示 import matplotlib.font_manager as fm import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D fig=plt.figure() x1=np.random.randint(30,100,100) y1=np.random.randint(30,100,100) z1=np.random.randint(30,100,100) x2=np.random.randint(30,100,100) y2=np.random.randint(30,100,100) z2=np.random.randint(30,100,100) x3=np.random.randint(30,100,100) y3=np.random.randint(30,100,100) z3=np.random.randint(30,100,100) ax3d=Axes3D(fig) #繪製3D圖形 ax3d.scatter(x1,y1,z1,c='r',marker=".") ax3d.scatter(x2,y2,z2,c='b',marker="*") ax3d.scatter(x3,y3,z3,c='r',marker='v') #markes 標記的形狀 http://matplotlib.org/api/markers_api.html #設定軸座標label ax3d.set_xlabel('武力') ax3d.set_ylabel('智慧') ax3d.set_zlabel('防禦') #未解決matplotlib中文亂碼的同學記得先解決亂碼問題。 plt.show()

結果:

這裡寫圖片描述

部分markes對應字元:

這裡寫圖片描述