axes.hlines会更改xlim,尽管使用了axes坐标

2024-09-30 20:34:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在我的轴范围之外添加一条小线,我希望在以后的阶段将其用作高度定制的图例。但是,使用axes.hlines会更改轴的xlim,即使我指定了transform = axes.transAxes。xlim的设置似乎使hline的坐标包含在datacoordinate范围内。只是,这些坐标是轴坐标,而不是数据坐标

下面是一个简单的工作示例:

import numpy as np
import matplotlib.pyplot as plt

x_data = np.random.rand(10)+10
y_data = np.random.rand(10)

fig, ax = plt.subplots()

ax.scatter(x_data,y_data)
ax.hlines(0.5,1.1,1.2, transform = ax.transAxes, clip_on = False)

通过ax.hlines命令更改XLIM的结果:

enter image description here

ax.hlines被注释掉时,您会得到:

enter image description here


Tags: importdataasnptransformpltrandomax