'What code should I add to make x start at 1?
I tried to make graph with matplotlib, and my graph should start from 1 not 0, But I don't know how to do. I can do this by writing all figures in x_coords and y_coords, but I want to know the code I can use while I use f = open(). Sorry that I'm not fluent in English
import matplotlib.pyplot as plt
import numpy as np
avg_prices = []
labels = (i for i in range(1, 53))
f = open('C:/1994_Weekly_Gas_Averages.txt', 'r')
for line in f:
avg_prices.append(float(line))
plt.plot(avg_prices, 'o--', label=labels)
plt.title('1994 Weekly Gas Prices')
plt.xlabel('Weeks')
plt.ylabel('Average prices')
plt.grid()
plt.xticks(np.arange(0, 60, 10))
plt.yticks(np.arange(1, 1.17, 0.025))
plt.show()
Solution 1:[1]
Just include the corresponding x coordinate in before the y coordinate.
x = list(range(1,53))
plt.plot(x, avg_prices)
Solution 2:[2]
labels = (i for i in range(0, 53)) should work. range function will start from the first parameter and run al the way till the n-1 of the second parameter.
labels = (i for i in range(0, 53)) will give labels from 0.....52
labels = (i for i in range(1, 53)) will give labels from 1.....52
labels = (i for i in range(1, 54)) will give labels from 1.....53
labels = (i for i in range(0, 54)) will give labels from 0.....53
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jacob |
| Solution 2 | Sathya |
