在matplotlib中组合日志和线性比例

时间:2014-02-13 06:07:06

标签: python matplotlib scipy

这里的例子 What is the difference between 'log' and 'symlog'? 很好地展示了原点的线性刻度如何与其他地方的对数刻度一起使用。我想走另一条路。我想要一个1-100的对数刻度,然后是线性的!规模从100-1000。我有什么选择?如上图所示 这种尝试不起作用

    import matplotlib.pyplot as plt
    plt.figure()
    plt.errorbar(x, y, yerr=yerrors)
    plt.xscale('symlog', linthreshx= (100,1000))

问题似乎是linthreshx定义为取范围(-x,x)。因此,如果x为5,我们将得到线性比例(-5,5)。一个局限于原点。我认为只是选择一个不同的范围应该有效,但事实并非如此。有什么想法吗?

3 个答案:

答案 0 :(得分:5)

From the response of user1318806 to cphlewis

  

谢谢。实际上我想要x 轴上的log + linear的组合而不是y。但我认为你的代码应该很容易适应。

您好!如果你想在x轴上组合log + linear(从Duncan Watts and CubeJockey的代码图案化):

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np

# Numbers from -50 to 50, with 0.1 as step
xdomain = np.arange(-50,50, 0.1)

axMain = plt.subplot(111)
axMain.plot(np.sin(xdomain), xdomain)
axMain.set_xscale('linear')
axMain.set_xlim((0.5, 1.5))
axMain.spines['left'].set_visible(False)
axMain.yaxis.set_ticks_position('right')
axMain.yaxis.set_visible(False)


divider = make_axes_locatable(axMain)
axLin = divider.append_axes("left", size=2.0, pad=0, sharey=axMain)
axLin.set_xscale('log')
axLin.set_xlim((0.01, 0.5))
axLin.plot(np.sin(xdomain), xdomain)
axLin.spines['right'].set_visible(False)
axLin.yaxis.set_ticks_position('left')
plt.setp(axLin.get_xticklabels(), visible=True)

plt.title('Linear right, log left')

上面的代码产生:Answer1

(其他)这是一个非常小的修正标题,右边没有刻度线:

# Fix for: title + no tick marks on the right side of the plot
ax2 = axLin.twinx()
ax2.spines['left'].set_visible(False)
ax2.tick_params(axis='y',which='both',labelright='off')

添加这些行将为您提供:Answer2

答案 1 :(得分:4)

我假设你想要在原点附近线性,记录更远 - 因为`symlog'反过来做了 - 我无法想出看起来像这样的数据,但是你可以将它与axes_grid:

放在一起
# linear and log axes for the same plot?
# starting with the histogram example from 
# http://matplotlib.org/mpl_toolkits/axes_grid/users/overview.html
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np

# Numbers from -50 to 50, with 0.1 as step
xdomain = np.arange(-50,50, 0.1)

axMain = plt.subplot(111)
axMain.plot(xdomain, np.sin(xdomain))
axMain.set_yscale('log')
axMain.set_ylim((0.01, 0.5))
divider = make_axes_locatable(axMain)
axLin = divider.append_axes("top", size=2.0, pad=0.02, sharex=axMain)
axLin.plot(xdomain, np.sin(xdomain))

axLin.set_xscale('linear')
axLin.set_ylim((0.5, 1.5))
plt.title('Linear above, log below')

plt.show()

enter image description here

答案 2 :(得分:2)

此解决方案添加了cphlewis's answer,以便平滑过渡,并且图表似乎具有一致的刻度标记。我的更改添加了这三行:

axLin.spines['bottom'].set_visible(False)

axLin.xaxis.set_ticks_position('top')

plt.setp(axLin.get_xticklabels(), visible=False)

总的来说,代码是

# linear and log axes for the same plot?
# starting with the histogram example from 
# http://matplotlib.org/mpl_toolkits/axes_grid/users/overview.html
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np

# Numbers from -50 to 50, with 0.1 as step
xdomain = np.arange(-50,50, 0.1)

axMain = plt.subplot(111)
axMain.plot(xdomain, np.sin(xdomain))
axMain.set_yscale('log')
axMain.set_ylim((0.01, 0.5))
axMain.spines['top'].set_visible(False)
axMain.xaxis.set_ticks_position('bottom')

divider = make_axes_locatable(axMain)
axLin = divider.append_axes("top", size=2.0, pad=0, sharex=axMain)
axLin.plot(xdomain, np.sin(xdomain))
axLin.set_xscale('linear')
axLin.set_ylim((0.5, 1.5))

# Removes bottom axis line
axLin.spines['bottom'].set_visible(False)
axLin.xaxis.set_ticks_position('top')
plt.setp(axLin.get_xticklabels(), visible=False)

plt.title('Linear above, log below')

plt.show()

enter image description here