如何在symlog图中保持刻度间距相等?

4 投票
1 回答
2005 浏览
提问于 2025-04-17 21:33

我做了一个symlog图,因为我想在对数尺度上绘图,同时有些数值是负数。但是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()

输出的图像是这样的: 在这里输入图片描述

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

1 个回答

4

这是因为你指定了 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()

在这里输入图片描述

撰写回答