Matplotlib从数据单元到轴单位的转换

时间:2014-09-05 16:16:21

标签: python matplotlib plot

我试图用Matplotlib(Python库)绘制一些数据并添加一条水平线,它不会覆盖整个轴范围,而是从中间开始并在右轴上完成。

我正在使用:

plt.axhline(y=1.75,xmin=0.5)

其中y以数据单位指定行的高度,但xmin(以及xmax)需要以轴为单位定义(对于轴的开头= 0,在结束时= 1)。虽然我只知道我希望我的行以数据单位开始,但是有一个方法/函数可以从一个转换成另一个吗?

1 个答案:

答案 0 :(得分:2)

只需用plt

画一条线
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0.2,10,100)
fig, ax = plt.subplots()
ax.plot(x, 1/x)
ax.plot(x, np.log(x))
ax.set_aspect('equal')
ax.grid(True, which='both')

y = 1.25
xmin = 2
xmax = ax.get_xlim()[1]
ax.plot([xmin, xmax], [y, y], color='k')

给了我:

Example Plot

相关问题