【问题标题】:combining a log and linear scale in matplotlib在 matplotlib 中结合对数和线性比例
【发布时间】:2014-02-13 06:07:06
【问题描述】:

这里的例子 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 if 5 我们将在 (-5,5) 上获得线性比例。一是局限在原点。我认为简单地选择一个不同的范围应该可行,但它没有。有什么想法吗?

【问题讨论】:

  • 你为什么没有一个从 1 到 1000 的对数刻度?没看懂你的目的是什么?你能提供一个你想要达到的目标的草图吗?
  • 最后一句可能有错别字?标题表示对数和线性,但 qu 表示两个对数刻度

标签: python matplotlib scipy


【解决方案1】:

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')

上面的代码产生:

(杂项)这是对标题和右侧没有刻度线的一个非常小的修复:

# 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')

添加这些行将为您提供:

【讨论】:

  • 欢迎来到 SO。在这种情况下 - 回答评论中提出的问题 - 您实际上完全可以接受关于 SO 的全新问题,可能链接回源,然后回答您自己的问题。见stackoverflow.com/help/self-answer
【解决方案2】:

我假设你想要在原点附近线性,记录得更远——因为 `symlog' 反过来——我想不出像这样看起来不错的 data,但是你可以把它和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()

【讨论】:

  • 谢谢。实际上,我想要 x 轴上的 log+linear 而不是 y 的组合。但我认为你的代码应该很容易适应。我会尽快尝试的。我想做 symlog 所做的事情,但顺序颠倒了。 symlog 在 x 轴上做线性+对数。我想要 x 轴上的 log+linear。
  • 只要共享轴使用相同的刻度就可以了;是的,make_axes_locatable 可以在任一方向添加轴。 (或两者兼有。)
【解决方案3】:

此解决方案对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()

【讨论】:

    猜你喜欢
    • 2016-12-15
    • 2018-05-22
    • 2013-07-18
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 2012-05-22
    相关资源
    最近更新 更多