godaygo
godaygo

Reputation: 2288

Set tick labels in matplotlib

I'm trying to make some manipulations with tick labels in matplotlib. But it seems that code does not do what I want, and I have no idea why? There is no labels.

In[1]: import matplotlib.pyplot as plt
       import numpy as np

In[2]: x = np.arange(0,10)

In[3]: plt.plot(x,x)

       locs, labs = plt.xticks()
       plt.xticks(locs[1:], labs[1:])

       plt.show()

enter image description here

Any help please! What I want is to delete first label on x axis:
enter image description here
I'm using:
python 3.5.2
matplotlib 1.5.3
win 10

Upvotes: 3

Views: 29693

Answers (3)

Bill Bell
Bill Bell

Reputation: 21663

This is a direct answer to your question, rather than a remedy to your predicament.

>>> locs, labs = plt.xticks()
>>> locs
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.])
>>> labs
<a list of 10 Text xticklabel objects>

Notice that when you make a get call to xticks it yields a one-dimensional array of locations for tick marks and a list of objects. However, when you make a put call to xticks it expects an array-like collection of locations and an array-like collection of strings. For some reason, xticks seems to have failed to croak upon being given that list of objects.

Upvotes: 2

Julien Spronck
Julien Spronck

Reputation: 15433

fig, ax = plt.subplots()
ax.plot(x, x)
ticks = ax.get_xticks()
ax.set_xticks(ticks[1:])
plt.show()

Upvotes: 6

gold_cy
gold_cy

Reputation: 14236

Try this:

locs, labs = plt.xticks() 
plt.xticks(locs[1:]) 
plt.show()

Here you go

Upvotes: 4

Related Questions