简体   繁体   English

Matplotlib Colorbar科学记数法偏移

[英]Matplotlib Colorbar scientific notation offset

When plotting a colorbar, the top label (I guess this would be called the offset) is mis-centred. 在绘制颜色条时,顶部标签(我猜这将被称为偏移)是错误居中的。 This didn't use to happen, I have examples of old code where it was centred above the colorbar, but I have no clue what has changed. 这并没有发生,我有旧代码的示例,它位于颜色条上方,但我不知道发生了什么变化。

Example: 例:

import numpy as np
import matplotlib.pyplot as plt

z = np.random.random((10,10))

fig, ax = plt.subplots()
im = ax.imshow(z)
cb = fig.colorbar(im)

cb.formatter.set_powerlimits((0, 0))
cb.update_ticks()

plt.show()

Gives this: 给出这个:

偏移颜色条表示法

As an example of how it used to look (taken from one of my old papers, so different data etc.) 作为它过去常见的一个例子(取自我的一篇旧论文,如此不同的数据等)

正确的颜色条符号

Using the most recent anaconda python 2.7, on MacOSX, mpl version 1.5.0 使用最新的anaconda python 2.7,在MacOSX上,mpl版本1.5.0

Edit: I should also note, tight_layout() does not improve this either, though it is missing from the working example. 编辑:我还应该注意,tight_layout()也没有改善这一点,尽管工作示例中缺少它。

You can simply use set_offset_position for the y-axis of the colorbar. 您可以简单地使用set_offset_position作为set_offset_position的y轴。 Compare: 相比:

fig, ax = plt.subplots()                
im = ax.imshow(np.random.random((10,10)))                    
cb = fig.colorbar(im)     
cb.formatter.set_powerlimits((0, 0))
cb.ax.yaxis.set_offset_position('right')                         
cb.update_ticks()
plt.show()

在此输入图像描述

versus

fig, ax = plt.subplots()
im = ax.imshow(np.random.random((10,10)))
cb = fig.colorbar(im)
cb.formatter.set_powerlimits((0, 0))
cb.ax.yaxis.set_offset_position('left')
cb.update_ticks()
plt.show()

在此输入图像描述

All in all, it simply looks like the default has changed from right to left. 总而言之,它看起来就像默认值从右向左变化。

Using your above code and matplotlib version 1.4.3 I get the following plot 使用上面的代码和matplotlib 1.4.3版本,我得到以下图表 在此输入图像描述

So this may be a version issue. 所以这可能是一个版本问题。 One possible work around could be to use cb.ax.text() 一个可能的解决方法是使用cb.ax.text()

# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

z = np.random.random((10,10))

fig, ax = plt.subplots()
im = ax.imshow(z)
cb = fig.colorbar(im)
cb.ax.text(-0.25, 1, r'$\times$10$^{-1}$', va='bottom', ha='left')

plt.show()

This way you have more control over the centring. 这样您就可以更好地控制居中。 The above code gives me the following plot 上面的代码给出了以下图表 在此输入图像描述 Note that I use an r at the start of the string so that $\\times$ produces the correct symbol. 请注意,我在字符串的开头使用r ,以便$\\times$生成正确的符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM