TypeError: 'PathCollection' object is not iterable when adding second legend to plot

前端 未结 1 1156
终归单人心
终归单人心 2021-01-02 10:29

I am making a scatter plot from three separate dataframes and plotting the points as well as the best fit lines. I can accomplish this using this code:

impo         


        
相关标签:
1条回答
  • 2021-01-02 10:46

    I had the same error. I found out that you shouldn't include the comma after your variable names. So try

    scat1 =ax1.scatter(ex_x, ex_y, s=10, c='r', label='Fire Exclusion')
    scat2 =ax2.scatter(one_x,one_y, c='b', marker='s',label='One Fire')
    scat3 =ax3.scatter(two_x, two_y, s=10, c='g', marker='^', label='Two Fires')
    

    instead of

    scat1,=ax1.scatter(ex_x, ex_y, s=10, c='r', label='Fire Exclusion')
    scat2,=ax2.scatter(one_x,one_y, c='b', marker='s',label='One Fire')
    scat3,=ax3.scatter(two_x, two_y, s=10, c='g', marker='^', label='Two Fires')
    

    This is because axes.scatter returns a PathCollection unlike axes.plot which returns a tuple of the lines plotted (see http://matplotlib.org/1.3.1/users/pyplot_tutorial.html#controlling-line-properties and Python code. Is it comma operator?).

    So for your lines you will still need the comma because you are unpacking the tuple but for the scatter you should not have the comma.

    0 讨论(0)
提交回复
热议问题