使用 Matplotlib 在绘图上定义网格的大小 [英] Define the size of a grid on a plot using Matplotlib

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

问题描述

我正在Python中使用Matplotlib进行绘图.我想用网格创建绘图,这里是绘图教程中的一个示例.在我的绘图范围内,如果 y 轴从 0 到 14,并且如果我使用 pylab.grid(True) 那么它会生成一个大小为 2 的正方形的网格,但我希望大小为1.我该如何强制?

I am plotting using Matplotlib in Python. I want create plot with grid, and here is an example from a plotting tutorial. In my plot range if the y axis is from 0 to 14 and if I use pylab.grid(True) then it makes a grid with the size of square of two, but I want the size to be 1. How can I force it?

推荐答案

尝试使用 ax.grid(True,which ='both')将网格线放置在主要和次要刻度上,按照此处的建议.

Try using ax.grid(True, which='both') to position your grid lines on both major and minor ticks, as suggested here.

或者像这样手动设置刻度线:

Or just set your ticks manually, like this:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1,2,3,14],'ro-')

# set your ticks manually
ax.xaxis.set_ticks([1.,2.,3.,10.])
ax.grid(True)

plt.show()

这篇关于使用 Matplotlib 在绘图上定义网格的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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