代码之家  ›  专栏  ›  技术社区  ›  Rebel

当轴在具有双Y轴的绘图中具有对数刻度时,Y轴刻度看起来不是10的好幂。

  •  0
  • Rebel  · 技术社区  · 6 年前

    我试着用两个y轴作图,它们都是对数的;y1是左边的y轴,y2是右边的y轴。这里,y1的值是通过将y2值除以要在代码段中定义的某个数字来计算的。由于为Y2轴选择了十进制数字,该数字看起来不正常:

    导入numpy as np 从numpy导入* 将matplotlib导入为mpl 将matplotlib.pyplot导入为plt 从matplotlib导入rcparams,cm 从matplotlib.ticker导入maxnlocator #绘图装饰 xtick_-label_-size,ytick_-label_-size,axles_-label_-size,font_-size,tick_-width,lw,alpha=18,18,20,30,2,0.5,0.5 plt.rcparams['mathtext.fontset']='stix' plt.rcparams['font.family']='stixgeneral' mpl.rcparams['xtick.label size'],mpl.rcparams['ytick.labelsize'],mpl.rcparams['axes.labelsize']=xtick_label_size,ytick_label_size,axes_label_size 一些_数=平均值(数组([0.01614,0.01381,0.02411,0.007436,0.03223])) f,(ax)=plt.子批次(1,1,FigSize=(10100)) ax.set xlim([1E8,3E12]) ax.设定值([3e-1,3e3]) ax.yaxis.set_major_locator(maxnlocator(prune='upper')) ax.设置“ylabel”(“y1”,fontsize=12) ax.set xlabel('x',fontsize=12) ax.set xscale(“日志”,nonposx='clip') ax.set yscale(“日志”,nonposy='clip') ax.xaxis.set_tick_参数(width=tick_width) ax.yaxis.set_tick_参数(width=tick_width) ax.get_xaxis()。勾选_Bottom()。 ax.get_yaxis()。勾选_Left()。 数字=np.数组([1e-4,1e-3,1e-2,1e-1,1,1e1,1e2,1e3]) numberticks=[i*数字中的i的某个数字] axsecond=ax.twinx()。 axsecond.set_ylabel('y2',fontsize=12) axsecond.set yscale(“日志”,nonposy='clip') axsecond.yaxis.set_tick_参数(width=tick_width) axsecond.set时钟(数字) axsecond.set ytickLabels([':g'。格式(i)for i in numberticks]) F.子批次调整(顶部=0.98,底部=0.14,左侧=0.14,右侧=0.98) plt.setp([a.get_xtickLabels()for a in f.axes[:-1]],visible=true) f.紧凑布局 显示() < /代码>

    在得到为Y2定义正确标签的帮助后,我想将这些标签表示为类似于Y1的10的幂。你知道怎么做吗?

    . 一些数字在代码段中定义。由于为Y2轴选择了十进制数字,该数字看起来不正常:

    import numpy as np
    from numpy import *
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    from matplotlib import rcParams, cm
    from matplotlib.ticker import MaxNLocator
    
    #Plotting Decorations
    xtick_label_size, ytick_label_size, axes_label_size, font_size, tick_width, lw, alpha = 18, 18, 20, 30, 2, 0.5, 0.5
    plt.rcParams['mathtext.fontset'] = 'stix'
    plt.rcParams['font.family'] = 'STIXGeneral'
    mpl.rcParams['xtick.labelsize'], mpl.rcParams['ytick.labelsize'], mpl.rcParams['axes.labelsize'] = xtick_label_size, ytick_label_size, axes_label_size
    
    some_number = mean(array([0.01614, 0.01381, 0.02411, 0.007436, 0.03223]))
    f, (ax) = plt.subplots(1, 1, figsize=(10,100))
    
    ax.set_xlim([1e8, 3e12])
    ax.set_ylim([3e-1, 3e3])
    ax.yaxis.set_major_locator(MaxNLocator(prune='upper')) 
    ax.set_ylabel('Y1', fontsize=12)
    ax.set_xlabel('X', fontsize=12)
    ax.set_xscale("log", nonposx='clip')
    ax.set_yscale("log", nonposy='clip')
    ax.xaxis.set_tick_params(width=tick_width)
    ax.yaxis.set_tick_params(width=tick_width)
    ax.get_xaxis().tick_bottom()  
    ax.get_yaxis().tick_left()
    number = np.array([1e-4,1e-3,1e-2,1e-1,1,1e1,1e2,1e3])
    numberticks = [i*some_number for i in number]
    axsecond = ax.twinx()
    axsecond.set_ylabel('Y2', fontsize=12)
    axsecond.set_yscale("log", nonposy='clip')
    axsecond.yaxis.set_tick_params(width=tick_width)
    axsecond.set_yticks(number)
    axsecond.set_yticklabels(['{:g}'.format(i) for i in numberticks])
    
    f.subplots_adjust(top=0.98,bottom=0.14,left=0.14,right=0.98)
    plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=True)
    f.tight_layout()
    plt.show()
    

    在得到为Y2定义正确标签的帮助后,我想将这些标签表示为类似于Y1的10的幂。你知道怎么做吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   ImportanceOfBeingErnest    6 年前

    想法是同步轴限制。也就是说,如果第一个轴的Y限制是 [a,b] ,那么第二个轴的Y限制将需要是 [a*factor,b*factor]

    导入matplotlib.pyplot as plt 将numpy导入为np 因子=0.5 图,ax=plt.子批次()。 ax2=ax.twinx()。 ax.设定值([1,1E3]) ax2.set_ylm(np.array(ax.get_ylm())*系数) ax.设置刻度(“对数”)。 ax2.设置刻度(“log”) 最大绘图([0,1],[1100]) ax2.plot([0,1],np.array([1100])*系数) 显示() < /代码>

    [a*factor, b*factor] .

    import matplotlib.pyplot as plt
    import numpy as np
    
    factor = 0.5
    
    fig, ax = plt.subplots()
    ax2 = ax.twinx()
    
    ax.set_ylim([.1, 1e3])
    ax2.set_ylim(np.array(ax.get_ylim())*factor)
    
    ax.set_yscale("log")
    ax2.set_yscale("log")
    
    
    ax.plot([0,1],[1,100])
    ax2.plot([0,1],np.array([1,100])*factor)
    
    plt.show()
    

    enter image description here