如何绘制次要网格为1,主网格为10,xticklabel增量为 20个单位的图表?
这是我的示例代码,输出的xticklabels每10个单位递增:
plt.figure(figsize=(10, 10))
ax = plt.gca()
major_ticks = np.arange(0, 60, 10)
minor_ticks = np.arange(0, 60, 1)
ax.set_xticks(major_ticks)
ax.set_xticks(minor_ticks, minor=True)
ax.set_yticks(major_ticks)
ax.set_yticks(minor_ticks, minor=True)
ax.grid(which='major')
ax.grid(which='minor', alpha=0.5)
ax.set_aspect('equal')
但是随后我想以20而不是10的增量显示xticklabels和yticklabels,如下所示:
任何想法如何实现这一目标?谢谢
答案 0 :(得分:3)
只需在代码末尾添加以下4行:您只需隐藏第二个主要的刻度线标签。满足您的需求就差不多了。 [1::2]
索引意味着从第二个索引开始,然后从第二个索引中获取第二个元素。我必须从第二个索引开始,因为第一个索引的刻度标签是0,您不想删除。
编辑:如果只想在带有刻度标签的位置显示主要刻度,则可以进行以下修改(用箭头<---
标记)。您可能会发现official docs有用。
没有大刻度线但有小刻度线的图
# Hiding for x-axis
for t in ax.xaxis.get_major_ticks()[1::2]:
t.label.set_visible(False)
t.tick1On = False # <----
# Hiding for y-axis
for t in ax.yaxis.get_major_ticks()[1::2]:
t.label.set_visible(False)
t.tick1On = False # <----
没有次要和主要刻度线的图
如果您还想隐藏小刻度线,除了上面的代码外,还可以执行以下操作
for t in ax.xaxis.get_minor_ticks():
t.tick1On = False
# t._apply_params(width=0) # suggested by Jayjayyy
for t in ax.yaxis.get_minor_ticks():
t.tick1On = False
# t._apply_params(width=0) # suggested by Jayjayyy
建议避免所有for循环的直接方法
ax.tick_params(axis='both', which='minor', width=0)
答案 1 :(得分:1)
更新:
看起来更好一些:
NSUUID *animationUUID = [[NSUUID alloc] init];
self.currentAnimationUUID = animationUUID;
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
// set properties to animate
} completionHandler:^{
BOOL isCancelled = ![self.currentAnimationUUID isEqual:animationUUID];
if (isCancelled) {
return;
}
self.currentAnimationUUID = nil;
// do stuff
}];
我认为您必须手动将第二个标签设置为let animationUUID = UUID()
currentAnimationUUID = animationUUID
NSAnimationContext.runAnimationGroup({ context in
// set properties to animate
}, completionHandler: {
let isCancelled = self.currentAnimationUUID != animationUUID
guard !isCancelled else {
return
}
self.currentAnimationUUID = nil
// do stuff
})
:
ax.set_xticklabels(['' if i % 2 else l for i, l in enumerate(ax.get_xticks())])
ax.set_yticklabels(['' if i % 2 else l for i, l in enumerate(ax.get_yticks())])
和情节: