简体   繁体   English

如何编辑 matplotlib x-tickmarks 以匹配 x-labels

[英]How to edit matplotlib x-tickmarks to match x-labels

I'm trying to edit the tickmarks and labels on my x-axis to match the data I'm plotting.我正在尝试编辑 x 轴上的刻度线和标签以匹配我正在绘制的数据。 The two variables (x and bin_river) have a shape of 68 so I want to reduce the number of tickmarks plotted and change the labels for the first and list number in array [x].两个变量(x 和 bin_river)的形状为 68,因此我想减少绘制的刻度线数量并更改数组 [x] 中第一个和列表编号的标签。 Array [x] looks like this数组 [x] 看起来像这样

print(x)
[-30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10  -9  -8  -7  -6  -5  -4  -3  -2  -1   0   1   2   3   4   5 6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23 24  25  26  27  28  29  30  31  32  33  34  35  36  37]

These numbers almost correctly represent the data.这些数字几乎正确地代表了数据。 In array [x], '-30' actually represents all numbers '<-29' and '37' represents all numbers '>=37'.在数组[x]中,'-30'实际上代表所有数字'<-29','37'代表所有数字'>=37'。 So I want to want to change the tick labels to represent this.所以我想改变刻度标签来表示这一点。 I tried我试过

ax.xaxis.set_major_locator(plt.MaxNLocator(15))
ax.set_xticklabels(['<-29','-25','-20', '-15', '-10', '-5', '0', '5', '10', '15', '20', '25', '30', '35', '>=37'],fontsize=11) 
ax.plot(x,ar_prob, c=cmap(0.6))
ax.set_title('Atmospheric River Landfall Probability',fontsize=16)
plt.grid(True)
ax.set_xlabel('Blocking Index (dam)',fontsize=15)
ax.set_ylabel('AR Landfall Probability',fontsize=15)
ax.set_ylim(0, 100)
plt.show()

在此处输入图片说明

The tickmarks I want are not plotting and the line is misplaced.我想要的刻度线没有绘制并且线放错了位置。 How do I fit the line to the correct tickmarks and add an irregular tickmark after 35 since the spacing changes?由于间距发生变化,如何将线拟合到正确的刻度线并在 35 之后添加不规则刻度线?

When setting the xticklabels, it helps to set also explicitely set the xticks.在设置 xticklabels 时,它有助于设置也显式设置 xticks。 That way they are always aligned, also when zooming.这样它们总是对齐的,在缩放时也是如此。

In the code below, first the tick positions are set as multiples of 5, include the very last position and don't put a tick at 35 as it would be too close.在下面的代码中,首先将刻度位置设置为 5 的倍数,包括最后一个位置,不要在 35 处放置刻度,因为它太接近了。

Then, the string for the labels are formatted, with a special format for the first and the last.然后,标签的字符串被格式化,第一个和最后一个使用特殊格式。 A unicode '≥' is used as it is shorter.使用 unicode '≥',因为它更短。

from matplotlib import pyplot as plt
import numpy as np

x = list(range(-30, 38))
ar_prob = np.random.uniform(0, 100, len(x))
fig, ax = plt.subplots()
ax.plot(x, ar_prob, c='crimson')
ticks = [i for i in x if i == x[-1] or (i % 5 == 0 and i < x[-1] - 3)]
tick_labels = [f'<{t}' if t == x[0] else f'≥{t}' if t == x[-1] else f'{t}' for t in ticks]
ax.set_xticks(ticks)
ax.set_xticklabels(tick_labels, fontsize=11)
ax.set_title('Atmospheric River Landfall Probability', fontsize=16)
ax.grid(True)
ax.set_xlabel('Blocking Index (dam)', fontsize=15)
ax.set_ylabel('AR Landfall Probability', fontsize=15)
ax.set_ylim(0, 100)
plt.show()

样地

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

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