奇怪的问题,我有一个脚本,它创建了一个matplotlib三维散点图,然后将其保存到PNG文件中。
脚本是:plot_data.py
如下所示:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm
#import numpy as np
import argparse
import os
from pylab import *
def get_data(filename):
data = []
with open(filename) as F:
for line in F:
splitted = line.split(sep=',')
formatted = (int(x) for x in splitted)
data.append(formatted)
return list(map(list, zip(*data)))
def create_plot(data, save_file):
fig = plt.figure()
ax = fig.gca(projection='3d')
cset = ax.scatter(xs=data[0], ys=data[1], zs=data[2])
ax.set_xlabel('DAC0')
ax.set_ylabel('DAC1')
ax.set_zlabel('RSSI')
fig.set_size_inches(18,12)
savefig(save_file, dpi=200, transparent=True, bbox_inches='tight', pad_inches=0)
if __name__ == '__main__':
parser = argparse.ArgumentParser( description = "Visualize the data")
parser.add_argument('path', help="The path to the data file to visualize")
parser.add_argument('result_image', help="The path to where the resultant png image should be saved")
p = parser.parse_args()
data = get_data(p.path)
create_plot(data, p.result_image)
当我从命令行调用脚本时,脚本才能找到。
像这样:python plot_data.py test_data.dat test_data.png
现在,当我尝试从另一个脚本调用它时,我遇到了问题。
我制作了一个名为test_plot.py
的示例脚本,用于测试plot_data函数。顺便说一下,test_plot.py
中的注释代码只是用来生成一些测试数据。
test_plot.py如下:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm
import argparse
import os
from pylab import *
from itertools import product
from plot_data import create_plot
from random import randint
if __name__ == '__main__':
#grid = product(range(5), range(5))
#xs, ys = list(map(list, zip(*grid)))
#zs = [randint(0,10) for i in range(len(xs))]
#my_gen = ('{},{},{}\n'.format(x,y,z) for x,y,z in zip(xs,ys,zs))
#with open('test_data.dat', 'w') as F:
# for x,y,z in zip(xs, ys, zs):
# F.writelines(my_gen)
input_file = r"test_data.dat"
output_file = r"test_data.png"
create_plot(input_file, output_file)
当我运行此脚本时,我得到以下输出:
c:\projects\a>python test_plot.py
Traceback (most recent call last):
File "test_plot.py", line 31, in <module>
create_plot(input_file, output_file)
File "c:\projects\a\plot_data.py", line 29, in create_
plot
cset = ax.scatter(xs=data[0], ys=data[1], zs=data[2])
File "C:\Users\gkuhn\AppData\Local\Continuum\Anaconda3\lib\site-packages\mpl_toolkits\mp
lot3d\axes3d.py", line 2240, in scatter
patches = Axes.scatter(self, xs, ys, s=s, c=c, *args, **kwargs)
File "C:\Users\gkuhn\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\axes
\_axes.py", line 3660, in scatter
self.add_collection(collection)
File "C:\Users\gkuhn\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\axes
\_base.py", line 1459, in add_collection
self.update_datalim(collection.get_datalim(self.transData))
File "C:\Users\gkuhn\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\coll
ections.py", line 189, in get_datalim
offsets = np.asanyarray(offsets, np.float_)
File "C:\Users\gkuhn\AppData\Local\Continuum\Anaconda3\lib\site-packages\numpy\core\nume
ric.py", line 514, in asanyarray
return array(a, dtype, copy=False, order=order, subok=True)
ValueError: could not convert string to float: 't'
c:\projects\a>
如果有帮助,我在Windows上使用Python3 64bit的anaconda发行版。 我认为问题是某种形式的命名空间?
答案 0 :(得分:1)
create_plot
将数据集作为第一个参数,而不是文件名。
你应该修改你的测试脚本:
from plot_data import create_plot, get_data
(...)
create_plot(get_data(input_file), output_file)
或修改您的create_plot
以将文件名作为第一个参数。
由于python是动态类型的,因此很难检测到这些问题(您可以在create_plot
中声明对象类型或编写文档以帮助您记住第一个参数是数据集)