Changing marker style in scatter plot according to third variable

前端 未结 3 1347
借酒劲吻你
借酒劲吻你 2020-12-18 22:49

I am dealing with a multi-column dictionary. I want to plot two columns and subsequently change color and style of the markers according to a third and fourth column.

<
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-18 23:23

    The problem is that marker can only be a single value and not a list of markers, as the color parmeter.

    You can either do a grouping by marker value so you have the x and y lists that have the same marker and plot them:

    xs = [[1, 2, 3], [4, 5, 6]]
    ys = [[1, 2, 3], [4, 5, 6]]
    m = ['o', 'x']
    for i in range(len(xs)):
        plt.scatter(xs[i], ys[i], marker=m[i])
    plt.show()
    

    Or you can plot every single dot (which I would not recommend):

    x=[1,2,3,4,5,6]
    y=[1,3,4,5,6,7]
    m=['k','l','l','k','j','l']
    
    mapping = {'j' : 'o', 'k': 'x', 'l': '+'}
    
    for i in range(len(x)):
        plt.scatter(x[i], y[i], marker=mapping[m[i]])
    plt.show()
    

提交回复
热议问题