LogFormatter标记科学格式限制 [英] LogFormatter tickmarks scientific format limits

查看:358
本文介绍了LogFormatter标记科学格式限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个对数坐标轴来绘制一个广泛的范围,但是我想要显示10 ^ { - 1},10 ^ 0,10 ^ 1只是0.1,1,10。ScalarFormatter会改变一切都是整数而不是科学记数法,但我希望大多数的标记标签是科学的;我只是想改变一些标签。所以MWE是

pre $ code $> import numpy as np
import matplotlib as plt
fig = plt.figure( figsize = [7,7])
ax1 = fig.add_subplot(111)
ax1.set_yscale('log')
ax1.set_xscale('log')
ax1。 plot(np.logspace(-4,4),np.logspace(-4,4))
plt.show()

,我希望每个轴上的中间标签读取0.1,1,10而不是10 ^ { - 1},10 ^ 0,10 ^ 1

感谢您的帮助!

解决方案

设置 set_xscale('log' ),您正在使用 LogFormatterSciNotation (不是 ScalarFormatter )。你可以继承 LogFormatterSciNotation 来返回所需的值 0.1,1,10 ,如果它们碰巧标记为ticks

  import numpy as np 
import matplotlib.pyplot as plt
from matplotlib.ticker import LogFormatterSciNotation

类CustomTicker(LogFormatterSciNotation):
def __call __(self,x,pos = None):
如果x不在[0.1,1,10]:
返回LogFormatterSciNotation。 __call __(self,x,pos = None)
else:
return{x:g}。format(x = x)


fig = plt .figure(figsize = [7,7])
ax = fig.add_subplot(111)
ax.set_yscale('log')
ax.set_xscale('log')
.plot.plot(np.logspace(-4,4),np.logspace(-4,4))

ax.xaxis.set_major_formatter(CustomTicker())
plt.show ()



更新:使用matplotlib 2.1现在有一个新选项


$ b


指定最小值以格式化为LogFormatterMathtext的标量

LogFormatterMathtext现在包含指定最小值指数的选项,以格式化为标量(即0.001而不是10-3)。

这可以通过使用rcParams( plt.rcParams ['axes.formatter.min_exponent'] = 2 ):

  import numpy as np 
import matplotlib.pyplot as plt
plt.rcParams ['axes.formatter.min_exponent'] = 2

fig = plt.figure(figsize = [7, 7])
ax = fig.add_subplot(111)
ax.set_yscale('log')
ax.set_xscale('log')
ax.plot(np.logspace (-4,4),np.logspace(-4,4))

plt.show()

结果与上面相同。然而,注意这个限制是对称的,它不允许设置1和10,但不能设置为0.1。因此,最初的解决方案是更通用的。


I'm trying to plot over a wide range with a log-scaled axis, but I want to show 10^{-1}, 10^0, 10^1 as just 0.1, 1, 10. ScalarFormatter will change everything to integers instead of scientific notation, but I'd like most of the tickmark labels to be scientific; I'm only wanting to change a few of the labels. So the MWE is

import numpy as np
import matplotlib as plt
fig = plt.figure(figsize=[7,7])
ax1 = fig.add_subplot(111)
ax1.set_yscale('log')
ax1.set_xscale('log')
ax1.plot(np.logspace(-4,4), np.logspace(-4,4))
plt.show()

and I want the middle labels on each axis to read 0.1, 1, 10 instead of 10^{-1}, 10^0, 10^1

Thanks for any help!

解决方案

When setting set_xscale('log'), you're using a LogFormatterSciNotation (not a ScalarFormatter). You may subclass LogFormatterSciNotation to return the desired values 0.1,1,10 if they happen to be marked as ticks.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import LogFormatterSciNotation

class CustomTicker(LogFormatterSciNotation):
    def __call__(self, x, pos=None):
        if x not in [0.1,1,10]:
            return LogFormatterSciNotation.__call__(self,x, pos=None)
        else:
            return "{x:g}".format(x=x)


fig = plt.figure(figsize=[7,7])
ax = fig.add_subplot(111)
ax.set_yscale('log')
ax.set_xscale('log')
ax.plot(np.logspace(-4,4), np.logspace(-4,4))

ax.xaxis.set_major_formatter(CustomTicker())
plt.show()


Update: With matplotlib 2.1 there is now a new option

Specify minimum value to format as scalar for LogFormatterMathtext
LogFormatterMathtext now includes the option to specify a minimum value exponent to format as a scalar (i.e., 0.001 instead of 10-3).

This can be done as follows, by using the rcParams (plt.rcParams['axes.formatter.min_exponent'] = 2):

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['axes.formatter.min_exponent'] = 2

fig = plt.figure(figsize=[7,7])
ax = fig.add_subplot(111)
ax.set_yscale('log')
ax.set_xscale('log')
ax.plot(np.logspace(-4,4), np.logspace(-4,4))

plt.show()

This results in the same plot as above.

Note however that this limit is symmetric, it would not allow to set only 1 and 10, but not 0.1. Hence the initial solution is more generic.

这篇关于LogFormatter标记科学格式限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆