Python 在函数中使用颜色栏
在我的研究报告中,我编写了一个自定义模块,其中包括两个为我的报告制作图像的功能:Python 在函数中使用颜色栏,python,matplotlib,colorbar,Python,Matplotlib,Colorbar,在我的研究报告中,我编写了一个自定义模块,其中包括两个为我的报告制作图像的功能: process_mods.py: def make_image(image, filename, color_map, title): ''' Take as arguments an array, the filename to save as, a color map, and a title for the image. Produces a publication-quali
process_mods.py
:
def make_image(image, filename, color_map, title):
'''
Take as arguments an array, the filename to save as, a color map, and
a title for the image.
Produces a publication-quality image of the array, rescaled to Galactic
coordinates.
'''
l_max = 25
b_max = 15
xlabel = r'Galactic longitude $\ell$ (deg)'
ylabel = r'Galactic latitude $b$ (deg)'
plt.imshow(image, origin='lower',
extent=[l_max, -l_max, -b_max, b_max],
cmap = color_map)
plt.title(title, fontsize = 'small')
plt.xlabel(xlabel, fontsize = 'small')
plt.ylabel(ylabel, fontsize = 'small')
plt.savefig(filename + '.png',
transparent=False,
bbox_inches='tight',
overwrite = True)
def make_image_colorbar(image, filename, color_map, title):
'''
Take as arguments an array, the filename to save as, a color map, and
a title for the image.
Produces a publication-quality image of the array, rescaled to Galactic
coordinates, including color bar.
'''
l_max = 25
b_max = 15
xlabel = r'Galactic longitude $\ell$ (deg)'
ylabel = r'Galactic latitude $b$ (deg)'
ax = plt.subplot(111)
fig =ax.imshow(image, origin='lower',
extent=[l_max, -l_max, -b_max, b_max],
cmap = color_map)
plt.colorbar(fig, fraction = 0.028)
plt.title(title, fontsize = 'small')
plt.xlabel(xlabel, fontsize = 'small')
plt.ylabel(ylabel, fontsize = 'small')
plt.savefig(filename + '.png',
transparent=False,
bbox_inches='tight',
overwrite = True)
make_image
功能按预期工作,而make_image_colorbar
的思想只是在图像中包含一个色条。
当我在两个连续数组上调用make\u image\u colorbar
函数时,第一个数组看起来不错,但第二个数组有两个颜色栏,一个是自己的,另一个来自第一个图像。下面是我用来测试模块的代码:
import process_mods as pm
arr1 = pm.create_random_array(0, 100, 10, 12, 5)
arr2 = pm.create_random_array(0, 100, 10, 12, 10)
pm.make_image(arr1, 'arr1', 'spectral', 'Array1')
pm.make_image_colorbar(arr2, 'arr2', 'spectral', 'Array2')
该测试利用了
def create_random_array(low, high, height, width, seed):
'''
Create a random array of floats from lower_limit to upper_limit
in a height x width matrix
'''
np.random.seed(seed)
random_array = high * np.random.rand(height, width) + low
return random_array
这是process\u mods的另一个函数,但我认为这不是问题所在
有人能看到我如何阻止函数在
arr2
上打印arr1
的色条吗?我认为混淆源于没有意识到您正在打印的图形/轴。我发现使用matplotlib
的面向对象接口可以更清楚地了解您使用的轴
我对代码所做的更改是在主脚本中创建图形,并在调用process\u mode.py
中定义的函数时将其作为参数传递。这样,您可以确定正在打印的位置。此外,您可以将ax
参数传递给plt.colorbar()
,以指定它应用于哪些轴:
plt.colorbar(im, ax=ax)
process\u mods.py
中的make\u image\u colorbar
将变成:
def make_image_colorbar(image, fig, ax, filename, color_map, title):
l_max = 25
b_max = 15
xlabel = r'Galactic longitude $\ell$ (deg)'
ylabel = r'Galactic latitude $b$ (deg)'
im = ax.imshow(image, origin='lower',
extent=[l_max, -l_max, -b_max, b_max],
cmap = color_map)
fig.colorbar(im, fraction = 0.028, ax = ax)
ax.set_title(title, fontsize = 'small')
ax.set_xlabel(xlabel, fontsize = 'small')
ax.set_ylabel(ylabel, fontsize = 'small')
fig.savefig(filename + '.png',
transparent=False,
bbox_inches='tight',
overwrite = True)
注意:我删除了docstring只是为了让这个答案更容易阅读
对于make\u image
然后,您的主函数将如下所示
import process_mods as pm
arr1 = pm.create_random_array(0, 100, 10, 12, 5)
arr2 = pm.create_random_array(0, 100, 10, 12, 10)
fig1, ax1 = plt.subplots()
fig2, ax2 = plt.subplots()
pm.make_image_colorbar(arr1, fig1, ax1, 'arr1', 'spectral', 'Array1')
pm.make_image_colorbar(arr2, fig2, ax2, 'arr2', 'spectral', 'Array2')
plt.show()
其中: