Python 使用颜色条设置绘图中的相等纵横比
我需要生成一个在轴上具有相等纵横比的绘图,并在右侧生成一个颜色条。我试过设置Python 使用颜色条设置绘图中的相等纵横比,python,matplotlib,plot,colorbar,Python,Matplotlib,Plot,Colorbar,我需要生成一个在轴上具有相等纵横比的绘图,并在右侧生成一个颜色条。我试过设置aspect='auto',aspect=1,以及aspect='equal',但没有得到好的结果。有关示例和MWE,请参见下文 使用aspect='auto'色条的高度正确,但绘图失真: 使用aspect=1或aspect='equal'绘图是方形的(两个轴的aspect相等),但色条扭曲: 在这两个图中,由于某些原因,颜色条的位置过于向右。如何获得具有匹配高度色条的方形图 MWE import numpy as
aspect='auto'
,aspect=1
,以及aspect='equal'
,但没有得到好的结果。有关示例和MWE,请参见下文
使用aspect='auto'
色条的高度正确,但绘图失真:
使用aspect=1
或aspect='equal'
绘图是方形的(两个轴的aspect相等),但色条扭曲:
在这两个图中,由于某些原因,颜色条的位置过于向右。如何获得具有匹配高度色条的方形图
MWE
import numpy as np
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
def col_plot(params):
gs, i, data = params
xarr, yarr, zarr = zip(*data)[0], zip(*data)[1], zip(*data)[2]
xmin, xmax = min(xarr), max(xarr)
ymin, ymax = min(yarr), max(yarr)
#plt.subplot(gs[i], aspect='auto')
plt.subplot(gs[i], aspect=1)
#plt.subplot(gs[i], aspect='equal')
plt.xlim(xmin, xmax)
plt.ylim(xmin, xmax)
plt.xlabel('$x axis$', fontsize=20)
plt.ylabel('$y axis$', fontsize=20)
# Scatter plot.
cm = plt.cm.get_cmap('RdYlBu_r')
SC = plt.scatter(xarr, yarr, marker='o', c=zarr, s=60, lw=0.25, cmap=cm,
zorder=3)
# Colorbar.
ax0 = plt.subplot(gs[i + 1])
cbar = plt.colorbar(SC, cax=ax0)
cbar.set_label('$col bar$', fontsize=21, labelpad=-2)
# Generate data.
data0 = np.random.uniform(0., 1., size=(50, 3))
data1 = np.random.uniform(0., 1., size=(50, 3))
# Create the top-level container
fig = plt.figure(figsize=(14, 25))
gs = gridspec.GridSpec(4, 4, width_ratios=[1, 0.05, 1, 0.05])
# Generate plots.
par_lst = [[gs, 0, data0], [gs, 2, data1]]
for pl_params in par_lst:
col_plot(pl_params)
# Output png file.
fig.tight_layout()
plt.savefig('colorbar_aspect.png', dpi=300)
您可以使用一个。我对您的代码进行了一些修改,以使用AxesDivider
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
def col_plot(data):
xarr, yarr, zarr = zip(*data)[0], zip(*data)[1], zip(*data)[2]
xarr = [2*x for x in xarr]
xmin, xmax = min(xarr), max(xarr)
ymin, ymax = min(yarr), max(yarr)
fig = plt.figure()
ax0 = fig.add_subplot(111, aspect='equal')
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
plt.xlabel('$x axis$', fontsize=20)
plt.ylabel('$y axis$', fontsize=20)
# Scatter plot.
cm = plt.cm.get_cmap('RdYlBu_r')
SC = ax0.scatter(xarr, yarr, marker='o', c=zarr, s=60, lw=0.25, cmap=cm,
zorder=3)
the_divider = make_axes_locatable(ax0)
color_axis = the_divider.append_axes("right", size="5%", pad=0.1)
# Colorbar.
cbar = plt.colorbar(SC, cax=color_axis)
cbar.set_label('$col bar$', fontsize=21, labelpad=-2)
# Generate data.
data0 = np.random.uniform(0., 1., size=(20, 3))
col_plot(data0)
这里是结果(为了演示,我更改了数据,使其在x方向上跨越[0,2]的范围):上有以下很好的解决方案
1) 将colorbar
函数定义为:
from mpl_toolkits.axes_grid1 import make_axes_locatable
def colorbar(mappable):
ax = mappable.axes
fig = ax.figure
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
return fig.colorbar(mappable, cax=cax)
2) 当你想制作色条时,请调用colorbar(thing)
。就你而言:
SC = ax0.scatter(xarr, yarr, marker='o', c=zarr, s=60, lw=0.25, cmap=cm,
zorder=3)
colorbar(SC)
3) 你会得到:
谢谢朱利安!我不得不稍微修改一下你的答案,但使用
使轴可定位
是一个很好的解决方案。