How to plot one single data point?

前端 未结 4 583
旧时难觅i
旧时难觅i 2020-12-28 12:46

I have the following code to plot a line and a point:

df = pd.DataFrame({\'x\': [1, 2, 3], \'y\': [3, 4, 6]})
point = pd.DataFrame({\'x\': [2], \'y\': [5]})
         


        
4条回答
  •  暖寄归人
    2020-12-28 13:23

    Another issue that exists when using the .plot(..) method is that the legend is displayed with lines and not dots. To fix this issue, I would recommend to use plt.scatter(..) as such:

    df = pd.DataFrame({'x': [1, 2, 3], 'y': [3, 4, 6]})
    point = pd.DataFrame({'x': [2], 'y': [5]})
    
    fig, axes = plt.subplots(1, 2, figsize=(20, 5))
    
    # OP VERSION
    df.plot('x', 'y', ax=axes[0], label='line')
    point.plot('x', 'y', ax=axes[0], style='r-', label='point')
    
    # MY VERSION
    df.plot('x', 'y', ax=axes[1], label='line')
    axes[1].scatter(point['x'], point['y'], marker='o', color='r', label='point')
    axes[1].legend(loc='upper left')
    

    I obtain this result, with on the left, OP's method, and on the right, my method:

提交回复
热议问题