teratoulis
teratoulis

Reputation: 67

fix xticks starting from 1 in python with matplotlib

I am trying to put xticks in a plt figure. However, ticks start from 0 while I want to start from 1 how can I fix this? I tried this (based on a similar question I found) but still nothing.

import numpy as np
import matplotlib.pyplot as plt

y= np.array([[1], [2], [0.5], [1.4],[5], [4], [3.5], [1.2]])
plt.figure(1)
plt.plot(y, marker="o")
x_ticks = np.arange(1, len(y), 5)
plt.xticks(x_ticks)

thanks a lot!

Upvotes: 1

Views: 1307

Answers (1)

Zephyr
Zephyr

Reputation: 12496

You can specify a x array to be used to draw the plot:

x = np.arange(1, len(y) + 1, 1)

Complete Code

import numpy as np
import matplotlib.pyplot as plt

y= np.array([[1], [2], [0.5], [1.4],[5], [4], [3.5], [1.2]])
x = np.arange(1, len(y) + 1, 1)

plt.figure(1)
plt.plot(x, y, marker="o")

plt.show()

enter image description here

Upvotes: 1

Related Questions