Adding line to scatter plot using python's matplotlib

前端 未结 2 1271
旧时难觅i
旧时难觅i 2021-02-07 12:03

I am using python\'s matplotlib and want to create a matplotlib.scatter() with additional line. The line should proceed from the lower left corner to the upper righ

2条回答
  •  广开言路
    2021-02-07 12:40

    Besides unutbu's answer one other option is to get the limits of the axis after you ploted the data and to use them to add the line. After this you will still need to change back the axis limits as they would change with the addition of the line:

    # Scatter Plot
    x = data_calc_hourly.temp
    y =  data_obs_hourly.temp
    
    lineStart = data_calc_hourly.temp.min() 
    lineEnd = data_calc_hourly.temp.max()  
    
    plt.figure()
    plt.scatter(x, y, color = 'k', alpha=0.5)
    y_lim = plt.ylim()
    x_lim = plt.xlim()
    plt.plot(x_lim, y_lim, 'k-', color = 'r')
    plt.ylim(y_lim)
    plt.xlim(x_lim)
    plt.show()
    

提交回复
热议问题