matplotlib中的科学表示法颜色条 [英] Scientific notation colorbar in matplotlib

查看:165
本文介绍了matplotlib中的科学表示法颜色条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用matplotlib在我的图像上放置一个颜色条.当我尝试强制使用科学计数法标记刻度标签时,就会出现问题.如何在颜色栏的刻度中强制使用科学计数法(即1x10 ^ 0、2x10 ^ 0,...,1x10 ^ 2等)?

I am trying to put a colorbar to my image using matplotlib. The issue comes when I try to force the ticklabels to be written in scientific notation. How can I force the scientific notation (ie, 1x10^0, 2x10^0, ..., 1x10^2, and so on) in the ticks of the color bar?

示例,让我们使用其颜色栏创建和绘制图像:

Example, let's create and plot and image with its color bar:

import matplotlib as plot
import numpy as np

img = np.random.randn(300,300)
myplot = plt.imshow(img)
plt.colorbar(myplot)
plt.show()

当我这样做时,我得到以下图像:

When I do this, I get the following image:

但是,我希望看到科学记号的刻度线标签.是否有任何一行命令可以执行此操作?否则,那里有任何提示吗?谢谢!

However, I would like to see the ticklabels in scientific notation... Is there any one line command to do this? Otherwise, is there any hint out there? Thanks!

推荐答案

您可以使用

You could use colorbar's format parameter:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as ticker

img = np.random.randn(300,300)
myplot = plt.imshow(img)

def fmt(x, pos):
    a, b = '{:.2e}'.format(x).split('e')
    b = int(b)
    return r'${} \times 10^{{{}}}$'.format(a, b)

plt.colorbar(myplot, format=ticker.FuncFormatter(fmt))
plt.show()

这篇关于matplotlib中的科学表示法颜色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆