我试图在Python 2.7中使用二进制标签而不是浮点值来标记我的图表的x轴。我尝试使用
我收到的错误信息是:
FormatStrFormatter('%b')
,根据由Python Software Foundation提供的文档,这应该可以工作。我还希望所有二进制字符串的长度都相同。我还参考了这个链接。我收到的错误信息是:
我已经尝试过以下方式:ValueError: unsupported format character 'b' (0x62) at index 1
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FormatStrFormatter
fig, ax = plt.subplots()
ax.yaxis.set_major_formatter(FormatStrFormatter('%b'))
ax.yaxis.set_ticks(np.arange(0, 110, 10))
x = np.arange(1, 10, 0.1)
plt.plot(x, x**2)
plt.show()
'%b' % 12
会抛出错误,而'%x'
和'%o'
则不会。你确定'%b'
是%格式规范的一部分吗? - FHTMitchell%b
格式,因为str.__mod__
函数不支持。看一下你错误信息的最后两行:return self.fmt % x ValueError: unsupported format character 'b' (0x62) at index 1
- FHTMitchell