Log labels on colorbar matplotlib

淺唱寂寞╮ 提交于 2020-06-12 05:36:09

问题


I have a logarithmic imshow figure, and when the colorbar is created, its axis labels and ticks are logarithmic, but because of the short range (0-50) of the values, the colorbar looks like this:

enter image description here

and I would like it to instead show as 0, 5, 10, 20, 50 spaced along the axis (logarithmic spacing).

I can't seem to get this to work.

Thanks for any help.


回答1:


Use the LogFormatter class and set labelOnlyBase to False:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors
from matplotlib.ticker import LogFormatter 

A = np.random.rand(50,50)*50
plt.imshow(A, norm=matplotlib.colors.LogNorm())
formatter = LogFormatter(10, labelOnlyBase=False) 
cb = plt.colorbar(ticks=[1,5,10,20,50], format=formatter)
plt.show()


来源:https://stackoverflow.com/questions/27345005/log-labels-on-colorbar-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!