如何在Python的绘图上绘制网格? [英] How do I draw a grid onto a plot in Python?

查看:1746
本文介绍了如何在Python的绘图上绘制网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚用Python中的 pylab 编写了代码以绘图,现在我想要在散点图上叠加一个10x10的网格.我该怎么办?

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?

我当前的代码如下:

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.show()

其输出为:

我想要的是以下输出:

根据安德烈·索博列夫(Andrey Sobolev)的答案添加了一个示例

Added an exemple, based on Andrey Sobolev's answer

推荐答案

您要使用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.gridax.yaxis.grid可以控制网格线的属性.

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

这篇关于如何在Python的绘图上绘制网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆