Matplotlib:如何在 x logscale 轴上显示和增加次要和主要刻度的大小
Matplotlib : how to make appear and increase the size of minor and major ticks on x logscale axis
我正在绘制 (logscale,symlog) 数据图。我很难在 x 轴(logscale)上显示具有可见高度和宽度的小刻度。
在 x 轴上,我的值从 1e-8 到 1。开始时,由于我不知道的原因,我只有一个“2 的 10 次方”间隔,即我得到在 x_axis 的地块上,专业的价值观是这样的:
[1e-8, 1e-6, 1e-4, 1e-2, 1]
我明确地解决了这个问题:
# Important otherwise missing ticks
plt.xticks([1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1])
所以现在,我有下图:
如您所见,每个 10 次方的小刻度都丢失了,我不知道如何显示它们,即使每个 10 次方之间没有很多 space。
这是我用来生成此图的代码:
# Modules import
import sys
import numpy as np
import scipy.integrate as pyint
import os
from os import path
import glob
import scipy
import re
import shutil
import matplotlib.pyplot as plt
from matplotlib import ticker
# Initialize figure
fig = plt.figure(num=None, figsize=(12, 12), facecolor='w', edgecolor='k')
fig.patch.set_facecolor('white')
fig.patch.set_alpha(1)
# Control on graphic
ax = plt.gca()
# Increase space between x-axis and x-label
ax.tick_params(axis = 'x', which = 'major', pad = 15)
# Lebels on axis
plt.xlabel('Step $\Omega_{m}$', fontsize = 20)
plt.ylabel('$C_{\ell}^{\'}$($\Omega_{m}$) relative', fontsize = 20)
plt.xticks(fontsize = 20)
plt.yticks(fontsize = 20)
plt.xscale('log')
plt.grid()
# Important otherwise missing ticks
plt.xticks([1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1])
# Title : different multipoles
string_intel = '(CAMB-0.1.7) - '+type_GG_LL_GL+' - $C_{\ell}^{\'}$ relative of '+paramLatexArray[idParam]+'('+str(der)+' pts) at $z$ = '+str(zrange[idRedshift])+' - '+str(numel)+' pts'
plt.title(string_intel, fontsize = 20, pad = 25)
colorSimplePlot = ['b', 'r', 'g', 'k', 'y', 'm', 'c']
# Plot command
for idMultipole in range(len(multipole)):
plt.plot(stepNewArray[:], eval('matrixCl_der_'+str(der)+'[:, idMultipole, idParam, lx, ly]'), colorSimplePlot[idMultipole])
# Chose if negative or positive derivatives
# Plot with symlog or log along y-axis
# 1) For example : GG at z = 2.038 and l = 366
# needs symlog with lintreshy = 1e-9
# 2) For example : LL at z = 2.038 and l = 366
# needs nothing (even no log)
if (type_GG_LL_GL == 'GG'):
plt.yscale('symlog', linthreshy = 1e-9)
elif (type_GG_LL_GL == 'LL'):
plt.yscale('linear')
# Legend
plt.legend(['$l = 366.42$', '$l = 1079.28$', '$l = 1792.14$', '$l = 2505$',\
'$l = 3217.85$', '$l = 3930.71$', '$l = 4643.57$'], fontsize=15, loc='best')
# Save plot
plt.savefig('Cl_derivative_Omega_m.pdf')
任何人都可以看到我必须做些什么来显示 x 轴对数刻度的每个间隔的次要 xticks 吗?
我也尝试添加:
from matplotlib import ticker
from matplotlib.ticker import LogLocator
ax.tick_params(axis = 'x', which = 'major', pad = 15)
ax.xaxis.set_major_locator(LogLocator(base=10))
ax.xaxis.set_major_locator(LogLocator(base=10, subs=[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0]))
但这行不通...
第二次,我也想控制这些小刻度的大小。
感谢@Quand Huang 做了下图:
您会注意到 Major ticks 和 grid 命令都消失了,即通过在我的代码片段中执行:
minors = (0.1**np.arange(9)[:,None] * np.arange(0.1,1,0.1)).ravel()
plt.yticks(fontsize = 20)
plt.xscale('log')
# Important otherwise missing ticks
plt.xticks([1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1], fontsize = 40)
plt.xticks(minors, fontsize = 20)
plt.grid()
我尝试为手动主要刻度 (fontsize = 40
) 设置大字体,但它没有出现。对了,为什么grid
模式消失了?
您可以像这样生成次要刻度:
minors = (0.1**np.arange(9)[:,None] * np.arange(0.1,1,0.1)).ravel()
plt.xscale('log')
plt.xticks(minors);
输出:
我正在绘制 (logscale,symlog) 数据图。我很难在 x 轴(logscale)上显示具有可见高度和宽度的小刻度。
在 x 轴上,我的值从 1e-8 到 1。开始时,由于我不知道的原因,我只有一个“2 的 10 次方”间隔,即我得到在 x_axis 的地块上,专业的价值观是这样的:
[1e-8, 1e-6, 1e-4, 1e-2, 1]
我明确地解决了这个问题:
# Important otherwise missing ticks
plt.xticks([1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1])
所以现在,我有下图:
如您所见,每个 10 次方的小刻度都丢失了,我不知道如何显示它们,即使每个 10 次方之间没有很多 space。
这是我用来生成此图的代码:
# Modules import
import sys
import numpy as np
import scipy.integrate as pyint
import os
from os import path
import glob
import scipy
import re
import shutil
import matplotlib.pyplot as plt
from matplotlib import ticker
# Initialize figure
fig = plt.figure(num=None, figsize=(12, 12), facecolor='w', edgecolor='k')
fig.patch.set_facecolor('white')
fig.patch.set_alpha(1)
# Control on graphic
ax = plt.gca()
# Increase space between x-axis and x-label
ax.tick_params(axis = 'x', which = 'major', pad = 15)
# Lebels on axis
plt.xlabel('Step $\Omega_{m}$', fontsize = 20)
plt.ylabel('$C_{\ell}^{\'}$($\Omega_{m}$) relative', fontsize = 20)
plt.xticks(fontsize = 20)
plt.yticks(fontsize = 20)
plt.xscale('log')
plt.grid()
# Important otherwise missing ticks
plt.xticks([1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1])
# Title : different multipoles
string_intel = '(CAMB-0.1.7) - '+type_GG_LL_GL+' - $C_{\ell}^{\'}$ relative of '+paramLatexArray[idParam]+'('+str(der)+' pts) at $z$ = '+str(zrange[idRedshift])+' - '+str(numel)+' pts'
plt.title(string_intel, fontsize = 20, pad = 25)
colorSimplePlot = ['b', 'r', 'g', 'k', 'y', 'm', 'c']
# Plot command
for idMultipole in range(len(multipole)):
plt.plot(stepNewArray[:], eval('matrixCl_der_'+str(der)+'[:, idMultipole, idParam, lx, ly]'), colorSimplePlot[idMultipole])
# Chose if negative or positive derivatives
# Plot with symlog or log along y-axis
# 1) For example : GG at z = 2.038 and l = 366
# needs symlog with lintreshy = 1e-9
# 2) For example : LL at z = 2.038 and l = 366
# needs nothing (even no log)
if (type_GG_LL_GL == 'GG'):
plt.yscale('symlog', linthreshy = 1e-9)
elif (type_GG_LL_GL == 'LL'):
plt.yscale('linear')
# Legend
plt.legend(['$l = 366.42$', '$l = 1079.28$', '$l = 1792.14$', '$l = 2505$',\
'$l = 3217.85$', '$l = 3930.71$', '$l = 4643.57$'], fontsize=15, loc='best')
# Save plot
plt.savefig('Cl_derivative_Omega_m.pdf')
任何人都可以看到我必须做些什么来显示 x 轴对数刻度的每个间隔的次要 xticks 吗?
我也尝试添加:
from matplotlib import ticker
from matplotlib.ticker import LogLocator
ax.tick_params(axis = 'x', which = 'major', pad = 15)
ax.xaxis.set_major_locator(LogLocator(base=10))
ax.xaxis.set_major_locator(LogLocator(base=10, subs=[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0]))
但这行不通...
第二次,我也想控制这些小刻度的大小。
感谢@Quand Huang 做了下图:
您会注意到 Major ticks 和 grid 命令都消失了,即通过在我的代码片段中执行:
minors = (0.1**np.arange(9)[:,None] * np.arange(0.1,1,0.1)).ravel()
plt.yticks(fontsize = 20)
plt.xscale('log')
# Important otherwise missing ticks
plt.xticks([1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1], fontsize = 40)
plt.xticks(minors, fontsize = 20)
plt.grid()
我尝试为手动主要刻度 (fontsize = 40
) 设置大字体,但它没有出现。对了,为什么grid
模式消失了?
您可以像这样生成次要刻度:
minors = (0.1**np.arange(9)[:,None] * np.arange(0.1,1,0.1)).ravel()
plt.xscale('log')
plt.xticks(minors);
输出: