matplotlib绘制三维图

matplotlib绘制三维图

之前学可视化都是画的二维图,对三维图不太熟悉,这次尝试了一下

三维柱状图

首先学习的是这样一段代码和图

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#构造需要显示的值
X=np.arange(0, 5, step=1)#X轴的坐标
Y=np.arange(0, 9, step=1)#Y轴的坐标
#设置每一个(X,Y)坐标所对应的Z轴的值,在这边Z(X,Y)=X+Y
Z=np.zeros(shape=(5, 9))
for i in range(5):
    for j in range(9):
        Z[i, j]=i+j
xx, yy=np.meshgrid(X, Y)#网格化坐标
X, Y=xx.ravel(), yy.ravel()#矩阵扁平化
bottom=np.zeros_like(X)#设置柱状图的底端位值
Z=Z.ravel()#扁平化矩阵
width=height=1#每一个柱子的长和宽
#绘图设置
fig=plt.figure()
ax=fig.gca(projection='3d')#三维坐标轴
ax.bar3d(X, Y, bottom, width, height, Z, shade=True)#
#坐标轴设置
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z(value)')
plt.show()

matplotlib绘制三维图_第1张图片

可以看到他的Z轴数据是根据x和y轴数据得到的,我的工作就是x,y,z都换成了自己的数据,

并且我发现他们的柱子挤在一起不好看,也做了调整,如下:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#构造需要显示的值
X=np.arange(0, 7)#X轴的坐标
Y=np.arange(0, 4)#Y轴的坐标
Z1 = [[77.80,78.68,78.91,78.08,76.88,77.90,77.67 ],
     [77.80,78.68,78.91,78.08,76.88,77.90,77.67 ],
     [55.31,54.42,54.62,55.90,54.71,54.76,56.99 ],
     [64.64,64.93,66.27,66.81,65.47,67.44,69.07 ]    
]
#设置每一个(X,Y)坐标所对应的Z轴的值,在这边Z(X,Y)=X+Y
Z=np.zeros(shape=(7, 4))
for i in range(7):
    for j in range(4):
        Z[i,j] = Z1[j][i]
xx, yy=np.meshgrid(X, Y)#网格化坐标
X, Y=xx.ravel(), yy.ravel()#矩阵扁平化
bottom=np.zeros_like(X)#设置柱状图的底端位值
Z=Z.ravel()#扁平化矩阵
width=height=0.5#每一个柱子的长和宽
#绘图设置
fig=plt.figure()
ax=fig.gca(projection='3d')#三维坐标轴
ax.bar3d(X, Y, bottom, width, height, Z, shade=True,color=[0.33, 0.8, 0])#
#坐标轴设置
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z(value)')
plt.savefig('三维柱状图.jpg',dpi=1000)
plt.show()

matplotlib绘制三维图_第2张图片

但是唯一没解决的问题就是我想把这些柱子根据x轴的不同数据换成不同的颜色,如果有大神看到的话麻烦指点一下,非常感谢。

三维面图

学习的代码如下:

fig = plt.figure()  #定义新的三维坐标轴
ax3 = plt.axes(projection='3d')

#定义三维数据
xx = np.arange(-5,5,0.5)
yy = np.arange(-5,5,0.5)
X, Y = np.meshgrid(xx, yy)
Z = np.sin(X)+np.cos(Y)


#作图
ax3.plot_surface(X,Y,Z,cmap='rainbow')
#ax3.contour(X,Y,Z, zdim='z',offset=-2,cmap='rainbow)   #等高线图,要设置offset,为Z的最小值
plt.show()

matplotlib绘制三维图_第3张图片

换成自己的数据,修改如下:

fig = plt.figure()  #定义新的三维坐标轴
ax = plt.axes(projection='3d')

#定义三维数据
xx = np.arange(0, 7)
yy = np.arange(0, 4)
X, Y = np.meshgrid(xx, yy)
Z = np.array([[76.08,76.21,76.41,75.97,76.31,75.12,76.29 ],
     [77.80,78.68,78.91,78.08,76.88,77.90,77.67 ],
     [55.31,54.42,54.62,55.90,54.71,54.76,56.99 ],
     [64.64,64.93,66.27,66.81,65.47,67.44,69.07 ]])

#作图
ax.plot_surface(X,Y,Z,cmap='rainbow')
#分别上下旋转和左右旋转,可以自己设置成一个比较好的参数
ax.view_init(32, -32)
ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))

ax.contour(X,Y,Z, zdim='z',offset=30,cmap='rainbow')   #等高线图,要设置offset,为Z的最小值
plt.savefig('一周速度对比图.jpg',dpi=1000)
plt.show()

matplotlib绘制三维图_第4张图片

这个图遇到了一点问题,对于c的值不能直接写成列表,而是一个数组的形式,z = np.array([])

三维折线图

学习代码如下:

import numpy as np
import matplotlib.pyplot as plt

xs1 = np.ones(100)
ys1 = range(1, 101)
zs1 = np.zeros(100)

xs2 = np.ones(100) * 2
ys2 = range(1, 101)
zs2 = np.zeros(100)

xs3 = np.ones(100) * 3
ys3 = range(1, 101)
zs3 = np.zeros(100)
for i in range(21, 100):
  zs3[i] = -0.22

xs4 = np.ones(100) * 4
ys4 = range(1, 101)
zs4 = np.zeros(100)


# Plot
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
ax.plot(xs1, ys1, zs1, lw=2, color=[0.8, 0.33, 0])
ax.plot(xs2, ys2, zs2, lw=2, color=[0.33, 0.8, 0])
ax.plot(xs3, ys3, zs3, lw=2, color=[0, 0.5, 0.8])
ax.plot(xs4, ys4, zs4, lw=2, color=[0.8, 0.1, 0.5])
# ax.set_xlabel("X Axis")
# ax.set_ylabel("Y Axis")
# ax.set_zlabel("Z Axis")
# ax.set_title("Addective fault simulation")
plt.show()

matplotlib绘制三维图_第5张图片

换成自己的数据,修改如下:

import numpy as np
import matplotlib.pyplot as plt

xs1 = np.ones(24)
ys1 = range(1, 25)
zs1 = [78.28844534,83.08954433,81.34141666,80.52949257,85.21059924,80.4210032,81.49604266,77.150026,76.66013341,74.73200146,
       75.54207697,75.68582114,78.8503177,78.48700652,75.72040315,74.25533112,74.84000417,75.11228724,76.25884751,75.59676757,
       74.69122662,74.26263233,75.52238936,75.14358652]

xs2 = np.ones(24) * 2
ys2 = range(1, 25)
zs2 = [74.65020224,74.99389778,77.18669297,75.24653501,81.1249982,83.45131933,83.96719497,80.56359718,82.13180768,76.41013756,
       76.24368237,77.91918117,80.45393422,79.69904768,78.14059035,77.998017,77.45999249,76.2818803,76.64198104,76.22702139,
       75.86752834,77.16244126,75.0044124,76.34511916]

xs3 = np.ones(24) * 3
ys3 = range(1, 25)
zs3 = [79.96670262,83.75592043,82.78849088,84.03825874,85.90907644,73.28937891,55.70676235,69.57809216,68.68209425,
       51.67844727,48.85665881,50.16694097,50.78942338,51.80394098,47.96089747,44.66588706,45.25316517,43.67684213,
       43.21163936,44.98838566,51.97327685,61.32649666,66.005481,71.96841873]

xs4 = np.ones(24) * 4
ys4 = range(1, 25)
zs4 = [80.08812356,82.80684877,84.33708995,84.67549651,85.97903999,81.94341439,59.02903656,72.48107544,64.26525477,56.01675237,
       64.16629988,65.26833575,69.55879166,66.73558971,51.12279321,53.33410069,60.42553748,57.71003758,62.29596499,68.43479318,
       71.11060658,73.34943319,73.52011429,78.55941717
]


# Plot
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
ax.plot(xs1, ys1, zs1, lw=2, color=[0.8, 0.33, 0])
ax.plot(xs2, ys2, zs2, lw=2, color=[0.33, 0.8, 0])
ax.plot(xs3, ys3, zs3, lw=2, color=[0, 0.5, 0.8])
ax.plot(xs4, ys4, zs4, lw=2, color=[0.8, 0.1, 0.5])
ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))

# ax.set_xlabel("X Axis")
# ax.set_ylabel("Y Axis")
# ax.set_zlabel("Z Axis")
# ax.set_title("Addective fault simulation")
plt.savefig('一天24h速度变化图.jpg')
plt.show()

matplotlib绘制三维图_第6张图片

你可能感兴趣的:(python)