LogFormatter标记科学格式限制 [英] LogFormatter tickmarks scientific format limits
问题描述
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屋!