我想在pylab中创建一个在y轴上显示英尺的图,并将英尺细分为英寸而不是一英尺的分数。这在公制系统中不是问题,因为单位细分与十进制系统对齐,但在使用英制单位时确实难以阅读。
这可能吗?
我现在拥有的:
40 ft |
39.90 |
39.80 |
39.70 |
39.60 |
39.50 |------------>
我想要的是什么:
40 ft |
39 11in |
39 10in |
39 9in |
39 8in |
39 7in |
39 6in |------------>
答案 0 :(得分:4)
您可以使用ticker.FuncFormatter
创建custom tick label:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
x = np.linspace(0, 1, 100)
y = (np.random.random(100) - 0.5).cumsum()
fig, ax = plt.subplots()
ax.plot(x, y)
def imperial(x, pos):
ft, inches = divmod(round(x*12), 12)
ft, inches = map(int, [ft, inches])
return ('{} ft'.format(ft) if not inches
else '{} {} in'.format(ft, inches) if ft
else '{} in'.format(inches))
ax.yaxis.set_major_formatter(ticker.FuncFormatter(imperial))
plt.show()
要控制刻度线的位置,您可以使用ticker.MultipleLocator
。
例如,要每隔4英寸放置一个刻度线,请添加
loc = ticker.MultipleLocator(4./12)
ax.yaxis.set_major_locator(loc)