标题是不言自明的,找不到如何实现它。对于轴刻度格式类似的命令看起来像这样:ax.ticklabel_format(useMathText=True)
这个没有问题,它工作。但是对于颜色条的刻度,使它们以MathText格式显示,我无法找到如何实现它。我尝试将useMathText=True
作为arg传递到cbar.ax.tick_params()
和cbar = plt.colorbar()
中但这并不奏效。
重新创建:
import numpy as np
import matplotlib.tri as mtri
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
p = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [0.5, 0.5]])
e = np.array([[0, 1, 4], [3, 0, 4], [1, 2, 4], [2, 3, 4]])
t = mtri.Triangulation(p[:, 0], p[:, 1], e)
z = np.random.rand(5)
fig, ax = plt.subplots()
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
ax.set_aspect('equal', 'box')
ax.set(xlim=(min(p[:, 0]), max(p[:, 0])), ylim=(min(p[:, 1]), max(p[:, 1])))
c = ax.tricontourf(t, z, 10, cmap='plasma', vmin=np.min(z), vmax=np.max(z),
levels=np.linspace(np.min(z), np.max(z), 100))
ax.triplot(t, color='white', lw=0.1)
ax.set_title('Title', fontsize=16)
cbar = plt.colorbar(c, cax=cax, format='%.0e', ticks=np.linspace(np.min(z), np.max(z), 3))
cbar.set_label('Colorbar title', fontsize=16)
# cbar.ax.ticklabel_format(useMathText=True)
ax.ticklabel_format(useMathText=True)
plt.show()
1条答案
按热度按时间6ioyuze21#
正如Ernest在评论中解释的那样,轮廓的颜色条得到
FixedFormatter
,它不接受useMathText
。在我的版本中,代码没有给予错误,但正如你指出的那样,也没有生成所需的输出。因此,答案是将格式化程序更改为ScalarFormatter,同时告诉它使用数学文本。垂直颜色条的格式化程序可以通过
cbar.ax.yaxis.set_major_formatter
设置。请让我知道它现在是否适合你。(我的系统,现在与matplotlib 3.2.1,Pycharm 2019.3.4在Windows下。)