简体   繁体   中英

How to get the logscaled colobar from imshow with contourf?

I am trying to use contourf with matplotlib and I want my contourf colobar to have the same shape as the imshow colorbar has with all the ticks that a logscale would normally have.

Does matplotlib allows that kind of "transformation" ?

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import LogNorm


A = np.random.uniform(low=10e-3, high=10e3, size=(100,100))

fig, ax = plt.subplots()
im = ax.imshow(A, interpolation='bilinear', cmap=cm.jet, norm=LogNorm())
plt.colorbar(im)

在此处输入图片说明

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import LogNorm


A = np.random.uniform(low=10e-3, high=10e3, size=(100,100))

fig, ax = plt.subplots()
ctf = ax.contourf(A, 100, cmap=cm.jet, norm=LogNorm())
plt.colorbar(ctf)

在此处输入图片说明

The colorbar of a contour plot should of course show the actual levels of the contours. One would need to set the levels manually and then specify a respective ticker.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import LogNorm
import matplotlib.ticker as mticker


A = np.sort(np.logspace(-3, 3, num=100*100)).reshape(100,100)

levels = np.logspace(-3,3,num=101)

fig, ax = plt.subplots()

levs = np.power(10, np.arange(-3., 4., 0.1))
cntr = ax.contourf(A, cmap=cm.jet, levels=levs, norm=LogNorm())

cb = plt.colorbar(cntr, ticks=mticker.LogLocator())
plt.show()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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