matplotlib 设置日志轴上的次要刻度标签间距,并更改颜色条刻度标签大小

zed5wv10  于 2023-10-24  发布在  其他
关注(0)|答案(2)|浏览(166)

我试图创建一个图,但我只是想ticklabels显示如图所示的日志规模如上所示。我只想为50,500和2000显示次要ticklabels。无论如何都要指定次要ticklabels显示??我一直在试图弄清楚这一点,但还没有找到一个很好的解决方案。我能想到的是得到minorticklabels()并将fontsize设置为0。这在第一段代码下面显示。我希望有一个更干净的解决方案。
另一件事是改变colorbar中ticklabels的大小,我还没有弄清楚。如果有人知道一种方法来做到这一点,请让我知道,因为我没有看到一个方法在colorbar中,很容易做到这一点。
第一个代码:

  1. fig = figure(figto)
  2. ax = fig.add_subplot(111)
  3. actShape = activationTrace.shape
  4. semitones = arange(actShape[1])
  5. freqArray = arange(actShape[0])
  6. X,Y = meshgrid(self.testFreqArray,self.testFreqArray)
  7. Z = sum(activationTrace[:,:,beg:end],axis=2)
  8. surf = ax.contourf(X,Y,Z, 8, cmap=cm.jet)
  9. ax.set_position([0.12,0.15,.8,.8])
  10. ax.set_ylabel('Log Frequency (Hz)')
  11. ax.set_xlabel('Log Frequency (Hz)')
  12. ax.set_xscale('log')
  13. ax.set_yscale('log')
  14. ax.xaxis.set_minor_formatter(FormatStrFormatter('%d'))
  15. ax.yaxis.set_ticks_position('left')
  16. ax.xaxis.set_ticks_position('bottom')
  17. ax.tick_params(axis='both',reset=False,which='both',length=8,width=2)
  18. self.plotSetAxisLabels(ax,22)
  19. self.plotSetAxisTickLabels(ax,18)
  20. cbar = fig.colorbar(surf, shrink=0.5, aspect=20, fraction=.12,pad=.02)
  21. cbar.set_label('Activation',size=18)
  22. return ax, cbar

第二个代码:

  1. fig = figure(figto)
  2. ax = fig.add_subplot(111)
  3. actShape = activationTrace.shape
  4. semitones = arange(actShape[1])
  5. freqArray = arange(actShape[0])
  6. X,Y = meshgrid(self.testFreqArray,self.testFreqArray)
  7. Z = sum(activationTrace[:,:,beg:end],axis=2)
  8. surf = ax.contourf(X,Y,Z, 8, cmap=cm.jet)
  9. ax.set_position([0.12,0.15,.8,.8])
  10. ax.set_ylabel('Log Frequency (Hz)')
  11. ax.set_xlabel('Log Frequency (Hz)')
  12. ax.set_xscale('log')
  13. ax.set_yscale('log')
  14. ax.xaxis.set_minor_formatter(FormatStrFormatter('%d'))
  15. ax.yaxis.set_minor_formatter(FormatStrFormatter('%d'))
  16. ax.yaxis.set_ticks_position('left')
  17. ax.xaxis.set_ticks_position('bottom')
  18. ax.tick_params(axis='both',reset=False,which='both',length=8,width=2)
  19. self.plotSetAxisLabels(ax,22)
  20. self.plotSetAxisTickLabels(ax,18)
  21. cbar = fig.colorbar(surf, shrink=0.5, aspect=20, fraction=.12,pad=.02)
  22. cbar.set_label('Activation',size=18)
  23. count = 0
  24. for i in ax.xaxis.get_minorticklabels():
  25. if (count%4 == 0):
  26. i.set_fontsize(12)
  27. else:
  28. i.set_fontsize(0)
  29. count+=1
  30. for i in ax.yaxis.get_minorticklabels():
  31. if (count%4 == 0):
  32. i.set_fontsize(12)
  33. else:
  34. i.set_fontsize(0)
  35. count+=1
  36. return ax, cbar

对于颜色条:另一个快速的问题,如果你不介意,因为试图弄清楚,但不完全确定.我想使用科学计数法,我可以得到与ScalarFormatter.我如何设置小数位数和乘数??我希望它是8x10^8或.8x10^9来保存空间,而不是把所有的零。我认为有多种方法可以在axes对象中做到这一点,但你认为什么是最好的方法。我不知道如何改变在转换为ScalarFormatter时使用的符号。
对于图表:此外,我的数据从46开始,然后依次乘以2^(1/12),即46,49,50,55,58,61.3132.这些都是四舍五入的,但靠近2^(1/12).我决定最好把主要的代码放在这些数字附近。是使用固定格式的最好方法,并且在freqArray中每隔15个左右使用一个代码。然后在每隔一个频率使用一个次要的股票代码。我可以这样做,仍然保持一个日志轴??

bvjxkvbb

bvjxkvbb1#

1.使用FixedLocator静态定义显式记号位置。

  1. Colorbar cbar将有一个.ax属性,它将提供对常用轴方法的访问,包括tick格式。这不是对axes的引用(例如ax1ax2等)。
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. fig = plt.figure()
  4. ax = fig.add_subplot(111)
  5. x = np.arange(10,3000,100)
  6. y = np.arange(10,3000,100)
  7. X,Y = np.meshgrid(x,y)
  8. Z = np.random.random(X.shape)*8000000
  9. surf = ax.contourf(X,Y,Z, 8, cmap=plt.cm.jet)
  10. ax.set_ylabel('Log Frequency (Hz)')
  11. ax.set_xlabel('Log Frequency (Hz)')
  12. ax.set_xscale('log')
  13. ax.set_yscale('log')
  14. ax.xaxis.set_minor_formatter(plt.FormatStrFormatter('%d'))
  15. # defining custom minor tick locations:
  16. ax.xaxis.set_minor_locator(plt.FixedLocator([50,500,2000]))
  17. ax.yaxis.set_ticks_position('left')
  18. ax.xaxis.set_ticks_position('bottom')
  19. ax.tick_params(axis='both',reset=False,which='both',length=8,width=2)
  20. cbar = fig.colorbar(surf, shrink=0.5, aspect=20, fraction=.12,pad=.02)
  21. cbar.set_label('Activation',size=18)
  22. # access to cbar tick labels:
  23. cbar.ax.tick_params(labelsize=5)
  24. plt.show()

编辑

如果您想要tick marls,但又想有选择地显示标签,我认为您的迭代没有任何问题,只是我可能会使用set_visible而不是将fontsize设置为零。
您可能会喜欢使用FuncFormatter进行更精细的控制,您可以使用tick的值或位置来决定是否显示它:

  1. def show_only_some(x, pos):
  2. s = str(int(x))
  3. if s[0] in ('2','5'):
  4. return s
  5. return ''
  6. ax.xaxis.set_minor_formatter(plt.FuncFormatter(show_only_some))
展开查看全部
vptzau2j

vptzau2j2#

根据@Paul的回答,我创建了以下函数:

  1. def get_formatter_function(allowed_values, datatype='float'):
  2. """returns a function, which only allows allowed_values as axis tick labels"""
  3. def hide_others(value, pos):
  4. if value in allowed_values:
  5. if datatype == 'float':
  6. return value
  7. elif datatype == 'int':
  8. return int(value)
  9. return ''
  10. return hide_others

这是一个更灵活一点。

相关问题