Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在matplotlib中绘制规则网格作为背景_Python_Matplotlib - Fatal编程技术网

Python 如何在matplotlib中绘制规则网格作为背景

Python 如何在matplotlib中绘制规则网格作为背景,python,matplotlib,Python,Matplotlib,我想在2D绘图线的背景上绘制一个网格,与ECG演示类似,即在规则间隔的特定点上显示点,如图所示 在本例中,有4个点正好间隔在到主点之间。我不想要这样的东西,即只是虚线 到目前为止,我所做的(来自古老的matlab知识)是: 但这让我明白: 有什么地方不对?正如jpnadas所说,您可以使用plt.grid() 下面是一个关于如何放置和自定义网格的示例 import matplotlib.pyplot as plt from matplotlib.ticker import MultipleL

我想在2D绘图线的背景上绘制一个网格,与ECG演示类似,即在规则间隔的特定点上显示点,如图所示

在本例中,有4个点正好间隔在到主点之间。我不想要这样的东西,即只是虚线

到目前为止,我所做的(来自古老的matlab知识)是:

但这让我明白:


有什么地方不对?

正如jpnadas所说,您可以使用plt.grid()

下面是一个关于如何放置和自定义网格的示例

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator

img = plt.imread(imagename)
_, ax = plt.subplots(ncols=1,nrows=1)
ax.imshow(img)

plt.gca().xaxis.set_major_locator(MultipleLocator(16))
plt.gca().yaxis.set_major_locator(MultipleLocator(16))
plt.gca().xaxis.set_minor_locator(MultipleLocator(32))
plt.gca().yaxis.set_minor_locator(MultipleLocator(32))

# Don't allow the axis to be on top of your data
ax.set_axisbelow(True)
# Turn on the minor TICKS, which are required for the minor GRID
ax.minorticks_on()
# Customize the major grid
ax.grid(which='major', linestyle='-', linewidth='4', color='yellow')
# Customize the minor grid
ax.grid(which='minor', linestyle=':', linewidth='2', color='blue')

plt.show()

我发现了我的错误。这不是我思维中的一个错误,而是我的len(y)在x的linspace中引用了错误的向量,从而产生了一个太精细的网格,看起来像一条线。

我认为你不需要走那么远。你能试着只用
plt.grid()
吗?对不起,我想要一个精确定义的网格模式,网格没有给我
plt.grid()
几个选项,你确定你不能在那里定义它吗?@jpnadas我仔细检查了文档,但没有我能看到的那么远看看主/次网格线。。。谢谢你的建议,但我必须澄清:我想要的是一个确定的点间距。我想在主点之间有四个小点。我将把这些信息添加到原始帖子的解释中。您可以在两个ax.grid()中使用linestyle=(0,(1,4))和linestyle=(0,(1,2))。但我认为它不能解决问题,因为我不能“对齐”点
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator

img = plt.imread(imagename)
_, ax = plt.subplots(ncols=1,nrows=1)
ax.imshow(img)

plt.gca().xaxis.set_major_locator(MultipleLocator(16))
plt.gca().yaxis.set_major_locator(MultipleLocator(16))
plt.gca().xaxis.set_minor_locator(MultipleLocator(32))
plt.gca().yaxis.set_minor_locator(MultipleLocator(32))

# Don't allow the axis to be on top of your data
ax.set_axisbelow(True)
# Turn on the minor TICKS, which are required for the minor GRID
ax.minorticks_on()
# Customize the major grid
ax.grid(which='major', linestyle='-', linewidth='4', color='yellow')
# Customize the minor grid
ax.grid(which='minor', linestyle=':', linewidth='2', color='blue')

plt.show()