将颜色条定位在图形内 [英] Position colorbar inside figure
问题描述
我有一个简单的散点图,其中每个点都有一种颜色,该颜色由设置为所选颜色图的0到1之间的值给出.这是我的代码的 MWE
:
将matplotlib.pyplot导入为plt将numpy导入为np导入 matplotlib.gridspec 作为 gridspecx = np.random.randn(60)y = np.random.randn(60)z = [n范围(60)中的_的np.random.random()]无花果= plt.figure()gs = gridspec.GridSpec(1、2)ax0 = plt.subplot(gs [0,0])plt.scatter(x,y,s = 20)ax1 = plt.subplot(gs [0,1])cm = plt.cm.get_cmap('RdYlBu_r')plt.scatter(x, y, s=20 ,c=z, cmap=cm)cbaxes = fig.add_axes([0.6, 0.12, 0.1, 0.02])plt.colorbar(cax=cbaxes, ticks=[0.,1],orientation='horizontal')fig.tight_layout()plt.show()
看起来像这样:
这里的问题是,我希望在绘图的左下角有一个较小的水平颜色条位置,但使用 cax
参数不仅感觉有些古怪,而且显然与 tight_layout
导致警告:
/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py:1533:UserWarning:此图包括与紧密布局不兼容的轴,因此其结果可能不正确.warnings.warn(此图包括不是"
难道没有更好的方法来定位颜色条,即在您运行代码时不会收到令人讨厌的警告吗?
<小时>编辑
我希望颜色栏仅显示最大值和最小值,即:0和1,而Joe通过向 scatter 添加
vmin = 0,vmax = 1
来帮助我做到这一点
plt.scatter(x, y, s=20, vmin=0, vmax=1)
所以我删除了这部分问题.
一个人可以使用
将matplotlib.pyplot导入为plt将numpy导入为np导入 matplotlib.gridspec 作为 gridspec从 mpl_toolkits.axes_grid1.inset_locator 导入 inset_axesx = np.random.randn(60)y = np.random.randn(60)z = [n范围(60)中的_的np.random.random()]无花果= plt.figure()gs = gridspec.GridSpec(1、2)ax0 = plt.subplot(gs [0,0])plt.scatter(x,y,s = 20)ax1 = plt.subplot(gs[0, 1])cm = plt.cm.get_cmap('RdYlBu_r')plt.scatter(x,y,s = 20,c = z,cmap = cm)fig.tight_layout()cbaxes = inset_axes(ax1,width ="30%",height ="3%",loc = 3)plt.colorbar(cax=cbaxes, ticks=[0.,1],orientation='horizontal')plt.show()
请注意,为了消除警告,您可以在添加插入轴之前简单地调用 tight_layout
.
I have a simple scatter plot where each point has a color given by a value between 0 and 1 set to a chosen colormap. Here's a MWE
of my code:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec
x = np.random.randn(60)
y = np.random.randn(60)
z = [np.random.random() for _ in range(60)]
fig = plt.figure()
gs = gridspec.GridSpec(1, 2)
ax0 = plt.subplot(gs[0, 0])
plt.scatter(x, y, s=20)
ax1 = plt.subplot(gs[0, 1])
cm = plt.cm.get_cmap('RdYlBu_r')
plt.scatter(x, y, s=20 ,c=z, cmap=cm)
cbaxes = fig.add_axes([0.6, 0.12, 0.1, 0.02])
plt.colorbar(cax=cbaxes, ticks=[0.,1], orientation='horizontal')
fig.tight_layout()
plt.show()
which looks like this:
The problem here is that I want the small horizontal colorbar position to the lower left of the plot but using the cax
argument not only feels a bit hacky, it apparently conflicts with tight_layout
which results in the warning:
/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py:1533: UserWarning: This figure includes Axes that are not compatible with tight_layout, so its results might be incorrect.
warnings.warn("This figure includes Axes that are not "
Isn't there a better way to position the colorbar, ie without getting a nasty warning thrown at you whenever you run the code?
Edit
I wanted the colorbar to show only the max and min values, ie: 0 and 1 and Joe helped me do that by adding vmin=0, vmax=1
to scatter
like so:
plt.scatter(x, y, s=20, vmin=0, vmax=1)
so I'm removing this part of the question.
One may use a mpl_toolkits.axes_grid1.inset_locator.inset_axes
to place an axes inside another axes. This axes can be used to host the colorbar. Its position is relative the the parent axes, similar to how legends are placed, using a loc
argument (e.g. loc=3
means lower left). Its width and height can be specified in absolute numbers (inches) or relative to the parent axes (percentage).
cbaxes = inset_axes(ax1, width="30%", height="3%", loc=3)
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
x = np.random.randn(60)
y = np.random.randn(60)
z = [np.random.random() for _ in range(60)]
fig = plt.figure()
gs = gridspec.GridSpec(1, 2)
ax0 = plt.subplot(gs[0, 0])
plt.scatter(x, y, s=20)
ax1 = plt.subplot(gs[0, 1])
cm = plt.cm.get_cmap('RdYlBu_r')
plt.scatter(x, y, s=20 ,c=z, cmap=cm)
fig.tight_layout()
cbaxes = inset_axes(ax1, width="30%", height="3%", loc=3)
plt.colorbar(cax=cbaxes, ticks=[0.,1], orientation='horizontal')
plt.show()
Note that in order to suppress the warning, one might simply call tight_layout
prior to adding the inset axes.
这篇关于将颜色条定位在图形内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!