This article is participating in Python Theme Month. See the link to the event for more details

1. The background

When analyzing the data of large projects, common two-dimensional statistical graphs may be difficult to meet the requirements of visual analysis and presentation of multidimensional data. In this case, we may need to conduct 3D visualization to show the relationship between multidimensional data more intuitively.

The data used this time comes from the monitoring data of a bridge, which is an Excel file with four sheets. Different sheets represent different monitoring indicators, and each column in each sheet represents different observation points. It is hoped that the mean data of all observation points can be jointly displayed for subsequent analysis. The original data is available from GitHub: Python_3d

2. Code and ideas

Start by importing the Python library

from xlrd import open_workbook
import pandas as pd
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter,FuncFormatter
Copy the code

2.1 Drawing Settings

Set the coordinates before drawing

X = np.arange(0, 16, step=1) # Y = np.arange(0, 4, step=1) # Y = np.arange(0, 4, step=1) # Y = np.arange(0, 4, step=1) # Y = np.arange(0, 4, step=1) # Y = np.arange(0, 4, step=1) Y = x.ravel(), Y = x.ravel(), Y = x.ravel(), Yy.ravel () # flat matrix bottom = Np.zerOS_like (X) # Set the bottom bit value of the bar chart Z = z.ravel () # Flat matrixCopy the code

In order to match the display of data with the coordinate grid, the size of the graph after data visualization is set

width = height = 1
Copy the code

Finally, set up 3d drawing

Ax = FIG. Gca (X, Y, bottom, width, height, Z); shade=True) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z(mean)') plt.setp(ax.get_xminorticklabels(), visible=False) plt.show()Copy the code

2.2 Data Processing

The process of source data preprocessing is as follows. Since the name of sheet is not required, the method of dynamic cyclic reading sheet can be adopted to calculate the average value of each column and recycle it into the list. The loop assigns the list to the array to get an array of the mean values of the different observation points throughout the Excel table.

Since the number of columns of each sheet may be different, the list with fewer columns is added with 0 to satisfy the composition of the matrix.

XLSX wb = Open_workbook (FileName) sheets = Wb.sheet_names () m = len(sheets) n = pd.read_excel(FileName, sheet_name=sheets[0]).shape[1] - 1 arr = np.empty((m, N)) # Loop all sheets for I in range(len(sheets)): df = pd.read_excel(FileName, sheet_name=sheets[i]) d = df.mean() data = [] for j in range(1, len(d)): data.append(d.iat[j]) le = n while(len(data) < le): data.append(0) arr[i] = dataCopy the code

2.3 the drawing

Abstract the drawing function as a function, passing an array as an argument

Def ZZT (data): X = np.arange(0, 16, step=1) # Y = np.arange(0, 4, step=1) # Y = np.arange(0, 4, step=1) # Y = np.arange(0, 4, step=1) # Y = np.arange(0, 4, step=1) # Y = np.arange(0, 4, step=1) Y = x.ravel(), Y = x.ravel(), Y = x.ravel(), Yy.ravel () # matrix flattening bottom = Np.zerOS_like (X) # Set the bottom bit value of the bar chart Z = z.ravel () # Flattening matrix # length and width of each column = height = 1 # 3D drawing Settings Figure ax = FIG. Gca (X, Y, bottom, width, height, Z) shade=True) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z(mean)') plt.setp(ax.get_xminorticklabels(), visible=False) plt.show()Copy the code

The 3D visualization results are obtained by calling the drawing function

if __name__ == "__main__":
    zzt(arr)
Copy the code

The source code is available from GitHub: Python_3d

3. Result presentation

The results are as follows, and the resulting 3D images can be viewed from any Angle.


4. Summary and impressions

Through learning Python data analysis and visualization, I have a deeper understanding of Python language and data visualization methods. It was a good opportunity for me to learn from the abstraction of drawing functions, the preprocessing of raw data and the setting of coordinate axes. There are so many ways in the world that learning cannot end. In the future, I will further improve my Python skills, laying a solid foundation for professional learning and future application.