Scatterplot with different size, marker, and color from pandas dataframe

前端 未结 2 482
离开以前
离开以前 2020-12-17 00:09

I am trying to do a scatter plot with speed over meters for each point where marker indicate different types, size indicate different weights and color indicate how old a po

2条回答
  •  青春惊慌失措
    2020-12-17 00:44

    If you have just a few points, as here, you can pass a list of floats to the c argument:

    colors = ['r', 'b', 'k', 'g', 'm']
    plt.scatter(m.meters, m.speed, s=30*m.weight, vmin=0, vmax=10, cmap=cm)
    

    to have your points coloured in the order given. Alternatively, to use a colormap:

    cm = plt.cm.get_cmap('hot')  # or your colormap of choice
    plt.scatter(m.meters, m.speed, s=30*m.weight, c=m.old, cmap=cm)
    

    To change the marker shapes, you either need to add your own Patches, or add one point at a time: e.g.

    markers = ['^', 'o', 'v', 's', 'd']
    for px, py, c, s, t in zip(m.meters, m.speed, m.old, m.weight, markers):
        plt.scatter(px, py, marker=t, c=cm(c/10.), vmin=0, vmax=10, s=400*s+100)
    plt.show()
    

    enter image description here

    (I've scaled the m.weight to a different range to see the 5th point, which would otherwise have size 0.0).

提交回复
热议问题