【发布时间】:2020-06-22 00:35:09
【问题描述】:
简而言之,我想要右边的版本,而不是左边的版本。有什么方法可以做到这一点而不必画出拳头?您之前可以访问艺术家,但此时尚未设置文本。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.text import Text
image = np.random.uniform(10000000, 100000000, (100, 100))
fig, ax = plt.subplots()
image_artist = ax.imshow(image)
colorbar = fig.colorbar(image_artist)
colorbar.ax.ticklabel_format()
fig.show()
for artist in colorbar.ax.yaxis.get_children():
if isinstance(artist, Text) and artist.get_text():
exp = artist.get_text().split('e')[1].replace('+', '')
colorbar.ax.set_ylabel(rf'Parameter [U${{\times}}10^{{{exp}}}$]')
artist.set_visible(False)
fig.show()
【问题讨论】:
-
您可以通过
get_offset_text()访问偏移文本 -
另见Adjust exponent text after setting scientific limits on matplotlib axis。有
plt.tight_layout()建议强制填写offset_text。此后,您将获得所需的艺术家为offset_text = colorbar.ax.yaxis.get_offset_text() -
太好了,这基本上就是我想要的。尽管这些事情的记录如此糟糕,但真是太可惜了。你怎么知道
ticklabel_format生成的额外文本被传递给offset_text?无论如何..如果您使用constrained_layout=True,您对如何执行此操作有任何想法吗? -
您也可以在绘图前将数据除以 10^6 或 10^7,然后不必担心偏移文本,只需将该信息添加到标签中即可。
-
谢谢@PaulBrodersen,我也想过,但不幸的是,我不知道数量级,而且很难确定,因为对于
colorbar,它直接依赖于图像的clim。
标签: python matplotlib