我正在尝试获取一个刻度位置以确定它是否太靠近绘图的边缘,以便我可以在必要时删除其标签。但是,我尝试的任何方法总是为该位置返回零。下面是一些重现问题的示例代码和数据。如何获得刻度位置的实际x坐标?我的奇怪的情节格式化很大程度上是为了获得我最终目标情节所需的结果。
输出:
Python: 2.7.13 |Anaconda 4.3.1 (64-bit)| (default, Dec 20 2016, 23:09:15)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
Numpy: 1.11.3
Matplotlib: 2.0.0
0: 0
1: 0
2: 0
3: 0
4: 0
5: 0
代码:
import sys
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
print('Python: {:}'.format(sys.version))
print('Numpy: {:}'.format(np.__version__))
print('Matplotlib: {:}'.format(matplotlib.__version__))
print('')
def test(data, s=3):
XT = matplotlib.rcParams['xtick.direction']
matplotlib.rcParams['xtick.direction'] = 'in'
YT = matplotlib.rcParams['ytick.direction']
matplotlib.rcParams['ytick.direction'] = 'in'
fig = plt.figure(None)
grid = matplotlib.gridspec.GridSpec(1, 1)
grid.update(wspace=0, hspace=0)
ax = plt.subplot(grid[0])
ax.scatter(*data, c='k', marker='.', s=s)
ax.set_xlim(data[0,:].min(), data[0,:].max())
ax.set_ylim(6,13)
ax.yaxis.set_major_locator(matplotlib.ticker.MultipleLocator(4))
ax.yaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(1))
ax.invert_yaxis() # invert y axis because magnitudes suck
ax.ticklabel_format(style='sci', axis='x', scilimits=(-3,4))
ax.xaxis.tick_top()
ax.xaxis.set_ticks_position('both')
ax.xaxis.set_tick_params(pad=-10, labelsize=6)
for i, tick in enumerate(ax.xaxis.get_major_ticks()):
print("{:}: {:}".format(i, tick.get_loc()))
matplotlib.rcParams['xtick.direction'] = XT
matplotlib.rcParams['ytick.direction'] = YT
plt.savefig('test.png')
plt.close()
return None
_data = np.array([
[2017.33030847, 8.219],
[2017.33040156, 8.7],
[2017.33042138, 8.66],
[2017.33042305, 8.522],
[2017.33042486, 8.552],
[2017.33042669, 8.533],
[2017.33193969, 8.5],
[2017.33198084, 8.3],
[2017.33237782, 8.2],
[2017.33304624, 8.3],
[2017.33315704, 8.674],
[2017.33315869, 8.533],
[2017.33316049, 8.556],
[2017.33316233, 8.564],
[2017.33471869, 8.3],
[2017.33514031, 8.3],
[2017.33526788, 8.553],
[2017.335884, 8.803],
[2017.33588591, 8.643],
[2017.33588805, 8.459],
[2017.33589005, 8.623],
[2017.3372728, 8.6],
[2017.33731083, 8.5],
[2017.33740178, 8.3],
[2017.33763492, 8.642],
[2017.33772912, 8.4],
[2017.33863773, 8.85],
[2017.33863945, 8.725],
[2017.33864134, 8.618],
[2017.33864312, 8.722],
[2017.34001065, 8.6],
[2017.34036185, 8.587],
[2017.34044986, 8.5],
[2017.34134218, 8.901],
[2017.34134385, 8.737],
[2017.34134565, 8.701],
[2017.34134749, 8.762],
[2017.34135105, 8.7],
[2017.34148646, 8.676],
[2017.34293224, 8.4],
[2017.3430299, 8.7],
[2017.34309193, 8.746],
[2017.34406418, 8.8],
[2017.34407559, 8.6],
[2017.34409213, 9.046],
[2017.3440938, 8.909],
[2017.34409566, 8.842],
[2017.34409758, 8.945],
[2017.34413369, 8.829],
[2017.34413481, 8.823],
[2017.34545402, 8.9],
[2017.3455586, 8.65],
[2017.34565366, 8.7],
[2017.3459939, 8.965],
[2017.3466423, 9.0],
[2017.3485529, 9.171],
[2017.34927556, 9.2],
[2017.35129176, 9.479],
[2017.35140589, 9.43],
[2017.35207803, 9.5],
[2017.35504756, 10.224],
[2017.35504917, 10.049],
[2017.35505101, 9.934],
[2017.3550529, 9.888],
[2017.35688835, 10.6],
[2017.35778546, 10.758],
[2017.3577871, 10.588],
[2017.35778899, 10.438],
[2017.35779083, 10.324],
[2017.35909574, 10.6],
[2017.36045892, 11.077],
[2017.36046363, 10.901],
[2017.36046831, 10.646],
[2017.36047302, 10.313],
[2017.36246264, 11.292],
[2017.36304093, 11.4],
[2017.36319206, 11.627],
[2017.3631968, 11.312],
[2017.36320159, 10.973],
[2017.36320632, 10.593],
[2017.36324624, 11.2],
[2017.36512412, 11.426],
[2017.36594136, 12.087],
[2017.36594612, 11.668],
[2017.36595083, 11.246],
[2017.36595556, 10.747],
[2017.36616392, 11.574],
[2017.36616507, 11.593],
[2017.36744238, 11.7],
[2017.36749076, 11.7],
[2017.36814631, 11.755],
[2017.36874286, 11.9],
[2017.37039888, 11.9],
[2017.3728553, 12.1],
[2017.373535, 11.963],
[2017.3739885, 11.7],
[2017.37540315, 11.8],
[2017.37572819, 11.7],
[2017.3781191, 11.8],
[2017.37844131, 11.8],
]).T
test(_data)
答案 0 :(得分:1)
刻度线的位置仅在抽奖时确定。
因此,为了能够访问实际位置,您需要先绘制图形。这可以通过
完成fig.canvas.draw()
并将导致打印实际位置
0: 0
1: 2017.34
2: 2017.35
3: 2017.36
4: 2017.37
5: 0
答案 1 :(得分:0)
我发现你可以在没有用
绘制图形的情况下获得刻度位置ax.get_xticks()
返回x tick位置的数组。这些是indexex,其顺序与
相同ax.xaxis.get_major_ticks()
所以要使用x tick位置用tick / labels做一些事情,可以做以下事情:
xticks = ax.xaxis.get_major_ticks()
xlocs = ax.get_xticks()
for i, x in enumerate(xlocs):
tick = xticks[i]
if some_condition(x): # put your condition here
do_stuff_to(tick) # act on tick here