python - 更改 matplotlib 中轴线的长度

标签 python matplotlib plot

我正在尝试更改 matplotlib 图轴的显示长度。这是我当前的代码:

import matplotlib.pyplot as plt
import numpy as np

linewidth = 2
outward = 10
ticklength = 4
tickwidth = 1

fig, ax = plt.subplots()

ax.plot(np.arange(100))

ax.tick_params(right="off",top="off",length = ticklength, width = tickwidth, direction = "out")
ax.spines["top"].set_visible(False), ax.spines["right"].set_visible(False)

for line in ["left","bottom"]:
    ax.spines[line].set_linewidth(linewidth)
    ax.spines[line].set_position(("outward",outward))

生成以下图:

enter image description here

我希望我的情节看起来像下面的轴线缩短:

enter image description here

我无法在 ax[axis].spines 方法中找到它。我也无法使用 ax.axhline 方法很好地绘制它。

最佳答案

您可以将这些行添加到代码的末尾:

ax.spines['left'].set_bounds(20, 80)
ax.spines['bottom'].set_bounds(20, 80)

for i in [0, -1]:
    ax.get_yticklabels()[i].set_visible(False)
    ax.get_xticklabels()[i].set_visible(False)

for i in [0, -2]:
    ax.get_yticklines()[i].set_visible(False)
    ax.get_xticklines()[i].set_visible(False)

得到这个:

enter image description here

关于python - 更改 matplotlib 中轴线的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29650451/

相关文章:

python - 如何使用 python 写入 ISO?

python - 如何在 windows/dos 上从 Python 中运行脚本?

python - 使用 qt 将绘图添加到选项卡

r - 将绘图文本/二进制写入变量

plot - 如何将 chartjs 作为 es6 模块导入?

python - Core Reporting API - 如何使用多个 dimensionFilterClauses 过滤器?

python - 如何向散点图添加图例?

python - 克服空数组的 ValueError

python - matplotlib从url : TypeError: a bytes-like object is required,而不是 'str'读取数据

python - `antialiased`中的 `matplotlib.collections`是什么,怎么给它设置参数?