python - 在 matplotlib 中结合对数和线性刻度

标签 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 if 5,我们将在 (-5,5) 上得到一个线性比例。一是局限于产地。我认为简单地选择一个不同的范围应该有效,但事实并非如此。有什么想法吗?

最佳答案

From the response of user1318806 to cphlewis :

Thank you. Actually I wanted a combination of log+linear on the x axis not y. But I assume your code should be easily adaptable.

您好!如果你想在 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

(MISCELLANEOUS)这是对标题的一个非常小的修正,并且右侧没有刻度线:

# 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

关于python - 在 matplotlib 中结合对数和线性刻度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21746491/

相关文章:

python - plt.cm.get_cmap 中可以使用哪些名称?

python-3.x - 使用 NetworkX 进行图形排列和旋转

python - Pandas 重采样错误?

python - 切换seaborn x 和y 轴,但计算原始方向的标准偏差

python - 没有网络进程运行错误 - 在 Heroku 上部署 Django

python - 了解 matplotlib xticks 语法

python - 艾托夫投影,Python

python - 如何绘制标准差

python - 为 scipy.optimize.curve_fit 函数中的各个独立点分配边界

python - 射频 : Convert List of Tuple to List of List