如何在matplotlib颜色栏中使用偏移符号更改尾数的位数 [英] How to change the the number of digits of the mantissa using offset notation in matplotlib colorbar
问题描述
我在matplotlib中有一个等高线图,使用的色标是由
创建的from mpl_toolkits.axes_grid1 import make_axes_locatable
divider = make_axes_locatable(axe) #adjust colorbar to fig height
cax = divider.append_axes("right", size=size, pad=pad)
cbar = f.colorbar(cf,cax=cax)
cbar.ax.yaxis.set_offset_position('left')
cbar.ax.tick_params(labelsize=17)#28
t = cbar.ax.yaxis.get_offset_text()
t.set_size(15)
如何更改显示在."后仅2位数字的色条刻度标签(指数尾数).而不是3(保持偏移符号)?是否有可能还是我必须手动设置刻度?谢谢
我尝试使用str格式化程序
cbar.ax.yaxis.set_major_formatter(FormatStrFormatter('%.2g'))
到目前为止,但这还没有达到我想要的结果.
问题在于,尽管FormatStrFormatter
允许精确设置格式,但在处理1e-7
偏移量的情况下却不能像1e-7
那样处理偏移量.问题.
另一方面,默认的ScalarFormatter
会自动选择其自己的格式,而不会让用户对其进行更改.尽管这是最可取的,但在这种情况下,我们要自己指定格式.
一种解决方案是将ScalarFormatter
子类化并重新实现其._set_format()
方法,类似于此答案. /p>
请注意,您希望"%.2f"
而不是"%.2g"
始终在小数点后显示2位数字.
import numpy as np; np.random.seed(0)
import matplotlib.pyplot as plt
import matplotlib.ticker
class FormatScalarFormatter(matplotlib.ticker.ScalarFormatter):
def __init__(self, fformat="%1.1f", offset=True, mathText=True):
self.fformat = fformat
matplotlib.ticker.ScalarFormatter.__init__(self,useOffset=offset,
useMathText=mathText)
def _set_format(self, vmin, vmax):
self.format = self.fformat
if self._useMathText:
self.format = '$%s$' % matplotlib.ticker._mathdefault(self.format)
z = (np.random.random((10,10))*0.35+0.735)*1.e-7
fig, ax = plt.subplots()
plot = ax.contourf(z, levels=np.linspace(0.735e-7,1.145e-7,10))
fmt = FormatScalarFormatter("%.2f")
cbar = fig.colorbar(plot,format=fmt)
plt.show()
I have a contour plot in matplotlib using a colorbar which is created by
from mpl_toolkits.axes_grid1 import make_axes_locatable
divider = make_axes_locatable(axe) #adjust colorbar to fig height
cax = divider.append_axes("right", size=size, pad=pad)
cbar = f.colorbar(cf,cax=cax)
cbar.ax.yaxis.set_offset_position('left')
cbar.ax.tick_params(labelsize=17)#28
t = cbar.ax.yaxis.get_offset_text()
t.set_size(15)
How can I change the colorbar ticklabels (mantissa of exponent) showing up with only 2 digits after the '.' instead of 3 (keeping the off set notation)? Is there a possibility or do I have to set the ticks manually? Thanks
I have tried to use the str formatter
cbar.ax.yaxis.set_major_formatter(FormatStrFormatter('%.2g'))
so far but this doesn't give me the desired result.
The problem is that while the FormatStrFormatter
allows to set the format precisely, it is not capable of handling offsets like the 1e-7
in the case from the question.
On the other hand the default ScalarFormatter
automatically selects its own format, without letting the user change it. While this is mostly desireable, in this case, we want to specify the format ourself.
A solution is to subclass the ScalarFormatter
and reimplement its ._set_format()
method, similar to this answer.
Note that you would want "%.2f"
instead of "%.2g"
to always show 2 digits after the decimal point.
import numpy as np; np.random.seed(0)
import matplotlib.pyplot as plt
import matplotlib.ticker
class FormatScalarFormatter(matplotlib.ticker.ScalarFormatter):
def __init__(self, fformat="%1.1f", offset=True, mathText=True):
self.fformat = fformat
matplotlib.ticker.ScalarFormatter.__init__(self,useOffset=offset,
useMathText=mathText)
def _set_format(self, vmin, vmax):
self.format = self.fformat
if self._useMathText:
self.format = '$%s$' % matplotlib.ticker._mathdefault(self.format)
z = (np.random.random((10,10))*0.35+0.735)*1.e-7
fig, ax = plt.subplots()
plot = ax.contourf(z, levels=np.linspace(0.735e-7,1.145e-7,10))
fmt = FormatScalarFormatter("%.2f")
cbar = fig.colorbar(plot,format=fmt)
plt.show()
这篇关于如何在matplotlib颜色栏中使用偏移符号更改尾数的位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!