matplotlib 如何在x logscale轴上显示和增加次要和主要刻度的大小

iugsix8n  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(122)

我正在绘制(logscale,symlog)数据图。我很难在x轴(logscale)上显示具有可见高度和宽度的次要刻度。
在x轴上,我有从1 e-8到1的值。在开始时,由于我不知道的原因,我只有“2的10次方”间隔,即我在x轴上得到这样的专业值:

  1. [1e-8, 1e-6, 1e-4, 1e-2, 1]

我通过显式地执行以下操作来修复此问题:

  1. # Important otherwise missing ticks
  2. plt.xticks([1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1])

所以现在,我有了下面的图:

正如你所看到的,每个10次方的小刻度都不见了,我不知道如何显示它们,即使每个10次方之间没有太多的空间。
下面是我用来生成这个图的代码:

  1. # Modules import
  2. import sys
  3. import numpy as np
  4. import scipy.integrate as pyint
  5. import os
  6. from os import path
  7. import glob
  8. import scipy
  9. import re
  10. import shutil
  11. import matplotlib.pyplot as plt
  12. from matplotlib import ticker
  13. # Initialize figure
  14. fig = plt.figure(num=None, figsize=(12, 12), facecolor='w', edgecolor='k')
  15. fig.patch.set_facecolor('white')
  16. fig.patch.set_alpha(1)
  17. # Control on graphic
  18. ax = plt.gca()
  19. # Increase space between x-axis and x-label
  20. ax.tick_params(axis = 'x', which = 'major', pad = 15)
  21. # Lebels on axis
  22. plt.xlabel('Step $\Omega_{m}$', fontsize = 20)
  23. plt.ylabel('$C_{\ell}^{\'}$($\Omega_{m}$) relative', fontsize = 20)
  24. plt.xticks(fontsize = 20)
  25. plt.yticks(fontsize = 20)
  26. plt.xscale('log')
  27. plt.grid()
  28. # Important otherwise missing ticks
  29. plt.xticks([1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1])
  30. # Title : different multipoles
  31. 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'
  32. plt.title(string_intel, fontsize = 20, pad = 25)
  33. colorSimplePlot = ['b', 'r', 'g', 'k', 'y', 'm', 'c']
  34. # Plot command
  35. for idMultipole in range(len(multipole)):
  36. plt.plot(stepNewArray[:], eval('matrixCl_der_'+str(der)+'[:, idMultipole, idParam, lx, ly]'), colorSimplePlot[idMultipole])
  37. # Chose if negative or positive derivatives
  38. # Plot with symlog or log along y-axis
  39. # 1) For example : GG at z = 2.038 and l = 366
  40. # needs symlog with lintreshy = 1e-9
  41. # 2) For example : LL at z = 2.038 and l = 366
  42. # needs nothing (even no log)
  43. if (type_GG_LL_GL == 'GG'):
  44. plt.yscale('symlog', linthreshy = 1e-9)
  45. elif (type_GG_LL_GL == 'LL'):
  46. plt.yscale('linear')
  47. # Legend
  48. plt.legend(['$l = 366.42$', '$l = 1079.28$', '$l = 1792.14$', '$l = 2505$',\
  49. '$l = 3217.85$', '$l = 3930.71$', '$l = 4643.57$'], fontsize=15, loc='best')
  50. # Save plot
  51. plt.savefig('Cl_derivative_Omega_m.pdf')

任何人都可以看到我必须做什么来显示x轴对数刻度的每个间隔的次要xtick?
我还试着补充说:

  1. from matplotlib import ticker
  2. from matplotlib.ticker import LogLocator
  3. ax.tick_params(axis = 'x', which = 'major', pad = 15)
  4. ax.xaxis.set_major_locator(LogLocator(base=10))
  5. 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命令,即通过在我的代码片段中执行:

  1. minors = (0.1**np.arange(9)[:,None] * np.arange(0.1,1,0.1)).ravel()
  2. plt.yticks(fontsize = 20)
  3. plt.xscale('log')
  4. # Important otherwise missing ticks
  5. plt.xticks([1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1], fontsize = 40)
  6. plt.xticks(minors, fontsize = 20)
  7. plt.grid()

我尝试为手动主刻度设置大字体(fontsize = 40),但它不出现。顺便问一下,为什么grid模式消失了?

bgtovc5b

bgtovc5b1#

您可以像这样生成次要tick:

  1. minors = (0.1**np.arange(9)[:,None] * np.arange(0.1,1,0.1)).ravel()
  2. plt.xscale('log')
  3. plt.xticks(minors);

输出量:

相关问题