J S*_*pen 18 python numpy matplotlib scipy
我正在尝试创建一个图,但我只想让ticklabels显示如上所示的对数比例.我只希望显示50,500和2000的次要标签.无论如何指定要显示的次要刻度标签?我一直试图解决这个问题,但没有找到一个好的解决方案.我能想到的就是获取minorticklabels()并将fontsize设置为0.这显示在第一段代码下面.我希望有一个更清洁的解决方案.
另一件事是改变颜色栏中的勾选标签的大小,我还没想到.如果有人知道这样做的方法,请告诉我,因为我没有看到颜色栏中的方法很容易做到这一点.
第一个代码:
fig = figure(figto)
ax = fig.add_subplot(111)
actShape = activationTrace.shape
semitones = arange(actShape[1])
freqArray = arange(actShape[0])
X,Y = meshgrid(self.testFreqArray,self.testFreqArray)
Z = sum(activationTrace[:,:,beg:end],axis=2)
surf = ax.contourf(X,Y,Z, 8, cmap=cm.jet)
ax.set_position([0.12,0.15,.8,.8])
ax.set_ylabel('Log Frequency (Hz)')
ax.set_xlabel('Log Frequency (Hz)')
ax.set_xscale('log')
ax.set_yscale('log')
ax.xaxis.set_minor_formatter(FormatStrFormatter('%d'))
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
ax.tick_params(axis='both',reset=False,which='both',length=8,width=2)
self.plotSetAxisLabels(ax,22)
self.plotSetAxisTickLabels(ax,18)
cbar = fig.colorbar(surf, shrink=0.5, aspect=20, fraction=.12,pad=.02)
cbar.set_label('Activation',size=18)
return ax, cbar
Run Code Online (Sandbox Code Playgroud)

第二代码:
fig = figure(figto)
ax = fig.add_subplot(111)
actShape = activationTrace.shape
semitones = arange(actShape[1])
freqArray = arange(actShape[0])
X,Y = meshgrid(self.testFreqArray,self.testFreqArray)
Z = sum(activationTrace[:,:,beg:end],axis=2)
surf = ax.contourf(X,Y,Z, 8, cmap=cm.jet)
ax.set_position([0.12,0.15,.8,.8])
ax.set_ylabel('Log Frequency (Hz)')
ax.set_xlabel('Log Frequency (Hz)')
ax.set_xscale('log')
ax.set_yscale('log')
ax.xaxis.set_minor_formatter(FormatStrFormatter('%d'))
ax.yaxis.set_minor_formatter(FormatStrFormatter('%d'))
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
ax.tick_params(axis='both',reset=False,which='both',length=8,width=2)
self.plotSetAxisLabels(ax,22)
self.plotSetAxisTickLabels(ax,18)
cbar = fig.colorbar(surf, shrink=0.5, aspect=20, fraction=.12,pad=.02)
cbar.set_label('Activation',size=18)
count = 0
for i in ax.xaxis.get_minorticklabels():
if (count%4 == 0):
i.set_fontsize(12)
else:
i.set_fontsize(0)
count+=1
for i in ax.yaxis.get_minorticklabels():
if (count%4 == 0):
i.set_fontsize(12)
else:
i.set_fontsize(0)
count+=1
return ax, cbar
Run Code Online (Sandbox Code Playgroud)

对于colorbar:另一个快速的问题,如果你不介意,因为试图找出它但不完全确定.我想使用ScalarFormatter可以获得的科学记数法.如何设置小数位数和乘数?我希望它像8x10 ^ 8或.8x10 ^ 9来节省空间而不是把所有那些零.我想在轴对象中有多种方法可以做到这一点,但你认为最好的方法是什么.在更改为ScalarFormatter时,我无法弄清楚如何更改符号.
对于图表:此外,我的数据的点数从46开始,然后连续乘以2 ^(1/12),所以46,49,50,55,58,61 ... 3132.这些都是圆形但接近2 ^(1/12).我决定将主要代码放在这些数字附近更好.是使用固定格式化程序的最佳方法,并且在freqArray中每15个左右使用一个自动收报机.然后在每隔一个频率使用一个小的自动收报机.我可以这样做并仍然保持一个日志轴吗?
Pau*_*aul 115
1)FixedLocator用于静态定义显式刻度位置.
2)Colorbar cbar将具有一个ax属性,可以访问常用的轴方法,包括刻度格式.
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.arange(10,3000,100)
y = np.arange(10,3000,100)
X,Y = np.meshgrid(x,y)
Z = np.random.random(X.shape)*8000000
surf = ax.contourf(X,Y,Z, 8, cmap=plt.cm.jet)
ax.set_ylabel('Log Frequency (Hz)')
ax.set_xlabel('Log Frequency (Hz)')
ax.set_xscale('log')
ax.set_yscale('log')
ax.xaxis.set_minor_formatter(plt.FormatStrFormatter('%d'))
# defining custom minor tick locations:
ax.xaxis.set_minor_locator(plt.FixedLocator([50,500,2000]))
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
ax.tick_params(axis='both',reset=False,which='both',length=8,width=2)
cbar = fig.colorbar(surf, shrink=0.5, aspect=20, fraction=.12,pad=.02)
cbar.set_label('Activation',size=18)
# access to cbar tick labels:
cbar.ax.tick_params(labelsize=5)
plt.show()
Run Code Online (Sandbox Code Playgroud)

编辑
如果你想要勾选marls,但是你想要有选择地显示标签,我认为你的迭代没有任何问题,除了我可能会使用set_visible而不是使fontsize为零.
您可以使用FuncFormatter可以使用刻度线的值或位置决定是否显示来更好地控制:
def show_only_some(x, pos):
s = str(int(x))
if s[0] in ('2','5'):
return s
return ''
ax.xaxis.set_minor_formatter(plt.FuncFormatter(show_only_some))
Run Code Online (Sandbox Code Playgroud)