Python 打印仅在硬编码或复制/粘贴时有效

Python 打印仅在硬编码或复制/粘贴时有效,python,python-2.7,numpy,matplotlib,Python,Python 2.7,Numpy,Matplotlib,更新2完整错误代码: Traceback (most recent call last): File "C:\Users\Desktop\Radar2.py", line 156, in <module> radar = ComplexRadar(fig1, variables, ranges) File "C:\Users\Desktop\Radar2.py", line 127, in __init__ num=n_ordinate_levels) F

更新2完整错误代码:

Traceback (most recent call last):
  File "C:\Users\Desktop\Radar2.py", line 156, in <module>
    radar = ComplexRadar(fig1, variables, ranges)
  File "C:\Users\Desktop\Radar2.py", line 127, in __init__
    num=n_ordinate_levels)
  File "C:\Python27\lib\site-packages\numpy\core\function_base.py", line 90, in linspace
    start = start * 1.
TypeError: can't multiply sequence by non-int of type 'float'
这是我的密码。我的错误是“TypeError:不能将序列乘以'float'类型的非int”。我需要将它保持为浮点,因为我需要保留小数点,如果转换为Int,我将丢失小数点

import matplotlib.pyplot as plt
import numpy as np
import Image

tup1, tup2, data, variables, ranges = [], [], [], [], []

with open('Book1.csv') as f:
    content = f.read().splitlines()    
    data.append(content[7].split(',')[1])
    variables.append(content[7].split(',')[0])    
    tup1.append(content[7].split(',')[3])
    tup2.append(content[7].split(',')[4])

    data.append(content[6].split(',')[1])
    variables.append(content[6].split(',')[0])
    tup1.append(content[6].split(',')[3])
    tup2.append(content[6].split(',')[4])

    data.append(content[5].split(',')[1])
    variables.append(content[5].split(',')[0])
    tup1.append(content[5].split(',')[3])
    tup2.append(content[5].split(',')[4])

    data.append(content[4].split(',')[1])
    variables.append(content[4].split(',')[0])
    tup1.append(content[4].split(',')[3])
    tup2.append(content[4].split(',')[4])

    data.append(content[15].split(',')[1])
    variables.append(content[15].split(',')[0])
    tup1.append(content[15].split(',')[3])
    tup2.append(content[15].split(',')[4])

    data.append(content[14].split(',')[1])
    variables.append(content[14].split(',')[0])
    tup1.append(content[14].split(',')[3])
    tup2.append(content[14].split(',')[4])

    data.append(content[13].split(',')[1])
    variables.append(content[13].split(',')[0])
    tup1.append(content[13].split(',')[3])
    tup2.append(content[13].split(',')[4])

    data.append(content[12].split(',')[1])
    variables.append(content[12].split(',')[0])
    tup1.append(content[12].split(',')[3])
    tup2.append(content[12].split(',')[4])

    data.append(content[11].split(',')[1])
    variables.append(content[11].split(',')[0])
    tup1.append(content[11].split(',')[3])
    tup2.append(content[11].split(',')[4])

    data.append(content[10].split(',')[1])
    variables.append(content[10].split(',')[0])
    tup1.append(content[10].split(',')[3])
    tup2.append(content[10].split(',')[4])

    data.append(content[9].split(',')[1])
    variables.append(content[9].split(',')[0])
    tup1.append(content[9].split(',')[3])
    tup2.append(content[9].split(',')[4])

    data.append(content[8].split(',')[1])
    variables.append(content[8].split(',')[0])
    tup1.append(content[8].split(',')[3])
    tup2.append(content[8].split(',')[4])


    Name = content[0].split(',')[1]
    Team = content[1].split(',')[1]
    Season = content[2].split(',')[1]
    Age = content[3].split(',')[1]

    data = [float(i) for i in data]
    ranges = zip(tup1, tup2)
    variables = variables

##variables = ['Var4', 'Var3', 'Var2', 'Var1', 'Var12', 'Var11', 'Var10', 'Var9', 'Var8', 'Var7', 'Var6', 'Var5']
##data = [4.0, 3.0, 2.0, 1.0, 12.0, 4.2, 9.9, 8.8, 7.7, 6.6, 5.5, 5.0]
##ranges = [(0.001, 12), (0.001, 12), (0.001, 12), (0.001, 12), (0.001, 12), (0.001, 12), (0.001, 12), (0.001, 12), (0.001, 12), (0.001, 12), (0.001, 12), (0.001, 12)]

def _invert(x, limits):
    """inverts a value x on a scale from
    limits[0] to limits[1]"""
    return limits[1] - (x - limits[0])

def _scale_data(data, ranges):
    """scales data[1:] to ranges[0],
    inverts if the scale is reversed"""
    for d, (y1, y2) in zip(data[1:], ranges[1:]):
        assert (y1 <= d <= y2) or (y2 <= d <= y1)
    x1, x2 = ranges[0]
    d = data[0]
    if x1 > x2:
        d = _invert(d, (x1, x2))
        x1, x2 = x2, x1
    sdata = [d]
    for d, (y1, y2) in zip(data[1:], ranges[1:]):
        if y1 > y2:
            d = _invert(d, (y1, y2))
            y1, y2 = y2, y1
        sdata.append((d-y1) / (y2-y1) 
                     * (x2 - x1) + x1)
    return sdata

class ComplexRadar():
    def __init__(self, fig, variables, ranges,
                 n_ordinate_levels=7):
        angles = np.arange(0, 360, 360./len(variables))

        axes = [fig.add_axes([0.1,0.1,0.9,0.9],polar=True, axisbg='#ffffff',
                label = "axes{}".format(i))
                for i in range(len(variables))]
        l, text = axes[0].set_thetagrids(angles, 
                                         labels=variables, weight='semibold')
        [txt.set_rotation(angle-90) for txt, angle 
             in zip(text, angles)]
        for ax in axes[1:]:
            ax.patch.set_visible(False)
            ax.grid("off")
            ax.grid(False)
            ax.xaxis.set_visible(False)

        for i, ax in enumerate(axes):
            grid = np.linspace(*ranges[i], 
                               num=n_ordinate_levels)
            gridlabel = ["{}".format(round(x,2)) 
                         for x in grid]
            if ranges[i][0] > ranges[i][1]:
                grid = grid[::-1]
            gridlabel[0] = ""
            ax.set_rgrids(grid, labels=gridlabel, ha="center", va="center",
                         angle=angles[i], weight='semibold', fontsize=8, color='#333333')
            ax.spines["polar"].set_visible(False)
            ax.set_ylim(*ranges[i])
            theta = np.linspace(0., 2*np.pi, 80, endpoint=True)
        # THE LINE BELOW IS CAUSING THE PROBLEMS

        # variables for plotting
        self.angle = np.deg2rad(np.r_[angles, angles[0]])
        self.ranges = ranges
        self.ax = axes[0]
        self.ax.yaxis.grid(False)
    def plot(self, data, *args, **kw):
        sdata = _scale_data(data, self.ranges)
        self.ax.plot(self.angle, np.r_[sdata, sdata[0]], *args, **kw)
    def fill(self, data, *args, **kw):
        sdata = _scale_data(data, self.ranges)
        self.ax.fill(self.angle, np.r_[sdata, sdata[0]], *args, **kw)



# plotting
fig1 = plt.figure(figsize=(10, 10))
radar = ComplexRadar(fig1, variables, ranges)
radar.plot(data, color='#0000e6', linewidth=1)
radar.fill(data, alpha=0.4, color='#0000e6')
plt.savefig('radar-chart2.png',orientation='landscape',bbox_inches='tight',pad_inches=.8)  
导入matplotlib.pyplot作为plt
将numpy作为np导入
导入图像
tup1,tup2,数据,变量,范围=[],[],[],[],[],[]
将open('Book1.csv')作为f:
content=f.read().splitlines()
data.append(内容[7]。拆分(',)[1])
变量。追加(内容[7]。拆分(',')[0])
tup1.append(内容[7]。拆分(',)[3])
tup2.append(内容[7]。拆分(',)[4])
data.append(内容[6]。拆分(',)[1])
变量。追加(内容[6]。拆分(',')[0])
tup1.append(内容[6]。拆分(',)[3])
tup2.append(内容[6]。拆分(',)[4])
data.append(内容[5]。拆分(',)[1])
变量。追加(内容[5]。拆分(',')[0])
tup1.append(内容[5]。拆分(',)[3])
tup2.append(内容[5]。拆分(',)[4])
data.append(内容[4]。拆分(',)[1])
变量。追加(内容[4]。拆分(',')[0])
tup1.append(内容[4]。拆分(',)[3])
tup2.append(内容[4]。拆分(',)[4])
data.append(内容[15]。拆分(',)[1])
变量。追加(内容[15]。拆分(',')[0])
tup1.append(内容[15]。拆分(',)[3])
tup2.append(内容[15]。拆分(',)[4])
data.append(内容[14]。拆分(',)[1])
变量。追加(内容[14]。拆分(',')[0])
tup1.append(内容[14]。拆分(',)[3])
tup2.append(内容[14]。拆分(',)[4])
data.append(内容[13]。拆分(',)[1])
变量。追加(内容[13]。拆分(',')[0])
tup1.append(内容[13]。拆分(',)[3])
tup2.append(内容[13]。拆分(',)[4])
data.append(内容[12]。拆分(',)[1])
变量。追加(内容[12]。拆分(',')[0])
tup1.append(内容[12]。拆分(',)[3])
tup2.append(内容[12]。拆分(',)[4])
data.append(内容[11]。拆分(',)[1])
变量。追加(内容[11]。拆分(',')[0])
tup1.append(内容[11]。拆分(',)[3])
tup2.append(内容[11]。拆分(',)[4])
data.append(内容[10]。拆分(',')[1])
变量。追加(内容[10]。拆分(',')[0])
tup1.append(内容[10]。拆分(',)[3])
tup2.append(内容[10]。拆分(',)[4])
data.append(内容[9]。拆分(',)[1])
变量。追加(内容[9]。拆分(',')[0])
tup1.append(内容[9]。拆分(',)[3])
tup2.append(内容[9]。拆分(',)[4])
data.append(内容[8]。拆分(',)[1])
变量。追加(内容[8]。拆分(',')[0])
tup1.append(内容[8]。拆分(',)[3])
tup2.append(内容[8]。拆分(',)[4])
名称=内容[0]。拆分(',')[1]
团队=内容[1]。拆分(',)[1]
季节=内容[2]。拆分(',')[1]
年龄=内容[3]。拆分(',)[1]
数据=[数据中i的浮点(i)]
ranges=zip(图1、图2)
变量=变量
##变量=['Var4','Var3','Var2','Var1','Var12','Var11','Var10','Var9','Var8','Var7','Var6','Var5']
##数据=[4.0,3.0,2.0,1.0,12.0,4.2,9.9,8.8,7.7,6.6,5.5,5.0]
##范围=[(0.001,12)、(0.001,12)、(0.001,12)、(0.001,12)、(0.001,12)、(0.001,12)、(0.001,12)、(0.001,12)、(0.001,12)、(0.001,12)、(0.001,12)、(0.001,12)、(0.001,12)]
def_反转(x,极限):
“”“将刻度上的值x从
限制[0]到限制[1]“”“
返回限制[1]-(x-限制[0])
def刻度数据(数据、范围):
“”“将数据[1]缩放到范围[0],
如果比例反转,则反转
对于zip中的d,(y1,y2)(数据[1],范围[1]):

断言(y1错误在这一行:

for i, ax in enumerate(axes):
    grid = np.linspace(*ranges[i], 
                       num=n_ordinate_levels)
这是因为当您从文件中读取这些元素时,
范围中的元素不是float/int:

ranges
Out[24]: 
[('0.001', '12'),
 ('0.001', '12'),
 ('0.001', '12'),
 ('0.001', '12'),
 ('0.001', '12'),
 ('0.001', '12'),
 ('0.001', '12'),
 ('0.001', '12'),
 ('0.001', '12'),
 ('0.001', '12'),
 ('0.001', '12'),
 ('0.001', '12')]
这基本上意味着您不应该将字符串传递给
np.linspace
。您可以通过添加一行将字符串转换为数字来解决此问题:

data = [float(i) for i in data]
ranges = zip(tup1, tup2)
ranges = [(float(r[0]), int(r[1])) for r in ranges] #this is the added line

请始终添加完整的错误跟踪,而不仅仅是错误的名称。用错误更新。有什么想法吗?有人有什么想法吗?引发异常的代码是“start=start*1”。但在您共享的代码中不会出现这种情况。请共享完整的代码,以便我们能够提供帮助。它发生在numpy内部…您需要发布完整的跟踪!!
data = [float(i) for i in data]
ranges = zip(tup1, tup2)
ranges = [(float(r[0]), int(r[1])) for r in ranges] #this is the added line