1
我想在颜色条的顶部放置一个标签。绘制标签工作正常,但颜色条轴的最终位置调整不起作用。似乎对“set_position”的调用根本不起作用。这里是我的代码:调整matplotlib颜色条的位置
import numpy as np
import matplotlib.pyplot as plt
# Plot some sample data.
X, Y = np.meshgrid(np.arange(1, 10, 0.1), np.arange(1, 10, 0.1))
Z = np.sin(X) ** 2 + np.cos(Y) ** 2
plt.pcolor(X, Y, Z)
# Add the colorbar.
cb = plt.colorbar(shrink=0.8)
cax = cb.ax
# Add label on top of colorbar.
cb.ax.set_xlabel("mylabel")
cb.ax.xaxis.set_label_position('top')
cb.ax.xaxis.set_label_coords(1.8, 1.05)
# Adjust colorbar position (NOT working).
pos1 = cax.get_position()
yshift = pos1.height * 0.05/0.8
pos2 = [pos1.x0, pos1.y0 - yshift, pos1.width, pos1.height]
cax.set_position(pos2)
# Adjust and show the plot.
plt.tight_layout()
plt.show()
我在做什么错?
你对第二点有所了解;它确实是恢复手动定位的'plt.tight_layout()'调用。 – emher