简体   繁体   English

如何在matplotlib中的网格线之间打勾?

[英]How to make axes ticks in between grid lines in matplotlib?

In my simple example below, how to make x-axis tick values to appear between grids? 在下面的简单示例中,如何使x轴刻度值出现在网格之间?

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(1)
x = range(10)
y = np.random.random(10)
plt.plot(x,y)
plt.xticks(x)
plt.grid(True)
plt.show()

在此处输入图片说明

The following make ticks to be where I want but the grid lines also moves. 以下是我想要的位置,但网格线也会移动。

np.random.seed(1)
x = range(10)
y = np.random.random(10)
plt.plot(x,y)
plt.xticks(x)
plt.grid(True)
plt.xticks(np.arange(10)+0.5, x)
plt.show()

在此处输入图片说明

I would like the result to be: 我希望结果是: 在此处输入图片说明

You can set the minor ticks so that only 1 minor tick appears inbetween your major ticks. 您可以设置次要刻度,以便在主要刻度之间仅出现1个次要刻度。 This is done using matplotlib.ticker.AutoMinorLocator . 这是使用matplotlib.ticker.AutoMinorLocator完成的。 Then, set the gridlines to only appear at the minor ticks. 然后,将网格线设置为仅出现在较小的刻度上。 You also need to shift your xtick positions by 0.5: 您还需要将xtick位置偏移0.5:

from matplotlib.ticker import AutoMinorLocator

np.random.seed(10)

x = range(10)
y = np.random.random(10)
plt.plot(x,y)
plt.xticks(np.arange(0.5,10.5,1), x)
plt.xlim(0,9.5)
plt.ylim(0,1)
minor_locator = AutoMinorLocator(2)
plt.gca().xaxis.set_minor_locator(minor_locator)
plt.grid(which='minor')

plt.show()

在此处输入图片说明

Edit : I'm having trouble getting two AutoMinorLocator s to work on the same axis. 编辑 :我在使两个AutoMinorLocator在同一轴上工作时遇到麻烦。 When trying to add in another one for the y axis, the minor ticks get messed up. 当尝试为y轴添加另一个时,较小的刻度会被弄乱。 A work around I have found is to manually set the locations of the minor ticks using a matplotlib.ticker.FixedLocator and passing in the locations of the minor ticks. 我发现一种解决方法是使用matplotlib.ticker.FixedLocator手动设置次要刻度线的位置,然后传递次要刻度线的位置。

from matplotlib.ticker import AutoMinorLocator
from matplotlib.ticker import FixedLocator
np.random.seed(10)

x = range(10)
y = np.random.random(10)
plt.plot(x,y)
plt.xticks(np.arange(0.5,10.5,1), x)
plt.yticks([0.05,0.15,0.25,0.35,0.45,0.55,0.65,0.75,0.85,0.95,1.05], [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1])
plt.xlim(0,9.5)
plt.ylim(0,1.05)

minor_locator1 = AutoMinorLocator(2)
minor_locator2 = FixedLocator([0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1])
plt.gca().xaxis.set_minor_locator(minor_locator1)
plt.gca().yaxis.set_minor_locator(minor_locator2)
plt.grid(which='minor')

plt.show()

在此处输入图片说明

If you use plt.subplots for figure creation, you get an axes object, too: 如果使用plt.subplots进行图形创建,那么也会得到一个axes对象:

f, ax = plt.subplots(1)

This one has a better Interface for adjusting grid/ticks. 这是一个用于调整网格/刻度的更好的界面。 Then you can give explicitly x-values for your data shifted 0.5 to the left. 然后,您可以为数据显式给出向左移动0.5的x值。 The same do with the minor ticks and let the grid be shown at the minor ticks: 小刻度线也是如此,并在小刻度线处显示网格:

f, ax = plt.subplots(1)
ax.set_xticks(range(10))
x_values = np.arange(10) - .5
ax.plot(x_values, np.random.random(10))
ax.set_xticks(x_values, minor=True)
ax.grid(which='minor')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM