用python 画3d 图举例

用python 画3d 图举例

文本如下

C 400 200 100 400 300 200
C 400 300 0 400 400 100
C 400 300 100 400 400 200
P 0 100 0 100 0 0
P 0 400 0 100 300 0
P 300 300 200 400 400 200

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt


import numpy as np


def max(a, b):
    if(a < b):
        return b
    else:
        return a


def min(a, b):
    if(a < b):
        return a
    else:
        return b


def showbn(c1, c2, c3, file, type, ax0, color='grey'):
    # ax0 = fig.gca(projection='3d')
    lines = []
    with open(file, 'r') as f:
        for line in f.readlines():
            data = line.split()
            lines.append(data)
    f.close()
    for i in range(len(lines)):
        mcolor = color
        if(len(lines[i]) < 14):
            continue
        if(lines[i][0] != type):
            continue
        x1 = min(float(lines[i][2]), float(lines[i][8]))
        x2 = max(float(lines[i][2]), float(lines[i][8]))
        y1 = min(float(lines[i][3]), float(lines[i][9]))
        y2 = max(float(lines[i][3]), float(lines[i][9]))
        z1 = min(float(lines[i][4]), float(lines[i][10]))
        z2 = max(float(lines[i][4]), float(lines[i][10]))
        if (x1 == x2):
            yy = [y1, y2]
            zz = [z1, z2]
            Y, Z = np.meshgrid(yy, zz)
            X = Z * 0 + x1
            if(c1 == 1):
                ax0.plot_surface(X, Y, Z, color=mcolor)
        if(y1 == y2):
            xx = [x1, x2]
            zz = [z1, z2]
            X, Z = np.meshgrid(xx, zz)
            # print(y1)
            Y = X*0+y1
            if(c2 == 1):
                ax0.plot_surface(X, Y, Z, color=mcolor)
        if (z1 == z2):
            xx = [x1, x2]
            yy = [y1, y2]
            X, Y = np.meshgrid(xx, yy)
            Z = X * 0 + z1
            if(c3 == 1):
                ax0.plot_surface(X, Y, Z, color=mcolor)
    return


if __name__ == "__main__":
    plot_file = "/home/wsk/myfile/dataset/resdata/geor.txt"
    fig = plt.figure()
    ax0 = fig.add_subplot(111, projection='3d')
    showbn(1, 1, 1, plot_file, "conduct", ax0, "grey")
    showbn(1, 1, 1, plot_file, "port", ax0, "red")
    plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值