user1124702
user1124702

Reputation: 1135

Plotting in Python matplotlib with interleaved xticks

This question is regarding plotting in matplotlib with xticks - Python.

Lets say I have three vectors:

X = [ 1,2,3,4,5 ] with corresponding labels LVEC = [a,b,c,d,e] Y = [1,2,3,4,5]

I would like to plot X,Y but provide interleaved xticks from LVEC (ie., only a,c,e labels show on the plot). The graph should, however, consist of all the five data points. Is this possible natively?

Upvotes: 0

Views: 244

Answers (2)

Bart
Bart

Reputation: 10278

This should work:

import matplotlib.pylab as pl

X = [ 1,2,3,4,5 ] 
LVEC = ['a','b','c','d','e'] 
Y = [1,2,3,4,5]

pl.figure()
ax=pl.subplot(111)  # or pl.gca() or similar
pl.plot(X, Y, '-x')
ax.set_xticks([X[0],X[2],X[4]])
ax.set_xticklabels([LVEC[0],LVEC[2],LVEC[4]])

So the main trick is to first specify the x-locations where the labels should occur with set_xticks, and next the overwrite the labels with set_xticklabels.

Upvotes: 1

JGrSmi
JGrSmi

Reputation: 49

If I understand correctly, this should give you the effect you want.

fig, ax = plt.subplots()
ax.scatter(X,Y, c='white',edgecolors='None')
for i, txt in enumerate(LVEC):
    ax.annotate(txt, (X[i],Y[i]))

Upvotes: 0

Related Questions