标签:colorbar python matplotlib contourf
我正在尝试在matpllotlib contourf图上自定义颜色条.虽然我能够使用科学记数法,但我试图改变符号的基础 – 主要是因为我的刻度将在(-100,100)而不是(-10,10)的范围内.
例如,这会产生一个简单的情节……
import numpy as np
import matplotlib.pyplot as plt
z = (np.random.random((10,10)) - 0.5) * 0.2
fig, ax = plt.subplots()
plot = ax.contourf(z)
cbar = fig.colorbar(plot)
cbar.formatter.set_powerlimits((0, 0))
cbar.update_ticks()
plt.show()
像这样:
但是,我希望颜色条上方的标签为1e-2,数字范围为-10到10.
我该怎么做?
解决方法:
一个可能的解决方案可以是ScalarFormatter的子类并修复此问题中的数量级:Set scientific notation with fixed exponent and significant digits for multiple subplots
然后,您将调用此格式化程序,其数量级为参数顺序,OOMFormatter(-2,mathText = False). mathText设置为false以从问题中获取符号,即
将其设置为True时,将给出.
然后,您可以通过colorbar的format参数将格式化程序设置为colorbar.
import numpy as np; np.random.seed(0)
import matplotlib.pyplot as plt
import matplotlib.ticker
class OOMFormatter(matplotlib.ticker.ScalarFormatter):
def __init__(self, order=0, fformat="%1.1f", offset=True, mathText=True):
self.oom = order
self.fformat = fformat
matplotlib.ticker.ScalarFormatter.__init__(self,useOffset=offset,useMathText=mathText)
def _set_orderOfMagnitude(self, nothing):
self.orderOfMagnitude = self.oom
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.5) * 0.2
fig, ax = plt.subplots()
plot = ax.contourf(z)
cbar = fig.colorbar(plot, format=OOMFormatter(-2, mathText=False))
plt.show()
标签:colorbar,python,matplotlib,contourf 来源: https://codeday.me/bug/20190929/1831466.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。