Issue
How do I start plotting my values at position 1 on the x-axis rather than 0? All the solutions I have seen for this have been pretty complex, I think I must be overlooking a simple way to specify this.
import matplotlib.pyplot as plt
def plot_vals(vals, RANGE):
plt.grid()
plt.xticks(RANGE)
plt.plot(vals, 'ro-')
RANGE = range(0, 11)
vals = [600.0, 222.3, 139.8, 114.0, 90.8, 80.8, 70.7, 62.8, 55.5, 47.5]
plot_vals(vals, RANGE)
yields this graph
All I want is x to run from 1 - 10 (instead 0 to 9) while keeping the all the values to be plotted.
Solution
To ensure this, you can pass a range
for the x
argument of plt.plot
that runs from 1 to length of the values:
plt.plot(range(1, len(vals) + 1), vals)
which gives this:
to see them in 1..10 fashion, we use plt.xticks
with the same range:
plt.xticks(range(1, len(vals) + 1))
to get
overall:
x_range = range(1, len(vals) + 1)
plt.plot(x_range, vals)
plt.xticks(x_range)
(with the OOP style, last two lines' functions are ax.plot
and ax.set_xticks
)
Answered By - Mustafa Aydın
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.