Scatter plot colorbar - Matplotlib

后端 未结 1 1527
没有蜡笔的小新
没有蜡笔的小新 2020-12-18 18:45

I\'m trying to show a color bar of my scatter plot but I\'m keep getting the error:

TypeError: You must first set_array for mappable

相关标签:
1条回答
  • 2020-12-18 19:21

    You're passing in specific rgb values, so matplotlib can't construct a colormap, because it doesn't know how it relates to your original data.

    Instead of mapping the values to RGB colors, let scatter handle that for you.

    Instead of:

    # Mapping the values to RGBA colors
    data = plt.cm.jet(data[x_data, y_data])
    
    pts = plt.scatter(x_data, y_data, marker='s', color=data)
    

    Do:

    pts = plt.scatter(x_data, y_data, marker='s', c=data[x_data, y_data])
    

    (Just pass in to c what you were originally passing into plt.cm.jet.)

    Then you'll be able to construct a colormap normally. The specific error is telling you that the colors have been manually set, rather than set through set_array (which handles mapping an array of data values to RGB).

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