【python】pandas & matplotlib 数据处理 绘制曲面图

时间:2024-09-20 20:04:08

Python matplotlib模块,是扩展的MATLAB的一个绘图工具库,它可以绘制各种图形

建议安装 Anaconda后使用 ,集成了很多第三库,基本满足大家的需求,下载地址,对应选择python 2.7 或是 3.5 的就可以了:
https://www.continuum.io/downloads#windows

脚本默认执行方式:
             1.获取当前文件夹下的1.log文件
             2.将数据格式化为矩阵
             3.以矩阵的列索引为x坐标,行索引为y坐标,值为z坐标
             4.绘制曲面图
测试数据
测试所用数据:
r_gain=
79.000000f,  89.000000f, 104.000000f, 120.000000f, 135.000000f,
149.000000f, 160.000000f, 172.000000f, 176.000000f, 172.000000f,
164.000000f, 159.000000f, 143.000000f, 128.000000f, 113.000000f, 
97.000000f,  81.000000f,
r_gain=
84.000000f, 100.000000f, 120.000000f, 136.000000f, 156.000000f,
176.000000f, 192.000000f, 204.000000f, 208.000000f, 204.000000f,
196.000000f, 180.000000f, 164.000000f, 144.000000f, 124.000000f,
108.000000f,  92.000000f,
r_gain=
91.000000f, 112.000000f, 132.000000f, 156.000000f, 176.000000f,
200.000000f, 224.000000f, 240.000000f, 248.000000f, 244.000000f,
228.000000f, 208.000000f, 188.000000f, 164.000000f, 140.000000f,
120.000000f,  99.000000f,
r_gain=
99.000000f, 120.000000f, 144.000000f, 172.000000f, 200.000000f,
228.000000f, 256.000000f, 276.000000f, 284.000000f, 280.000000f,
264.000000f, 240.000000f, 208.000000f, 180.000000f, 156.000000f,
132.000000f, 105.000000f,
r_gain=107.000000f,
128.000000f, 156.000000f, 184.000000f, 216.000000f, 256.000000f,
288.000000f, 308.000000f, 320.000000f, 316.000000f, 296.000000f,
264.000000f, 228.000000f, 196.000000f, 164.000000f, 140.000000f,
113.000000f,
r_gain=111.000000f,
132.000000f, 160.000000f, 192.000000f, 232.000000f, 272.000000f,
304.000000f, 332.000000f, 340.000000f, 336.000000f, 316.000000f,
284.000000f, 244.000000f, 204.000000f, 172.000000f, 144.000000f,
117.000000f,
r_gain=109.000000f,
136.000000f, 164.000000f, 196.000000f, 232.000000f, 276.000000f,
312.000000f, 336.000000f, 348.000000f, 344.000000f, 320.000000f,
288.000000f, 248.000000f, 208.000000f, 172.000000f, 144.000000f,
117.000000f,
r_gain=111.000000f,
132.000000f, 160.000000f, 192.000000f, 228.000000f, 268.000000f,
304.000000f, 328.000000f, 340.000000f, 332.000000f, 312.000000f,
280.000000f, 240.000000f, 200.000000f, 168.000000f, 140.000000f,
119.000000f,
r_gain=101.000000f,
128.000000f, 152.000000f, 180.000000f, 212.000000f, 248.000000f,
280.000000f, 304.000000f, 312.000000f, 308.000000f, 288.000000f,
260.000000f, 224.000000f, 192.000000f, 160.000000f, 136.000000f,
109.000000f,
r_gain=
95.000000f, 116.000000f, 140.000000f, 164.000000f, 192.000000f,
224.000000f, 248.000000f, 272.000000f, 280.000000f, 272.000000f,
256.000000f, 232.000000f, 200.000000f, 176.000000f, 152.000000f,
128.000000f, 101.000000f,
r_gain=
87.000000f, 108.000000f, 128.000000f, 148.000000f, 172.000000f,
192.000000f, 216.000000f, 232.000000f, 236.000000f, 232.000000f,
220.000000f, 200.000000f, 180.000000f, 156.000000f, 136.000000f,
116.000000f,  95.000000f,
r_gain=
80.000000f,  96.000000f, 112.000000f, 132.000000f, 148.000000f,
168.000000f, 180.000000f, 192.000000f, 196.000000f, 196.000000f,
184.000000f, 172.000000f, 156.000000f, 136.000000f, 120.000000f,
104.000000f,  88.000000f,
r_gain=
69.000000f,  85.000000f,  96.000000f, 111.000000f, 127.000000f,
141.000000f, 153.000000f, 160.000000f, 164.000000f, 159.000000f,
157.000000f, 145.000000f, 135.000000f, 120.000000f, 104.000000f, 
88.000000f,  77.000000f,

【python】pandas & matplotlib 数据处理 绘制曲面图

曲面图脚本
# -*- coding: utf-8 -*-
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from pandas import DataFrame
def draw(x, y, z):
    '''
    采用matplolib绘制曲面图
    :param x: x轴坐标数组
    :param y: y轴坐标数组
    :param z: z轴坐标数组
    :return:
    '''
    X = x
    Y = y
    Z = z
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_trisurf(X, Y, Z)
    plt.show()
if __name__ == '__main__':
    '''
       默认执行方式:
             1.获取当前文件夹下的1.log文件
             2.将数据格式化为矩阵
             3.以矩阵的列索引为x坐标,行索引为y坐标,值为z坐标
             4.绘制曲面图
    '''
    data = {}
    index_origin = 0
    f = open("1.log")
    line = f.readline()
    while line:
        data[index_origin] = line.split('=')[-1].replace(' ', '').split('f,')[0:-1]
        index_origin = index_origin + 1
        line = f.readline()
    f.close()
    df = DataFrame(data)
    df = df.T
    x = []
    for i in range(len(df.index)):
        x = x + list(df.columns)
    print(x)
    y = []
    for i in range(len(df.index)):
        for m in range(17):
            y.append(i)
    print(y)
    z = []
    for i in range(len(df.index)):
        z = z + df[i:i + 1].values.tolist()[0]
    z = map(float, z)
    print (z)
    draw(x, y, z)