Reputation: 35
i have this code in python, its not change coordinate y , plt.yticks instruction not change, any help?
import matplotlib.pyplot as plt
year = [1927, 1934, 1947, 1957, 1965, 1977, 1987, 1997]
population = [2968540, 3213174, 4816185, 6536109, 8261527, 12497000,16635199,19184543]
plt.plot(year,population,color="red")
plt.title("Iraq Population 1927-1997")
plt.xlabel('Year')
plt.ylabel("Population")
plt.yticks([0, 4, 8, 12, 16, 20], ["0m", "4m", "8m", "12m", "16m", "20m"])
plt.grid()
plt.show()
Upvotes: 0
Views: 104
Reputation: 339470
This is the typical usecase of a matplotlib.ticker.EngFormatter
.
import matplotlib.pyplot as plt
from matplotlib.ticker import EngFormatter
year = [1927, 1934, 1947, 1957, 1965, 1977, 1987, 1997]
population = [2968540, 3213174, 4816185, 6536109, 8261527, 12497000,16635199,19184543]
plt.plot(year,population,color="red")
plt.title("Iraq Population 1927-1997")
plt.xlabel('Year')
plt.ylabel("Population")
plt.gca().yaxis.set_major_formatter(EngFormatter())
plt.grid()
plt.show()
Note that EngFormatter uses a capital M for million, as this is the standard in the SI system.
Upvotes: 1
Reputation: 10880
You have millions of people on the y-axis but want ticks at 0, 4, 8, 12, 16
and 20
.
I suggest to try
plt.yticks([0e6, 4e6, 8e6, 12e6, 16e6, 20e6], ["0m", "4m", "8m", "12m", "16m", "20m"])
Upvotes: 2