python - 将水平线延伸至图形边缘

标签 python matplotlib

在下面的代码中,我获得了一条在绘图的“框”内延伸的水平线(轴对象):

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)

这给出了下图: enter image description here

有没有办法将这条水平线延伸到整个绘图/图形? (换句话说,越过了情节的“盒子”)。

最佳答案

您可以将 plt.axhline 中的 xminxmax 参数设置为 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)

enter image description here

请注意,axhline 函数的文档字符串确实建议不要将这些值设置在 01 范围之外:

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/

相关文章:

python - 有条件地定义属性(使用装饰器)(例如 try/except?)

python - Matplotlib 中轴上的数字和轴标签具有相同的字体

python - 将 2x2 DataFrame 整理成 4x1 系列 (Pandas)

Matplotlib:在图表上的每个点旁边显示值

python - 将两个二维数组组合成一个图像图

python - 更改 matplotlib 中错误栏的 capstyle

Python subprocess.check_call 的工作方式与 bash 不同

python - 如果它是通过 query_property 创建的,是否可以缩小 SQLAlchemy 中生成的查询的范围?

python - 列表上的循环操作仅对第一项无法正确执行

python - 您可以使用 pandas 在一行中从 groupby 对象创建新列吗?