简体   繁体   中英

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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