当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python Matplotlib.colors.SymLogNorm用法及代码示例


Matplotlib是Python中令人惊叹的可视化库,用于数组的二维图。 Matplotlib是一个基于NumPy数组的多平台数据可视化库,旨在与更广泛的SciPy堆栈配合使用。

注意:有关更多信息,请参阅Python Matplotlib-概述

matplotlib.colors.SysLogNorm

matplotlib.colors.SysLogNorm类属于matplotlib.colors模块。 matplotlib.colors模块用于将颜色或数字参数转换为RGBA或RGB。此模块用于将数字映射到颜色或以一维颜色数组(也称为colormap)进行颜色规格转换。

matplotlib.colors.SymLogNorm类用于从原点到正向和负向的对称对数缩放。由于接近零范围的值趋于无穷大,因此需要在零附近具有线性范围。绘图呈线性的范围称为linthresh。线性范围(-linthresh到+ linthresh)在linscale的帮助下相对于对数范围超出了范围。线性范围的每一半要使用的十年数是其值。例如,如果linscale == 1.0(这也是默认值),则线性范围的正半部和负半部所覆盖的空间等于自然对数范围内的十进制。

该类的方法:



  1. autoscale(self, A):用于设置A的Vmax和Vmin。
  2. autoscale_None(self, A):它用于自动缩放以None为值的vmax vmin。
  3. inverse(self, value):它返回值的对数倒数。

范例1:

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.colors as colors 
  
  
# SymLogNorm:two humps, one  
# negative and one positive 
N = 100
A, B = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)] 
C1 = np.exp(-A**2 - B**2) 
C2 = np.exp(-(A - 1)**2 - (B - 1)**2) 
C = (C1 - C2) * 2
  
figure, axes = plt.subplots(2, 1) 
  
pcm = axes[0].pcolormesh(A, B, C, 
                       norm = colors.SymLogNorm(linthresh = 0.03, 
                                                linscale = 0.03, 
                                                vmin =-1.0,  
                                                vmax = 1.0), 
                       cmap ='RdBu_r') 
  
figure.colorbar(pcm, ax = axes[0], extend ='both') 
  
pcm = axes[1].pcolormesh(A, B, C,  
                         cmap ='RdBu_r', 
                         vmin =-np.max(C)) 
  
figure.colorbar(pcm, ax = axes[1], 
                extend ='both') 
  
plt.show()

输出
SymLogNorm

范例2:

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib import colors, ticker 
  
# helper function to  
# show syslognorm in action 
def symlog(arr, vmin = None, vmax = None, 
           logthresh = 5, logstep = 1, 
           linscale = 1, **kwargs):
      
    vmin = arr.min() if vmin is None else vmin 
    vmax = arr.max() if vmax is None else vmax 
    image = plt.imshow(arr, 
                       vmin = float(vmin), 
                       vmax = float(vmax), 
                       norm = colors.SymLogNorm(10**-logthresh, 
                                                linscale = linscale), 
                       **kwargs) 
  
    maxlog = int(np.ceil(np.log10(vmax))) 
    minlog = int(np.ceil(np.log10(-vmin))) 
  
    # generate logarithmic ticks 
    tick_locations =([-(10**x) for x in range(-logthresh, 
                                             minlog + 1, 
                                             logstep)][::-1] 
                    +[0.0] 
                    +[(10**x) for x in range(-logthresh, 
                                             maxlog + 1, 
                                             logstep)] ) 
  
    cb = plt.colorbar(ticks = tick_locations,  
                    format = ticker.LogFormatter()) 
      
    return image, cb 
  
data = np.arange(4).reshape(-1, 1)+np.arange(4).reshape(1, -1) 
data = 10**(data / 2.) 
data2 = data - data[::-1,::-1] 
plt.figure(figsize =(4, 3))   
image, cb = symlog(data2, interpolation ="None", 
                   cmap ="gray", logthresh = 0) 
plt.show()

输出:
SymLogNorm




相关用法


注:本文由纯净天空筛选整理自RajuKumar19大神的英文原创作品 Matplotlib.colors.SymLogNorm class in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。