在matplotlib中更改轴线的长度

时间:2015-04-15 12:38:06

标签: 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方法很好地绘制这个。

1 个答案:

答案 0 :(得分:4)

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

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