Changing centerline lengths in matplotlib

I am trying to change the displayed length of the axis of a matplotlib plot. This is my current code:

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))

What generates the following chart:

enter image description here

I would like my plot to look like this: abbreviated axis:

enter image description here

I could not find this in the method ax[axis].spines. I also could not build it beautifully using the method ax.axhline.

+4
source share
1 answer

You can add these lines at the end of your code:

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)

To get the following:

enter image description here

+3
source

All Articles