我正在尝试创建一个绘图,但我只想让刻度标签显示为如上所示的对数刻度的位置。我只希望显示 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
第二个代码:
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
对于颜色条: 如果您不介意,另一个快速问题是因为试图弄清楚但不完全确定。我想使用可以通过 ScalarFormatter 获得的科学记数法。如何设置小数位数和乘数?我希望它像 8x10^8 或 .8x10^9 以节省空间,而不是放置所有这些零。我认为在轴对象内有多种方法可以做到这一点,但你认为最好的方法是什么。更改为 ScalarFormatter 时,我不知道如何更改符号。
对于图表: 此外,我的数据点从 46 开始,然后连续乘以 2^(1/12),即 46、49、50、55、58、61...3132。这些都是四舍五入的,但接近 2^(1/12)。我决定最好将主要股票代码放在靠近这些数字的地方。是使用固定格式化程序并在 freqArray 中每隔 15 左右使用一次代码的最佳方式。然后每隔一个频率使用一个次要代码。我可以这样做并且仍然保持日志轴吗?
最佳答案
- 使用
FixedLocator
静态定义明确的刻度位置。 - Colorbar
cbar
将具有.ax
属性,该属性将提供对包括刻度格式在内的常用轴方法的访问。这不是对axes
的引用(例如ax1
、ax2
等)。
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()
编辑
如果您想要刻度线,但又想有选择地显示标签,我认为您的迭代没有任何问题,除了我可能使用 set_visible
而不是将字体大小设为零。
您可能会喜欢使用 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))
关于python - Matplotlib 所以日志轴在指定点只有次要刻度线标签。还可以更改颜色栏中刻度标签的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6567724/