matplotlib中【colormap】和【colorbar】的使用,以及用自己的颜色创建colormap

官方自带的colormap

import numpy as np
import matplotlib.pyplot as plt

num_curves = 100 #100条曲线
cm = plt.cm.rainbow(np.linspace(0, 1, num_curves))
sm = plt.cm.ScalarMappable(cmap='rainbow',
                               norm=plt.Normalize(vmin=0, vmax=num_curves))
fig = plt.figure(figsize=(6,4))
for i in range(num_curves):
    curve = np.linspace(0,100+0.4*i,100) #生成曲线
    plt.plot(curve,color=cm[i])
plt.title('An example of ColorMap and ColorBar')
cb = plt.colorbar(sm)
cb.set_label('curve number')
plt.show()

其他的colormap

可参考官网链接: https://matplotlib.org/2.0.2/users/colormaps.html

结果

在这里插入图片描述

单独绘制一个colorbar

cycle_num = 100

fig, ax = plt.subplots(figsize=(5, 2))  # 设置colorbar的尺寸
fig.subplots_adjust(bottom=0.3, top=0.7,left=0.1,right=0.9)  # 调整图的上下边距

sm = plt.cm.ScalarMappable(cmap='rainbow', norm=plt.Normalize(vmin=0, vmax=cycle_num))
cb = plt.colorbar(sm, cax=ax, orientation='horizontal')  # 指定colorbar为水平方向
cb.set_label('label', fontsize=14)  # 设置colorbar的标签字体大小
plt.title('title', fontsize=14)  # 设置colorbar的标题字体大小
plt.show()

结果:
在这里插入图片描述

用自己的颜色创建colormap

from matplotlib.colors import LinearSegmentedColormap
def create_colormap(rgb_list,alpha_list=None):
    '''
    取rgb_list里面的颜色,通过等间隔线性插值,生成colormap
    :param rgb_list: 形如[(0.69,0.53,0.81),(0.92,0.10,0.50),(1.00,0.78,0.01)]
                    list中的颜色为colorbar中插值点的颜色
    :param alpha_list: 透明度列表,形如[0.2,0.3]
    :return:colorbar
    '''
    num_of_colors = len(rgb_list)
    interpolation_points = np.linspace(0,1,num_of_colors) #插值点位置
    red, green, blue = [], [], []
    for i in range(num_of_colors):
        r = [interpolation_points[i],rgb_list[i][0],rgb_list[i][0]]
        g = [interpolation_points[i],rgb_list[i][1],rgb_list[i][1]]
        b = [interpolation_points[i],rgb_list[i][2],rgb_list[i][2]]
        red.append(r)
        green.append(g)
        blue.append(b)
    if alpha_list is not None: #透明度变化
        alpha = []
        local = np.linspace(0, 1, len(alpha_list))
        for i in range(len(alpha_list)):
            alpha.append([local[i],alpha_list[i],alpha_list[i]])
    else: #不透明
        alpha =[[0,1,1],[1,1,1]]
    color_dict = {
        'red':red,
        'green':green,
        'blue':blue,
        'alpha':alpha
    }
    my_colormap = LinearSegmentedColormap(name='my_colormap', segmentdata=color_dict)
    plt.register_cmap(cmap=my_colormap)
    return my_colormap

测试create_colormap函数

color_list = [(0.69,0.53,0.81),(0.92,0.10,0.50),(1.00,0.78,0.01)]
cmap = create_colormap(color_list)
num_curves = 100 #100条曲线
cm = cmap(np.linspace(0, 1, num_curves))
sm = plt.cm.ScalarMappable(cmap=cmap,norm=plt.Normalize(vmin=0, vmax=num_curves))
fig = plt.figure(figsize=(6,4))
for i in range(num_curves):
    curve = np.linspace(0,100+0.4*i,100) #生成曲线
    plt.plot(curve,color=cm[i])
plt.title('Another example of ColorMap and ColorBar')
cb = plt.colorbar(sm)
cb.set_label('curve number')
plt.show()

在这里插入图片描述

获取图片颜色

可以参考这篇:https://blog.csdn.net/baidu_38963740/article/details/125210583?spm=1001.2014.3001.5501

给定一个基本颜色,通过改变饱和度来创建colorbar

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors as mcolors

def generate_colorbar(base_color, num_steps=10):
    # Convert the base_color to RGB format if it's not already
    base_color_rgb = mcolors.to_rgba(base_color)[:3]

    # Create an array of colors with the same hue as the base_color but varying saturation
    saturation_values = np.linspace(0.2, 1, num_steps)
    colors = [base_color_rgb + (s,) for s in saturation_values]

    # Create a custom colormap using the generated colors
    cmap = mcolors.ListedColormap(colors)

    # Create a colorbar with the custom colormap
    norm = mcolors.Normalize(vmin=0, vmax=num_steps)
    scalar_map = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
    scalar_map.set_array([])

    # Plot the colorbar
    plt.colorbar(scalar_map)

    # Optional: Display a sample image with the colorbar to visualize the gradient
    gradient_image = np.linspace(0, 1, 100).reshape(1, -1)
    plt.imshow(gradient_image, cmap=cmap, aspect='auto', extent=(0, 1, 0, 0.1))
    plt.yticks([])

    # Show the plot
    plt.show()

# Example:
base_color = 'red'  
generate_colorbar(base_color, num_steps=10)

运行结果:
在这里插入图片描述

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<matplotlib.colorbar.colorbar at 0x2b48e65ebe0> 是一个 matplotlib颜色条(colorbar)对象。在数据可视化过程颜色条可以作为一种辅助工具,用于表示数据的颜色映射关系。它通常被添加到图表的某个位置,将颜色与数据值相对应起来。 在创建一个颜色条对象时,需要指定一些参数,比如颜色映射(colormap)、轴对象等。颜色映射定义了如何将数据值映射到颜色值上, matplotlib 提供了很多内置的颜色映射,如热力图(hot)、彩虹图(rainbow)等。轴对象则用于指定颜色条在图表的位置和大小,可以根据需要进行调整。 一旦创建颜色条对象,可以使用一系列方法来对其进行进一步的定制。比如,可以通过设置标签、标题和刻度等属性,使颜色条更具有可读性和可视化效果。同时,还可以设置颜色条的方向、对齐方式等,以适应不同的图表布局。 除了在图表使用颜色条作为辅助工具,颜色条对象还可以作为一个独立的绘图组件,单独使用。可以将其放置在图形界面的某个窗口,以创建交互式的颜色选择器。 总结来说,<matplotlib.colorbar.colorbar at 0x2b48e65ebe0> 是 matplotlib的一个颜色条对象。它可以用来表示数据的颜色映射关系,并可以通过各种方法进行个性化定制。在数据可视化颜色条是一种常见的辅助工具,可以帮助我们更好地理解数据的分布和趋势。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值