当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Cannot save figure (TypeError) in matplotlib?

问题描述:

Inspired by this answer, I am trying to highlight my segments with the thick yellow half-opaque lines highlighter. The visible part of highlighter is constrained by propArea.

Code:

def showAfterExpand(segments, segWidths, trajectories):

print segWidths

fig = plt.figure()

axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # left, bottom, width, height (range 0 to 1)

axes.axis('equal')

axes.set_xlabel('x (m)')

axes.set_ylabel('y (m)')

axes.set_title('After Expanding')

for i, segment in enumerate(segments):

# draw this segment

axes.plot([segment[0][0], segment[1][0]], [segment[0][1], segment[1][1]], color='b')

w1, w2 = segWidths[i][0], segWidths[i][1]

len = np.linalg.norm((segment[0][0]-segment[1][0], segment[0][1]-segment[1][1]))

# make sure to use data 'units', so set the transform to transData

propArea = pch.Rectangle(segment[0], width=len, height=w1 + w2, transform=axes.transData)

# save the line so when can set the clip

highlighter, = axes.plot([segment[0][0], segment[1][0]], [segment[0][1], segment[1][1]],

color='yellow',

linewidth=50,

alpha=0.5)

highlighter.set_clip_path(propArea)

fig.savefig(constants.PROJECT_PATH + '\\data\\%i_7_expand.svg'%len(trajectories))

Error Message:

in showAfterExpand(segments, segWidths, trajectories)

163 alpha=0.5)

164 corridor.set_clip_path(propArea)

--> 165 fig.savefig(constants.PROJECT_PATH + '\\data\\%i_7_expand.svg'%len(trajectories))

TypeError: 'numpy.float64' object is not callable

Note: I fully understand that I should make the code "copy-n-paste runnable" so that you guys can easily help spot the problem. I've tried really hard, but strangely once I simplified it and made it "copy-n-paste runnable", the error was gone! So I have no way but to directly post the snippet up.

What went wrong?

网友答案:

you are shadowing len with this line

len = np.linalg.norm((segment[0][0]-segment[1][0], segment[0][1]-segment[1][1]))

The error is telling you that you are trying to call a float ie 3(...)

You can reproduce this error with:

x = [1, 2, 5]
len = 5
len(x)
分享给朋友:
您可能感兴趣的文章:
随机阅读: