图形和颜色条上的大小相同[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我简单地绘制了一个拟合文件,并在右侧绘制了一个颜色条。我无法调节图像和颜色条的大小,我希望它们具有相同的尺寸。此外,我想知道如何更改颜色条的标签,如我通常在简单图上使用的ax0.set_xticks()ax0.set_xticklabels()

这是代码:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from astropy.io import fits
import copy
from astropy.wcs import WCS
from astropy.utils.data import get_pkg_data_filename
from matplotlib.colors import LogNorm, Normalize
from regions import read_ds9

fits_file = "fb.fits"
hdul = fits.open(fits_file)
image_data = hdul[0].data
wcs = WCS(hdul[0].header)
hdul.close()

fig = plt.figure(figsize=(8,8))
ax0=fig.add_subplot(111, projection=wcs)

norm = LogNorm(10,1)
my_cmap = copy.copy(matplotlib.cm.get_cmap('Blues_r'))
my_cmap.set_bad((0,0,0))
im = ax0.imshow(image_data, cmap=my_cmap, norm=norm)
bar = fig.colorbar(im)


reg_file = "astropy.reg"
regs = read_ds9(reg_file, errors='warn')
for i, reg in enumerate(regs):
    reg.plot(ax=ax0)

ax0.set_title('')
ax0.set_xlabel('RA')
ax0.set_ylabel('DEC')
fig.tight_layout(rect=[0.08, 0.08, 0.94, 0.9])

plt.show()

最后的形象:enter image description here

python matplotlib astropy
1个回答
0
投票

您可以使用shrink作为颜色条的参数。来自the docs

收缩分数乘以颜色条的大小

看一下链接,可以选择其他选项,例如设置填充或宽高比。

这样的事可能适合你:

bar = fig.colorbar(im, shrink=0.8)

你可以在你的colorbar所在的轴对象上使用set_yticklabels(你可以用bar.ax获得它)。如果您想自己指定ticklabels,请不要忘记第一个指定刻度线位置。

所以类似于:

# `myticklocs` are the locations and `mylabels` are your labels
bar = fig.colormap(im, ticks=myticklocs)
bar.ax.set_yticklabels(mylabels, weight='bold', size=9)

看看最小的official labelling demo

© www.soinside.com 2019 - 2024. All rights reserved.