程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python 3D graph -- specify data sequence

編輯:Python

Use python While processing data , It is often necessary to draw three-dimensional drawings . The following is a record of the process of drawing a three-dimensional diagram with a given data list .

  1. First, the large frame of 3D drawing :
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Define the axis 
fig = plt.figure()
ax = plt.axes(projection='3d')
# ax = fig.add_subplot(111,projection='3d') # Draw multiple subgraphs 
ax.plot_surface(xx, yy, zz, rstride=1, cstride=1, cmap='rainbow')
# ax.scatter3D(xx,yy,zz,cmap='red') # Scatter plot 
# ax.plot3D(xx,yy,zz,cmap='red') 
plt.show() # draw 

Among them xx,yy,zz It needs to be dealt with .


2. Data processing

Given three lists . First the x and y An array of axes

x=np.arange(1,4,1) # Generate a numpy Array , Range [1,4), In steps of 1
y = [10,11,12,13] # python list 
xx, yy = np.meshgrid(x, y) # take x,y Array extension 
# xx = [[1 2 3]
# [1 2 3]
# [1 2 3]
# [1 2 3] ]
# yy = [[10 10 10]
# [11 11 11]
# [12 12 12]
# [13 13 13]]
z = [[10,20,30], # Array z Should also be 4*3 Array of 
[11,22,33],
[12,24,36],
[13,26,39]]
zz = np.array(z) # python The two-dimensional array is converted to numpy Array 
# One x And a y Corresponding to one z, namely xx The first line in and yy The first line in corresponds to zz The first line in 

  1. Complete code
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Define the axis 
fig = plt.figure()
ax = plt.axes(projection='3d')
x=np.arange(1,4,1)
y = [10,11,12,13]
xx, yy = np.meshgrid(x, y)
z = [[10,20,30],
[11,22,33],
[12,24,36],
[13,26,39]]
zz = np.array(z)
ax.plot_surface(xx, yy, zz, rstride=1, cstride=1, cmap='rainbow')
plt.show()

  1. Result chart

Last figure , Because the data changes proportionally , So the picture is z The plane is a straight line .

  1. Add

if necessary x If the axis is divided into scales according to the given data , stay ax.plot_surface() Followed by plt.xticks(list), In parentheses are list types .

For example, add a sentence plt.xticks([1,2,3]), The result is as follows :


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved