matplotlib中的十六进制X轴

t2a7ltrp  于 2023-03-03  发布在  其他
关注(0)|答案(3)|浏览(204)

有没有可能在matplotlib中以十六进制表示法打印X轴上的值?
在我的图中,X轴代表内存地址。

rxztt3cl

rxztt3cl1#

可以在轴上设置格式设置程序,例如FormatStrFormatter。
简单举例:

  1. import matplotlib.pyplot as plt
  2. import matplotlib.ticker as ticker
  3. plt.plot([10, 20, 30], [1, 3, 2])
  4. axes = plt.gca()
  5. axes.get_xaxis().set_major_locator(ticker.MultipleLocator(1))
  6. axes.get_xaxis().set_major_formatter(ticker.FormatStrFormatter("%x"))
  7. plt.show()
relj7zay

relj7zay2#

在64位机器上使用python3.5时,由于类型不匹配而出现错误。

  1. TypeError: %x format: an integer is required, not numpy.float64

我通过使用一个函数格式化程序来绕过它,以便能够转换为整数。

  1. import matplotlib.pyplot as plt
  2. import matplotlib.ticker as ticker
  3. def to_hex(x, pos):
  4. return '%x' % int(x)
  5. fmt = ticker.FuncFormatter(to_hex)
  6. plt.plot([10, 20, 30], [1, 3, 2])
  7. axes = plt.gca()
  8. axes.get_xaxis().set_major_locator(ticker.MultipleLocator(1))
  9. axes.get_xaxis().set_major_formatter(fmt)
  10. plt.show()
展开查看全部
t1rydlwq

t1rydlwq3#

另一种方法是这样的:

  1. import matplotlib.pyplot as plt
  2. # Just some 'random' data
  3. x = sorted([489465, 49498, 5146, 4894, 64984, 465])
  4. y = list(range(len(x)))
  5. fig = plt.figure(figsize=(16, 4.5))
  6. ax = fig.gca()
  7. plt.plot(x, y, marker='o')
  8. # Create labels
  9. xlabels = map(lambda t: '0x%08X' % int(t), ax.get_xticks())
  10. ax.set_xticklabels(xlabels);

结果:

展开查看全部

相关问题