Michal
Michal

Reputation: 2083

Hide axis lines and labels in matplotlib except min/max y-label

I have plot in Python matplotlib:

from matplotlib import pyplot as plt

data = [1, 5, 2, 1, 1, 4, 3, 1, 7, 8, 9, 6, 1]
plt.plot(data)

Is there any way to hide axis lines and labels except min and max y-axis label? I know about plt.axis("off"), however I would like to show first and last label on y-axis.

The current plot is on the left of the image below and desired plot on the right.

enter image description here

Upvotes: 5

Views: 3609

Answers (3)

Tom
Tom

Reputation: 8800

Here you format the borders and ticks to hide them and then set the y-ticks explicitly:

from matplotlib import pyplot as plt

data = [1, 5, 2, 1, 1, 4, 3, 1, 7, 8, 9, 6, 1]

fig, ax = plt.subplots()
ax.plot(data)
for side in ['top','right','bottom','left']:
    ax.spines[side].set_visible(False)
ax.tick_params(axis='both',which='both',labelbottom=False,bottom=False,left=False)
ax.set_yticks([min(y),max(y)]) 

enter image description here

Upvotes: 6

ipj
ipj

Reputation: 3598

Try:

from matplotlib import pyplot as plt
data = [1, 5, 2, 1, 1, 4, 3, 1, 7, 8, 9, 6, 1]
plt.box(False)
plt.yticks(ticks= [min(data),max(data)])
plt.xticks(ticks= [])
plt.plot(data)

plt.box(False) is used to remove plot frame.

enter image description here

Please note that ticks for 1 and 9 exists on chart in this approach.

Upvotes: 3

r-beginners
r-beginners

Reputation: 35205

There may be a better way, but you can remove it with plt.axis('off'). Added in text from.

from matplotlib import pyplot as plt

data = [1, 5, 2, 1, 1, 4, 3, 1, 7, 8, 9, 6, 1]
plt.plot(data)
plt.axis('off')
plt.text(-0.5,9.0,'9')
plt.text(-0.5,1.0,'1')

enter image description here

Upvotes: 4

Related Questions