TypeError:仅大小为1的数组可以转换为Python标量(matplotlib)

问题描述 投票:0回答:1
#!/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

我想知道是否有人知道我该怎么做才能避免这种情况的发生。预先感谢。

python arrays matplotlib
1个回答
0
投票

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

import numpy as np

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

TypeError:仅大小为1的数组可以转换为Python标量

您可以使用.astype(int)

(mm/square_size).astype(int)
© www.soinside.com 2019 - 2024. All rights reserved.