Aga*_*'lo 8 python label matplotlib colormap
此代码使我能够绘制“3d”数组 [X,Y,Z] 的颜色图(它们是 3 个简单的 np.array 元素)。但是我无法在颜色条图例的右侧添加垂直书写标签。
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure("Color MAP 2D+")
contour = plt.tricontourf(X, Y, Z, 100, cmap="bwr")
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Color MAP 2D+")
#Legend
def fmt(x, pos):
a, b = '{:.2e}'.format(x).split('e')
b = int(b)
return r'${} \times 10^{{{}}}$'.format(a, b)
import matplotlib.ticker as ticker
plt.colorbar(contour, format=ticker.FuncFormatter(fmt))
plt.show()
Run Code Online (Sandbox Code Playgroud)
不能从谷歌得到一个简单的答案是很烦人的……有人可以帮我吗?
tmd*_*son 13
您正在寻找一个添加label到colorbar对象。谢天谢地,colorbar有一个set_label功能。
简而言之:
cbar = plt.colorbar(contour, format=ticker.FuncFormatter(fmt))
cbar.set_label('your label here')
Run Code Online (Sandbox Code Playgroud)
在一个最小的脚本中:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
X = np.random.uniform(-2, 2, 200)
Y = np.random.uniform(-2, 2, 200)
Z = X*np.exp(-X**2 - Y**2)
contour = plt.tricontourf(X, Y, Z, 100, cmap="bwr")
def fmt(x, pos):
a, b = '{:.2e}'.format(x).split('e')
b = int(b)
return r'${} \times 10^{{{}}}$'.format(a, b)
cbar = plt.colorbar(contour, format=ticker.FuncFormatter(fmt))
cbar.set_label('your label here')
plt.show()
Run Code Online (Sandbox Code Playgroud)