繁体   English   中英

类型错误:只有大小为 1 的数组可以转换为 Python 标量 (matplotlib)

[英]TypeError: only size-1 arrays can be converted to Python scalars (matplotlib)

#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from random import random

#Show Images
image_show = True

#Parameters ( mm )
square_size = 50
room_width = 5000
room_length = 10000
n_guess = 50 # number of random points generated
max_cell_value = 255 # highest number in a cell.

#Handy functions for conversions
def mm2grid( mm):
    return int(mm/square_size)
def grid2mm( grid):
    return int(grid*square_size)
def rotation( theta):
    return np.array([[np.cos(theta),-np.sin(theta)],[np.sin(theta),np.cos(theta)]])

# Build a simple empty map
map_floor = np.zeros((mm2grid(room_width), mm2grid(room_length)),)
# NOTE: Any cell >0 is considered occupied

print("Map size= {} cells".format( map_floor.shape))

# Set border walls
map_floor[ 0, :] = 255
map_floor[-1, :] = 255
map_floor[ :, 0] = 255
map_floor[ :,-1] = 255

#Draw something in the centre
map_floor[20:80,100:120] = 100

### Target Position ###
target = np.array([2500,0])

### Robot Position ###
x = np.array([1000,1000, np.pi*0])

# Randomly generate n_number of positions #
x_rand = np.zeros(( 3, n_guess))
for k in range( n_guess):
    x_rand[:,k] = np.array([ random()*room_width, random()* room_length, random()*3.14159]) 
    #remove any points inside a solid object
    if map_floor[ int(mm2grid( x_rand[0,k])), int(mm2grid( x_rand[1,k]))] > 0:
        x_rand[:,k] = 0

### TODO ###
#for n_number calculate angle to target#
#for n_number calculate distance measurement to nearest wall.

#### Display Map ####
# Remember: images are printed y inverted and x first.
if image_show:
    # Draw Map
    plt.matshow( max_cell_value-map_floor, cmap=plt.cm.gray) # max_cell_value is just to correct color scheme
    # Draw randomly positioned squares on the map
    plt.plot( [mm2grid( x_rand[1,:])], [mm2grid(x_rand[0,:])],'rx')
    # Draw Robot position
    plt.plot( [mm2grid( x[1])], [mm2grid(x[0])], 'b8', markersize=12)
    plt.plot( [mm2grid( x[1]), mm2grid( x[1]+ 300*np.cos(x[2]))],\
              [mm2grid( x[0]), mm2grid( x[0]+ 300*np.sin(x[2]))], 'b-', linewidth=2.5)
    plt.text( mm2grid( x[1]), mm2grid( x[0]+300), 'Robot', color='b')
    # Draw target
    plt.plot( [mm2grid( target[1])],[mm2grid( target[0])], 'g>', markersize=30)
    plt.text( mm2grid( target[1]), mm2grid( target[0] - 250), 'Target', color='g')
    # Draw line from robot to target
    plt.plot( [mm2grid( x[1]), mm2grid(target[1])],\
              [mm2grid( x[0]), mm2grid(target[0])], 'k--')
    # Show everything.
    plt.show()

我不确定如何修复错误。 在第 48 行中,我将 xrange 更改为 range 我还对其他一些地方做了一些其他的事情来修复和错误我在第 26 行中遇到了它说

TypeError: 'float' object cannot be interpreted as an index

但看起来我已经解决了这些问题,但我不确定我是否在某处搞砸了,现在它给了我这个错误:

Map size= (100, 200) cells
Traceback (most recent call last):
  File "/Users/oscarwallace/Downloads/Map.py", line 64, in <module>
    plt.plot( [mm2grid( x_rand[1,:])], [mm2grid(x_rand[0,:])],'rx')
  File "/Users/oscarwallace/Downloads/Map.py", line 19, in mm2grid
    return int(mm/square_size)
TypeError: only size-1 arrays can be converted to Python scalars

我想知道是否有人知道我必须做什么才能阻止这种情况发生。 提前致谢。

当您尝试将一些不只是一个标量的整数转换为整数时,会发生此错误。 例如,具有两个元素的ndarray 这是此错误的示例:

import numpy as np

int(np.array([1, 3]))

类型错误:只有大小为 1 的数组可以转换为 Python 标量

你可以做的是使用.astype(int)

(mm/square_size).astype(int)

暂无
暂无

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

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