Jan Jan -4 years ago 78
Python Question

Matplotlib: transform axis label [0,22,1] to not label from 0 but label only at odd numbers [1,3,5]

I have 22 samples on x-axis, the default label is 0,1,2,3,...,21 ([0,22,1]). I want the label to show only at the location of odd numbers. Is it possible to use np.arange? As I still need column 0 but without label.

If np.arange cannot do, what should I use?




Additional info

I tested

plt.xticks(np.arange((0,22,1), np.arange(1,22,2)))


but it changed the first column to be 1 and compressed everything to first 11 columns. I want it to leave space for even number and label only odds.

Answer Source

In your case you want the ticks' positions and the labels to be the same, so you need to set them to be the same. If you want odd numbers only, you may indeed use np.arange(1,22,2). The call in matplotlib would then be

plt.xticks(np.arange(1,22,2), np.arange(1,22,2))

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download