繁体   English   中英

3D曲面图genfromtxt

[英]3d surface plot genfromtxt

我在通过读取文本文件来绘制3d表面图时遇到了问题。 问题是,我使用的是不同的数据类型,例如float,int和string。.我已经将代码附加了要解析的示例数据。.我希望对代码进行任何注释以使其能够正常工作...

我现在得到的错误是ValueError:元组的大小必须匹配字段数。

提前thnx。

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from numpy import genfromtxt
fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(111, projection='3d') #ax = Axes3D(fig) 
ax.set_title("Plot 3d",fontsize=14)
ax.set_xlabel('Voltage (V)', fontsize=12)
ax.set_ylabel('Pulse Delay(ms)', fontsize=12)
ax.set_zlabel('Pulse Width(ms)', fontsize=12)
ax.grid(True, linestyle='-', color='0.75')
x,y,z,m =genfromtxt('sample.txt', dtype=[('col1', 'f15'), ('col2', 'i15'), ('col3',   'i15'), ('col4', 'S15')],  unpack=True)

use_colours = []
for tmp in m:
    if tmp=='PASS':
        use_colours.append('g')
    else:
        use_colours.append('r')

ax.scatter(x,y,z, s=50, c=use_colours, marker = 'o', linewidths=0);
plt.show()



sample.txt

6.000000    15.000000   21.000000   PASS
6.000000    15.000000   53.000000   PASS
6.000000    15.000000   91.000000   PASS
6.000000    15.000000   104.000000  PASS

在尝试执行您的代码时,我发现了一些滥用,如果不知道matplotlib库的正确功能,就很难避免。

这是一个有效的注释代码:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
fig = plt.figure(figsize=(12, 12))

ax = Axes3D(fig) # ax = fig.add_subplot(111, projection='3d')
ax.set_title("Plot 3d",fontsize=14)
ax.set_xlabel('Voltage (V)', fontsize=12)
ax.set_ylabel('Pulse Delay(ms)', fontsize=12)
ax.set_zlabel('Pulse Width(ms)', fontsize=12)
ax.grid(True, linestyle='-', color='0.75')

# 'i15' and 'f15' are not known as numerical types.
# You can use only 'i8' (np.int64) and 'f16' (np.float128) or 'f8' (np.float64).
# 'f16' seems not to be recognized as a valid type for the scatter library.
# I think that 'f8' and 'i8' are quoit enough.
data = np.genfromtxt('./sample.txt', dtype=[('col1', 'f8'), ('col2', 'i8'), ('col3',   'i8'), ('col4', 'S15')])

# scatter does not take for the c option a list of colours such as ['g', 'g', 'r', ...].
# In this case, we will define data for each colour, not a color for each scatter point.
m = data["col4"]
data1 = data[m == "PASS"]
data2 = data[m != "PASS"]

for dat, color in [(data1, 'g'), (data2, 'r')]:
    # Don't forget that having empty data columns may raise exceptions ...
    try:
        x, y, z = dat['col1'], dat['col2'], dat['col3']
        ax.scatter(xs=x, ys=y, zs=z, s=50, c=color, marker='o', linewidths=0)
    except:
        pass

plt.show()

您得到的错误代码表明您在某些时候传递了不适合变量的信息。 这似乎来自genfromtxt调用中的unpack = true标志。 查看有关genfromtxt的一些文档,似乎使用unpack = True标志将导致“返回的数组已转置,因此可以使用x,y,z = loadtxt(...)解压缩参数”,最终导致您的x,y,z和m值看起来像:

x = (6.0, 15, 21, 'PASS') 
y = (6.0, 15, 53, 'PASS') 
z = (6.0, 15, 91, 'PASS')
m = (6.0, 15, 104, 'PASS')

这些值显然不适用于代码中的内容。 有趣的是,只有在sample.txt中有4行并试图解压缩4个变量时,此方法才有效。

一种解决方案是不标记unpack = true。 相反,这样的事情应该工作:

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

## takes a matrix and a column index, returns a list of the values
## in that column
def column(matrix, i):
    return [row[i] for row in matrix]

fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(111, projection='3d') #ax = Axes3D(fig) 
ax.set_title("Plot 3d",fontsize=14)
ax.set_xlabel('Voltage (V)', fontsize=12)
ax.set_ylabel('Pulse Delay(ms)', fontsize=12)
ax.set_zlabel('Pulse Width(ms)', fontsize=12)
ax.grid(True, linestyle='-', color='0.75')

## Your original call
#x,y,z,m =genfromtxt('sample.txt', delimiter="""", dtype=[('col1', 'f15'),
##('col2','i15'), ('col3',   'i15'), ('col4', 'S15')],  unpack=True)

## The modified call
data = np.genfromtxt('sample.txt', 
                     delimiter='\t', 
                     usecols=(0,1,2,3),
                     dtype=[('col1', 'f15'), 
                           ('col2', 'i15'), 
                           ('col3', 'i15'), 
                           ('col4', 'S15')])

## split the data so that each column in the 2d array ends up in a variable.
## There is a numpy way of doing this, you should look into that
x = column(data, 0)
y = column(data, 1)
z = column(data, 2)
m = column(data, 3)

use_colours = []
for tmp in m:
    if tmp=='PASS':
        use_colours.append('g')
    else:
        use_colours.append('r')

## Let's make sure the values look like we expect:
print x
print "\n"
print y
print "\n"
print z
print "\n"
print m

ax.scatter(x,y,z, s=50, c=use_colours, marker = 'o', linewidths=0);
plt.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM