在下面的代码中,我获得了一条在绘图的“框”内延伸的水平线(轴对象):
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fig.patch.set_facecolor('white')
fig.patch.set_alpha(1.0)
plt.axhline(y = 155, clip_on = False)
有没有办法将这条水平线延伸到整个绘图/图形? (换句话说,越过了情节的“盒子”)。
最佳答案
您可以将 plt.axhline
中的 xmin
和 xmax
参数设置为 0.0
之外1.0
范围。
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fig.patch.set_facecolor('white')
fig.patch.set_alpha(1.0)
plt.axhline(y = 155, xmin=-.1, xmax=1.1, clip_on = False)
请注意,axhline
函数的文档字符串确实建议不要将这些值设置在 0
到 1
范围之外:
xmin : scalar, optional, default: 0 Should be between 0 and 1, 0 being the far left of the plot, 1 the far right of the plot.
xmax : scalar, optional, default: 1 Should be between 0 and 1, 0 being the far left of the plot, 1 the far right of the plot.
所以,随心所欲。它可能会在将来引起问题,但至少现在是有效的。 (请注意,我使用的是 matplotlib 版本 1.5.1 和 2.0.0,两者都可以工作)
关于python - 将水平线延伸至图形边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44097945/