Plot is unclear using matplotlib and pandas library

后端 未结 1 1736
独厮守ぢ
独厮守ぢ 2021-01-27 23:01

Any explication why I got this kind of plot? The range of index returns is from 100 to 130. I need help to understand this plot above. The code is simple, but the plot

相关标签:
1条回答
  • 2021-01-27 23:18

    Matplotlib plots the data in the order it is provided. You may sort the data if that is required.

    import numpy as np
    import matplotlib.pyplot as plt
    
    x = np.array([3, 5, 1, 2, 7, 4, 6, 9, 8])
    y = np.array([8, 10, 3, 6, 8, 10, 10, 3, 6])
    
    plt.subplot(121)
    plt.plot(x,y, marker="o", label="unsorted")
    
    plt.legend()
    
    # now sort the values
    plt.subplot(122)
    plt.plot(np.sort(x),y[np.argsort(x)], marker="o", color="C3", label="sorted")
    
    plt.legend()
    plt.show()
    

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