matplotlib colorbar:需要在bar顶部使用指数强制科学记数法

问题描述 投票:-1回答:1

使用matplotlib颜色条,通常数字将以科学记数法打印,只有尾巴显示在条形图的一侧,并且单个指数位于条形图的顶部。我经常得到这种格式,即使我不想要它。现在,我真的需要它,因为我用十进制表示法绘制六个零的小数字,突然matplotlib决定它想要以十进制格式而不是科学格式打印它的数字。有没有办法强制它使用科学记数法与条形顶部的单个指数?

matplotlib colorbar scientific-notation
1个回答
0
投票

找到了。

colorbar有一个可选的format参数。您可以使用简单的文本参数指定简单的科学记数法或十进制格式。您还可以提供ScalarFormatter对象作为参数。 ScalarFormatter对象具有set_powerlimits(min,max)函数。如果它调用该函数,则任何小于10 ^ min或大于10 ^ max的数字将以科学计数法表示。如果您的colorbar值的整个范围小于10 ^ min或大于10 ^ max,则您的颜色条将按照OP中的要求显示结果:科学记数法,条形图两侧只有尾数,而单个指数位于最佳。对于我的情况,colorbar值都在10 ^ -6的数量级,我做了:

import matplotlib.ticker                         # here's where the formatter is
cbformat = matplotlib.ticker.ScalarFormatter()   # create the formatter
cbformat.set_powerlimits((-2,2))                 # set the limits for sci. not.

#  do whatever plotting you have to do
fig = plt.figure()
ax1 =   # create some sort of axis on fig
plot1 = ax1....   # do your plotting here ... plot, contour, contourf, whatever
# now add colorbar with your created formatter as an argument
fig.colorbar(plot1, ax=ax1, format=cbformat)
© www.soinside.com 2019 - 2024. All rights reserved.