目前,我的直方图的 x-tick 标签是 [200. 400. 600. 800. 1000. 1200. 1400.]。它代表美元汇率。我想在这些报价的前缀中添加 $,例如 [$200 $400 $600 $800 $1000 $1200 $1400]。
我试过这个 axs.xaxis.get_majorticklocs()
来获取 x-tick 标签和 axs.xaxis.set_ticks([f"${amount:.0f}"在axs[0, 1].xaxis.get_majorticklocs()])
设置更新的。我知道它会抛出错误,因为 x-tick 标签现在是字符串。
需要帮助!请协助我该怎么做。
最佳答案
您可以像这样使用自动 StrMethodFormatter
:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(100*np.random.rand(20))
# Use automatic StrMethodFormatter
ax.xaxis.set_major_formatter('${x:1.2f}')
plt.show()
关于python - 如何为已经存在的 x-ticks 标签 matplotlib 添加额外的符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73594284/