How do I draw a grid onto a plot in Python?

前端 未结 5 957
挽巷
挽巷 2020-12-04 06:06

I just finished writing code to make a plot using pylab in Python and now I would like to superimpose a grid of 10x10 onto the scatter plot. How do I do that?

My cur

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-04 06:55

    You want to use pyplot.grid:

    x = numpy.arange(0, 1, 0.05)
    y = numpy.power(x, 2)
    
    fig = plt.figure()
    ax = fig.gca()
    ax.set_xticks(numpy.arange(0, 1, 0.1))
    ax.set_yticks(numpy.arange(0, 1., 0.1))
    plt.scatter(x, y)
    plt.grid()
    plt.show()
    

    ax.xaxis.grid and ax.yaxis.grid can control grid lines properties.

    Enter image description here

提交回复
热议问题