Python Matplotlib:根据字符串标签数组输入设置手动x轴标签,但仅在主刻度上设置

Python Matplotlib:根据字符串标签数组输入设置手动x轴标签,但仅在主刻度上设置,python,pandas,matplotlib,plot,axis-labels,Python,Pandas,Matplotlib,Plot,Axis Labels,我试过了,但是标签没有打印在正确的位置。有些标签未打印,且打印位置不正确 我有一个对应于每个数据点的标签数组。我只想打印一些标签,并且只打印在主刻度上。但我不知道如何设置主刻度,并保持标签在正确的位置 import matplotlib.pyplot as plt import numpy as np import pandas as pd fig, ax1 = plt.subplots(1, 1) top = np.arange(100) btm = top-2 x = np.arange(l

我试过了,但是标签没有打印在正确的位置。有些标签未打印,且打印位置不正确

我有一个对应于每个数据点的标签数组。我只想打印一些标签,并且只打印在主刻度上。但我不知道如何设置主刻度,并保持标签在正确的位置

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
fig, ax1 = plt.subplots(1, 1)
top = np.arange(100)
btm = top-2
x = np.arange(len(top))
ax1.vlines(x, top, btm, color='r', linewidth=1)

labels = np.linspace(200,300,100).astype(np.int).astype(np.str)
factor = 10
labels = [label for i,label in enumerate(labels) if ((i+1)%factor==1)]
plt.xticks(x, labels, rotation='horizontal')
from matplotlib.ticker import MultipleLocator, FormatStrFormatter, FixedFormatter
majorLocator   = MultipleLocator(factor)
majorFormatter = FixedFormatter(labels)
minorLocator   = MultipleLocator(1)
ax1.xaxis.set_minor_locator(minorLocator)
ax1.xaxis.set_major_formatter(majorFormatter)
ax1.xaxis.set_major_locator(majorLocator)
plt.tick_params(axis='both', which='major', labelsize=9, length=10)
plt.tick_params(axis='both', which='minor', labelsize=5, length=4)
救命啊。谢谢

编辑:
标签数组的长度与数据点的数量相同,等于x轴的长度。所以对于x轴位置的每一个增量,我都有相应的标签。因此,对于x轴上的第i个位置或记号,应具有空标签或等于标签数组第i个元素的标签。如果它没有落在主刻度上,则应该为空标签不是简单的整数,而是字符串。更具体地说,它们是日期时间字符串。

如果没有明确的问题描述,我需要猜测以下可能是您想要的:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib.ticker import MultipleLocator

fig, ax1 = plt.subplots(1, 1)
top = np.arange(100)
btm = top-2
x = np.arange(len(top))
ax1.vlines(x+200, top, btm, color='r', linewidth=1)

majorLocator   = MultipleLocator(10)
minorLocator   = MultipleLocator(1)
ax1.xaxis.set_major_locator(majorLocator)
ax1.xaxis.set_minor_locator(minorLocator)

plt.tick_params(axis='both', which='major', labelsize=9, length=10)
plt.tick_params(axis='both', which='minor', labelsize=5, length=4)

plt.show()

您还可以使用
FuncFormatter
作为标签

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import MultipleLocator, FuncFormatter

fig, ax1 = plt.subplots(1, 1)
top = np.arange(100)
btm = top-2
x = np.arange(len(top))
ax1.vlines(x, top, btm, color='r', linewidth=1)

majorLocator   = MultipleLocator(10)
minorLocator   = MultipleLocator(1)
ax1.xaxis.set_major_locator(majorLocator)
ax1.xaxis.set_minor_locator(minorLocator)

fmt = lambda x,pos : str(int(x+200))
ax1.xaxis.set_major_formatter(FuncFormatter(fmt))


plt.tick_params(axis='both', which='major', labelsize=9, length=10)
plt.tick_params(axis='both', which='minor', labelsize=5, length=4)

plt.show()
我需要的是带有FixedFormatter的FixedLocator,还有一个整数数组majorpos,它指定了主记号所在的索引。 使用FuncFormatter的另一个答案会带来一些问题

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
fig, ax1 = plt.subplots(1, 1)
top = np.arange(100)
btm = top-2
x = np.arange(len(top))
ax1.vlines(x, top, btm, color='r', linewidth=1)

labels = np.linspace(200,300,100).astype(np.int).astype(np.str)
print(labels)
factor = 10
plt.xticks(x, labels, rotation='horizontal')

from matplotlib.ticker import MultipleLocator, FormatStrFormatter, FixedFormatter, FixedLocator
majorpos = np.arange(0,len(labels),int(len(labels)/10))
ax1.xaxis.set_major_locator(FixedLocator((majorpos)))
ax1.xaxis.set_major_formatter(FixedFormatter((labels[majorpos])))
ax1.xaxis.set_minor_locator(MultipleLocator(1))
plt.tick_params(axis='both', which='major', labelsize=9, length=10)
plt.tick_params(axis='both', which='minor', labelsize=5, length=4)

对不起,那不是我要找的。请检查我的编辑。我更新了答案。然而,问题似乎有点复杂。实际上,您希望格式化日期,但问题中没有日期。可以使用
matplotlib.Dates
定位器和格式化程序格式化日期。