# matplotlib：将轴的偏移值格式化为整数或特定的数字

``p = axes.plot(x,y) p.ticklabel_format(style='plain')` `

` `myyfmt = ScalarFormatter(useOffset=True) myyfmt._set_offset(1e9) axes.get_yaxis().set_major_formatter(myyfmt)` `

` `myxfmt = ScalarFormatter(useOffset=True) myxfmt.set_portlimits((-9,5)) axes.get_xaxis().set_major_formatter(myxfmt)` `

### 8 Solutions collect form web for “matplotlib：将轴的偏移值格式化为整数或特定的数字”

` `y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False) ax.yaxis.set_major_formatter(y_formatter)` `

` `from pylab import * # Generate some random data... x = linspace(55478, 55486, 100) y = random(100) - 0.5 y = cumsum(y) y -= y.min() y *= 1e-8 # plot plot(x,y) # xticks locs,labels = xticks() xticks(locs, map(lambda x: "%g" % x, locs)) # ytikcs locs,labels = yticks() yticks(locs, map(lambda x: "%.1f" % x, locs*1e9)) ylabel('microseconds (1E-9)') show()` `

` `locs,labels = yticks() yticks(locs, map(lambda x: "%.1f" % x, locs*1e9)) text(0.0, 1.01, '1e-9', fontsize=10, transform = gca().transAxes)` `

EDIT2

` `locs,labels = xticks() xticks(locs, map(lambda x: "%g" % x, locs-min(locs))) text(0.92, -0.07, "+%g" % min(locs), fontsize=10, transform = gca().transAxes)` `

` `import numpy as np import matplotlib.pyplot as plt from matplotlib.ticker import ScalarFormatter, FormatStrFormatter class FixedOrderFormatter(ScalarFormatter): """Formats axis ticks using scientific notation with a constant order of magnitude""" def __init__(self, order_of_mag=0, useOffset=True, useMathText=False): self._order_of_mag = order_of_mag ScalarFormatter.__init__(self, useOffset=useOffset, useMathText=useMathText) def _set_orderOfMagnitude(self, range): """Over-riding this to avoid having orderOfMagnitude reset elsewhere""" self.orderOfMagnitude = self._order_of_mag # Generate some random data... x = np.linspace(55478, 55486, 100) y = np.random.random(100) - 0.5 y = np.cumsum(y) y -= y.min() y *= 1e-8 # Plot the data... fig = plt.figure() ax = fig.add_subplot(111) ax.plot(x, y, 'b-') # Force the y-axis ticks to use 1e-9 as a base exponent ax.yaxis.set_major_formatter(FixedOrderFormatter(-9)) # Make the x-axis ticks formatted to 0 decimal places ax.xaxis.set_major_formatter(FormatStrFormatter('%0.0f')) plt.show()` `

` `import numpy as np import matplotlib.pyplot as plt from matplotlib.ticker import FuncFormatter # Generate some random data... x = np.linspace(55478, 55486, 100) y = np.random.random(100) - 0.5 y = np.cumsum(y) y -= y.min() y *= 1e-8 # Plot the data... fig = plt.figure() ax = fig.add_subplot(111) ax.plot(x, y, 'b-') # Force the y-axis ticks to use 1e-9 as a base exponent ax.yaxis.set_major_formatter(FuncFormatter(lambda x, pos: ('%.1f')%(x*1e9))) ax.set_ylabel('microseconds (1E-9)') # Make the x-axis ticks formatted to 0 decimal places ax.xaxis.set_major_formatter(FuncFormatter(lambda x, pos: '%.0f'%x)) plt.show()` `

` `from pylab import * from matplotlib.ticker import MultipleLocator, FormatStrFormatter majorLocator = MultipleLocator(20) xFormatter = FormatStrFormatter('%d') yFormatter = FormatStrFormatter('%.2f') minorLocator = MultipleLocator(5) t = arange(0.0, 100.0, 0.1) s = sin(0.1*pi*t)*exp(-t*0.01) ax = subplot(111) plot(t,s) ax.xaxis.set_major_locator(majorLocator) ax.xaxis.set_major_formatter(xFormatter) ax.yaxis.set_major_formatter(yFormatter) #for the minor ticks, use no labels; default NullFormatter ax.xaxis.set_minor_locator(minorLocator)` `

` `ax=gca() fmt=matplotlib.ticker.ScalarFormatter(useOffset=False) fmt.set_scientific(False) ax.xaxis.set_major_formatter(fmt)` `

` `matplotlib.rcParams['axes.formatter.useoffset'] = False` `

` `class CustomScalarFormatter(ScalarFormatter): def format_data(self, value): if self._useLocale: s = locale.format_string('%1.2g', (value,)) else: s = '%1.2g' % value s = self._formatSciNotation(s) return self.fix_minus(s) xmajorformatter = CustomScalarFormatter() # default useOffset=True axes.get_xaxis().set_major_formatter(xmajorformatter)` `

• 2套联盟不包含所有项目
• 我怎样才能用Python的最近邻居algorithm分类数据？
• NLTK的所有pos标签是什么？
• Django DoesNotExist
• 如何重新加载使用`从模块导入*`导入的Python模块
• 如何使用Django Rest Framework包含相关的模型字段？
• 如何在Python中设置matplotlib中的“后端”？
• manage.py runserver
• 在CPU上运行Tensorflow
• matplotlib - 从等高线中提取数据
• 如果Dictionary键不可用，则返回None