这是我的例子:
import numpy as np
import matplotlib.pyplot as plt
a1 = np.random.randn(10000)
b1 = np.random.randn(10000)
a2 = np.random.randn(5000)
b2 = np.random.randn(5000)
fig, axs = plt.subplots(1, 2, figsize=(9, 4))
ax = axs[0]
m = ax.hist2d(a1, b1, bins=30)
fig.colorbar(m[3], ax=ax)
ax = axs[1]
m = ax.hist2d(a2, b2, bins=30)
fig.colorbar(m[3], ax=ax)
plt.tight_layout()
我的问题是如何更改正确图形的颜色范围,以使其最大颜色值为 115
而不是 {{1} } ? (注意:不仅更改颜色条的值,相同颜色值的颜色应与左图相同。)
我尝试这样设置cmax关键字:
66
但是结果没有改变。那我该怎么办?
答案 0 :(得分:2)
您要显式添加vmin和vmax范围以获得所需的结果。 回答您的问题-
import numpy as np
import matplotlib.pyplot as plt
a1 = np.random.randn(10000)
b1 = np.random.randn(10000)
a2 = np.random.randn(5000)
b2 = np.random.randn(5000)
fig, axs = plt.subplots(1, 2, figsize=(9, 4))
ax = axs[0]
m = ax.hist2d(a1, b1, bins=30)
fig.colorbar(m[3], ax=ax)
ax = axs[1]
m = ax.hist2d(a2, b2, bins=30, vmax = 115) # Added Vmax here.
fig.colorbar(m[3], ax=ax)
plt.tight_layout()