如果您在 DS9 或 QFitsView 中打开任何图像,则可以通过单击并向两个方向拖动鼠标来更改颜色条:
我认为我的图像结果非常正确,我的代码如下,但颜色图不会拉伸,只是刻度/值。我想知道是否可以制作一个与 qfitsview 相同的颜色条,使用 matplotlib 颜色条看到颜色图拉伸而不仅仅是刻度/值。我也不知道 PowerNorm 是否是正确使用的规范,因为我希望围绕中心值进行拉伸,而不是所有颜色图都在最小值或最大值中崩溃。
这是我的尝试: 该代码在 jupyter 笔记本中运行。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm, PowerNorm, SymLogNorm
from ipywidgets import interact, FloatSlider
from matplotlib.ticker import MaxNLocator
data = np.random.rand(10,10)
def update_plot(gamma,vmin,vmax):
# Create a PowerNorm with the current gamma value and fixed vmin/vmax
norm = PowerNorm(gamma=gamma, vmin=vmin, vmax=vmax)
plt.imshow(data, cmap='viridis', norm=norm)
# Get the current axes and create a ScalarMappable to set the number of levels for the colorbar
ax = plt.gca()
sm = plt.cm.ScalarMappable(cmap='viridis', norm=norm)
sm.set_array(data) # Set the data to determine colorbar levels
# Adjust the colorbar to show more levels for stretching effect
plt.colorbar(sm, ax=ax, extend='both', ticks=MaxNLocator(nbins=10)) # You can adjust nbins as needed
plt.title(f'Colormap: Viridis, PowerNorm (Gamma: {gamma}, vmin: {vmin}, vmax: {vmax})')
plt.show()
# Use interact to create the interactive plot with sliders for gamma, vmin, and vmax
interact(update_plot,
gamma=FloatSlider(value=0.5, min=0.01, max=2.0, step=0.01, description='Gamma'),
vmin=FloatSlider(value=np.min(data), min=np.min(data), max=np.max(data), step=0.01, description='vmin'),
vmax=FloatSlider(value=np.max(data), min=np.min(data), max=np.max(data), step=0.01, description='vmax'));
plt.show()