sorting labels in matplotlib scaterplot

后端 未结 3 1924
借酒劲吻你
借酒劲吻你 2021-01-27 02:32

I have the following code for a scatterplot, and the corresponding plot is shown below:

x = [\'C9-U2\', \'C10-U5\', \'C10-U5\', \'C11-U1\', \'C11-U1\']
y = [\'J\         


        
3条回答
  •  旧时难觅i
    2021-01-27 02:46

    I changed how your scatterplot is created quite a bit.

    Here is my code:

    import matplotlib.pyplot as plt
    
    # This is your original code.
    # x = ['C9-U2', 'C10-U5', 'C10-U5', 'C3-U1', 'C3-U1']
    # y = ['J',     'C',      'H',      'J',     'H']
    # plt.scatter(x,y)
    # plt.show()
    
    ordered_pairs = set([
         ('C9-U2', 'J'),
         ('C10-U5', 'C'),
         ('C10-U5', 'H'),
         ('C3-U1', 'J'),
         ('C3-U1', 'H')
    ])
    
    x,y = zip(*ordered_pairs)
    plt.scatter(x, y)
    plt.show()
    

    I turned your data points into a set of ordered pairs. This lets us zip the set, which is used to pack and unpack arrays with each argument passed. We use the * operator to inverse the process. You can read more about zip here.

    When the code is ran, the image shown is the following, which I hope is what you were looking for:

提交回复
热议问题