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

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

问题描述

我刚刚完成了使用 Python 中的

我想要的是以下输出:

添加了一个示例,基于 Andrey Sobolev 的回答

解决方案

你想使用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 可以控制网格线属性.

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 current code is the following:

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

And its output is:

What I would like is the following output:

EDIT: Added an exemple, based on Andrey Sobolev's answer

解决方案

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.

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

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