Intersecting matplotlib graph with unsorted data

前端 未结 1 1749
再見小時候
再見小時候 2020-12-16 15:43

When plotting some points with matplotlib I encountered some strange behavior when creating a graph. Here is the code to produce this graph.

imp         


        
相关标签:
1条回答
  • 2020-12-16 16:17

    You can maintain the order using numpy's argsort function.

    Argsort "...returns an array of indices of the same shape as a that index data along the given axis in sorted order.", so we can use this to re-order the x and y coordinates together. Here's how it's done:

    import matplotlib.pyplot as plt
    import numpy as np
    
    desc_x =[4000,3000,2000,2500,2750,2250,2300,2400,2450,2350]
    rmse_desc = [.31703 , .31701, .31707, .31700, .31713, .31698, .31697, .31688, .31697, .31699]
    
    order = np.argsort(desc_x)
    xs = np.array(desc_x)[order]
    ys = np.array(rmse_desc)[order]
    
    fig = plt.figure()
    ax = plt.subplot(111)
    
    fig.suptitle('title')
    plt.xlabel('x')
    plt.ylabel('y')
    
    ax.plot(xs, ys, 'b', label='desc' )
    ax.legend()
    plt.show()
    

    enter image description here

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