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


Python Matplotlib.ticker.FuncFormatter用法及代码示例

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

matplotlib.ticker.FuncFormatter

这个 matplotlib.ticker.FuncFormatter类使用用户定义的函数进行格式化。此用户定义的函数必须将两个值用作刻度值x和位置pos的输入。

用法: class matplotlib.ticker.FuncFormatter(func)

参数:

  • func:用户定义的函数,用于格式化图。

范例1:



import matplotlib.pyplot as plt 
import matplotlib.ticker as tick 
import numpy as np 
  
  
x = np.linspace(0, 10, 1000) 
y = 0.000001 * np.sin(10 * x) 
  
fig = plt.figure() 
ax = fig.add_subplot(111) 
  
ax.plot(x, y) 
  
def y_fmt(x, y):
    return '{:2.2e}'.format(x).replace('e', 'x10^') 
  
ax.yaxis.set_major_formatter(tick.FuncFormatter(y_fmt)) 
  
plt.show()

输出:

范例2:

import matplotlib.pyplot as plt 
from matplotlib.ticker import FuncFormatter 
  
  
fig, ax = plt.subplots() 
ax.axis([0.01, 10000, 1, 1000000]) 
ax.loglog() 
  
for axis in [ax.xaxis, ax.yaxis]:
    formatter = FuncFormatter(lambda y, _:'{:.16g}'.format(y)) 
    axis.set_major_formatter(formatter) 
  
plt.show()

输出:




相关用法


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