Skip to main content
 首页 » 编程设计

python之我怎样才能在符号对数图中的刻度之间保持相同的长度

2024年11月01日9shangdawei

我制作了一个符号对数图,因为我想在一些数量为负的情况下以对数标度绘制。但是 y 轴刻度被搞砸了。刻度线之间的长度不一样。这是我编写的用于绘图的代码:

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib import rc 
import uncertainties 
from uncertainties import ufloat 
from uncertainties.umath import * 
import uncertainties.unumpy as unp 
 
ANISO_POLY=['2','3','4','5'] 
ST_INT=['3','4','5'] 
for st in ST_INT: 
        j=0 
        rc('text', usetex=True) 
        rc('font', family='serif') 
        fig = plt.figure() 
        ax = fig.add_subplot(1,1,1) 
        ax.set_xscale('symlog') 
        ax.set_yscale('symlog', linthreshy=0.004) 
 
        for aniso in ANISO_POLY: 
            correlationGalStarfile='/ST_INTEG_LIM.'+st+'.ANIS_POLY_ORD.'+aniso+'/xi.resample.cat' 
            cor=np.loadtxt(correlationGalStarfile) 
            Theta=cor[:,0] 
            GSPlus=cor[:,1];GSPlusErr=cor[:,5] 
            correlationStarfile='ST_INTEG_LIM.'+st+'.ANIS_POLY_ORD.'+aniso+'/xi.cat' 
            scor=np.loadtxt(correlationfile) 
            SSPlus=scor[:,1];SSPlusErr=scor[:,5] 
            GSC = unp.uarray(GSPlus, GSPlusErr) 
            SSC = unp.uarray(SSCorPlus, SSPlusErr) 
            ratio=GSC*abs(GSC)/SSC 
            ErrorXi=unp.std_devs(ratio) 
            Xi=unp.nominal_values(ratio) 
            ax.errorbar(Theta, Xi, yerr=ErrorXi,  fmt='-', color=colors[j], ecolor=colors[j],  capsize=2, capthick=None,label='aniso. poly. ord. '+aniso) 
            j+=1 
 
        ax.set_xlabel(r'$\Theta$', fontsize=20) 
        ax.set_ylabel(r'$\xi^{+}_{sys}$', fontsize=20) 
        ax.set_title('stellar integration limit '+st) 
        ax.set_ylim(-5e-4,5e-4) 
        ax.set_yticks((-1e-4,-1e-5,0.0,1e-5,1e-4)) 
        ax.set_yticklabels([r'$-10^{-4}$',r'$-10^{-5}$' , r'$0.0$', r'$10^{-5}$',r'$10^{-4}$']) 
 
        fontsize=15 
        for tick in ax.xaxis.get_major_ticks(): 
            tick.label1.set_fontsize(fontsize) 
        for tick in ax.yaxis.get_major_ticks(): 
            tick.label1.set_fontsize(fontsize)             
        leg=plt.legend(numpoints=1,loc='upper right', ncol=1,fontsize=15) 
        leg.draw_frame(False) 
        plotfile='Correlation.SIL.'+st+'.pdf' 
        plt.savefig(plotfile, dpi=50, bbox_inches='tight') 
        plt.close() 

输出图如下所示:

如何定义刻度之间的距离?

请您参考如下方法:

这是因为您指定了 linthreshy。

如果您指定一个包含您的刻度位置的线性阈值,那么您将看到接近 0 的线性比例对您的刻度位置的影响。

作为复制它的快速示例:

import matplotlib.pyplot as plt 
 
plt.rc('axes', labelsize=20) 
 
fig, ax = plt.subplots() 
ax.set(xscale='symlog', xlabel=r'$\Theta$', ylabel=r'$\xi^{+}_{sys}$') 
ax.set_yscale('symlog', linthreshy=0.004) 
ax.set_yticks([-1e-4, -1e-5, 0.0, 1e-5, 1e-4]) 
ax.tick_params(labelsize=15) 
 
ax.axis([1e-1, 1e2, -10**-3.5, 10**-3.5]) 
 
fig.tight_layout() 
plt.show() 

如果我们只是将 linthreshy 更改为小于您手动指定的刻度位置,您将看不到线性比例的效果。这段代码的唯一区别是linthreshy=1e-5:

import matplotlib.pyplot as plt 
 
plt.rc('axes', labelsize=20) 
 
fig, ax = plt.subplots() 
ax.set(xscale='symlog', xlabel=r'$\Theta$', ylabel=r'$\xi^{+}_{sys}$') 
ax.set_yscale('symlog', linthreshy=1e-5) 
ax.set_yticks([-1e-4, -1e-5, 0.0, 1e-5, 1e-4]) 
ax.tick_params(labelsize=15) 
 
ax.axis([1e-1, 1e2, -10**-3.5, 10**-3.5]) 
 
fig.tight_layout() 
plt.show()