放置颜色栏 [英] Positioning the colorbar
问题描述
我有一个matplotlib图,并附有一个颜色条.我想将颜色条定位为水平,并位于绘图下方.
I have a matplotlib plot with a colorbar attached. I want to position the colorbar so that it is horizontal, and underneath my plot.
我几乎可以通过以下方式做到这一点:
I have almost done this via the following:
plt.colorbar(orientation="horizontal",fraction=0.07,anchor=(1.0,0.0))
但是颜色条仍与图略有重叠(以及x轴的标签).我想进一步将颜色栏下移,但我不知道该怎么做.
But the colorbar is still overlapping with the plot slightly (and the labels of the x axis). I want to move the colorbar further down, but I can't figure out how to do it.
推荐答案
获得对颜色条位置的良好控制的最佳方法是为其赋予自己的轴.像这样:
The best way to get good control over the colorbar position is to give it its own axis. Like so:
# What I imagine your plotting looks like so far
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(your_data)
# Now adding the colorbar
cbaxes = fig.add_axes([0.8, 0.1, 0.03, 0.8])
cb = plt.colorbar(ax1, cax = cbaxes)
add_axes方括号中的数字表示[left,bottom,width,height],其中坐标只是绘图区域从0到1的分数.
The numbers in the square brackets of add_axes refer to [left, bottom, width, height], where the coordinates are just fractions that go from 0 to 1 of the plotting area.
这篇关于放置颜色栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!