本文介绍了循环移动色彩图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法循环移位,例如hsv颜色图,以便可以更改中心颜色?它在我的图中占据了相当多的空间,我希望它是一种不同的颜色.由于颜色图是周期性的,这应该是可能的.

解决方案

基于此

Is there a way to cyclically shift e.g. the hsv colormap so that the central color can be altered? It's taking up quite some space in my figure, and I'd like it to be a different color. As the colormap is cyclical, this should be possible.

解决方案

Based on the non-linear colormap used in this example, but with linear levels replaces by a shift,

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

class roll_cmap(LinearSegmentedColormap):

    def __init__(self, cmap, shift):

        assert 0. < shift < 1.
        self.cmap = cmap
        self.N = cmap.N
        self.monochrome = self.cmap.monochrome
        self._x = np.linspace(0.0, 1.0, 255)
        self._y = np.roll(np.linspace(0.0, 1.0, 255),int(255.*shift))

    def __call__(self, xi, alpha=1.0, **kw):
        yi = np.interp(xi, self._x, self._y)
        return self.cmap(yi, alpha)


if __name__ == '__main__':

    y, x = np.mgrid[0.0:3.0:100j, 0.0:5.0:100j]
    H = np.sin(8*x/np.pi)

    cmap = plt.cm.hsv
    cmap_rolled = roll_cmap(cmap, shift=0.8)

    plt.subplot(2,1,1)
    plt.contourf(x, y, H, cmap=cmap)
    plt.colorbar()
    plt.subplot(2,1,2)
    plt.contourf(x, y, H, cmap=cmap_rolled)
    plt.colorbar()

    plt.show()

which results in the following output,

这篇关于循环移动色彩图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 00:52