在图中显示自定义刻度值

问题描述 投票:0回答:1

假设我已经绘制了一个图,并且在该图中有一个特定的点,我从x轴绘制垂直线。例如,这一点具有x值33.55。但是,我的滴答分离大概是10或20从0到100.所以基本上:有没有办法可以将这个单个自定义值添加到刻度轴,所以它与之前的所有其他值一起显示?

python matplotlib
1个回答
1
投票

使用np.append添加到刻度数组:

import numpy as np

from matplotlib import pyplot as plt

x = np.random.rand(100) * 100
y = np.random.rand(100) * 100

fig, ax = plt.subplots(figsize=(8, 6))

ax.scatter(x, y)

ax.set_xticks(np.append(ax.get_xticks(), 33.55))

enter image description here

请注意,如果您的绘图不够大,则刻度标签可能会重叠。

如果你想让新的滴答声“清除其轨道”,可以这么说:

special_value = 33.55
black_hole_radius = 10
new_ticks = [value for value in ax.get_xticks() if abs(value - special_value) > black_hole_radius] + [special_value]

ax.set_xticks(new_ticks)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.