如何在symlog图中的刻度之间保持相同的长度?

时间:2014-03-09 21:25:50

标签: python python-2.7 numpy matplotlib

我制作了一个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()

输出图如下所示: enter image description here

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

1 个答案:

答案 0 :(得分:2)

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

enter image description here

如果我们只是将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()

enter image description here