更新时间:2023-11-28 18:16:52
为了确保只有整数位置才能获得刻度标签,可以使用带有整数的matplotlib.ticker.MultipleLocator
作为参数.
In order to make sure only integer locations obtain a ticklabel, you may use a matplotlib.ticker.MultipleLocator
with an integer number as argument.
要在轴上格式化数字,可以使用matplotlib.ticker.StrMethodFormatter
.
To then format the numbers on the axes, you may use a matplotlib.ticker.StrMethodFormatter
.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker
df = pd.DataFrame({"FISCAL_YEAR" : np.arange(2000,2017),
'TOTAL' : np.random.rand(17)})
plt.bar(df['FISCAL_YEAR'],df['TOTAL'],align='center')
locator = matplotlib.ticker.MultipleLocator(2)
plt.gca().xaxis.set_major_locator(locator)
formatter = matplotlib.ticker.StrMethodFormatter("{x:.0f}")
plt.gca().xaxis.set_major_formatter(formatter)
plt.show()