【问题标题】:Plotting scientific format on the axis label and different size minor formatter text size在轴标签和不同大小的次要格式化程序文本大小上绘制科学格式
【发布时间】:2011-07-26 10:43:48
【问题描述】:

我写了下面的代码来输出数字,但仍然需要弄清楚两件事。

1) 如何在轴上获得小刻度标签的科学记数法。由于我只让它显示 5000,我假设我可以在 showOnlySomeTicks 函数中更改它以返回科学格式的字符串。有一个更好的方法吗??也是仅显示 5000 个次要刻度线的更好方法(例如 5e5)。 2)我想让次要刻度标签尺寸小于主要刻度标签尺寸。我该怎么办?

为任何帮助干杯

import matplotlib.pyplot as plt
import matplotlib.ticker as tick
from numpy import load, sqrt, shape, size

def clear_spines(ax):
    ax.spines['top'].set_color('none')
    ax.spines['right'].set_color('none')
def set_spineLineWidth(ax, lineWidth):
    for i in ax.spines.keys():
        ax.spines[i].set_linewidth(lineWidth)
def showOnlySomeTicks(x, pos):
    s = str(int(x))
    if x == 5000:
        return s
    return ''


plt.close('all')
spineLineWidth = 0.5
inFile = '7semiat220'
outFile = inFile
inExt = '.npz'
outExt = ['.eps','.pdf','.tif','.png']
mydpi = 300
storeMat = load(inFile+inExt)
fig_width_pt = 246.0  
inches_per_pt = 1.0/72.27               
golden_mean = (sqrt(5)-1.0)/2.0         
fig_width = fig_width_pt*inches_per_pt  
fig_height = 2 #fig_width*golden_mean       
fig_size = [fig_width,fig_height]
tick_size = 9
fontlabel_size = 10.5
params = {'backend': 'wxAgg', 'axes.labelsize': fontlabel_size, 'text.fontsize': fontlabel_size, 'legend.fontsize': fontlabel_size, 'xtick.labelsize': tick_size, 'ytick.labelsize': tick_size, 'text.usetex': True, 'figure.figsize': fig_size}
plt.rcParams.update(params)
x = storeMat['freq']
y = storeMat['mag']
sizeX = x.size
fig = plt.figure(1)
#figure(num=None, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')
#fig.set_size_inches(fig_size)
plt.clf()
ax = plt.axes([0.125,0.2,0.95-0.125,0.95-0.2])
plt.plot(x,y,'k',label='$\sin(x)$')
plt.xscale('log')
plt.xlim(x[sizeX-1]*.95,x[0]*1.05)
plt.xlabel('Log Frequency (Hz)')
plt.ylabel('Magnitude')
set_spineLineWidth(ax,spineLineWidth)
clear_spines(ax)
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
ax.xaxis.set_minor_formatter(tick.FuncFormatter(showOnlySomeTicks))
#plt.legend()
for i in outExt:
    plt.savefig(outFile+i, dpi = mydpi)
#def grayConvert()
#Image.open('color.png').convert('L').save('bw.png') #direct method
#image=Image.open('colored_image.png').convert("L") #other manipulations can use this method
#arr=np.asarray(image)
#p.figimage(arr,cmap=cm.Greys_r)
#p.savefig('grayed.png')

【问题讨论】:

  • matplotlib 特有的问题在 matplotlib 用户邮件列表中可能会更好。

标签: python numpy matplotlib


【解决方案1】:

您必须使用格式化程序和定位器的组合

定位器会告诉你应该把勾放在哪里。

格式化程序将返回要在该位置显示的字符串。

见:Ticker module documentation

ax.xaxis.set_minor_locator( xLocator )
ax.xaxis.set_minor_formatter( xminorFormatter )

【讨论】:

  • 我已经在使用每个定义的次要格式化程序 showOnlySomeTicks 并且次要日志轴格式化程序是默认值,并且适用于我想要刻度的位置。我只是想知道如何在 showOnlyTicks 中获得像 5e3 或等效的科学记数法。我知道您可以使用 %e 但不确定如何格式化尾随零 5e+03。你能把它做成5e3吗?干杯,杰夫
猜你喜欢
  • 1970-01-01
  • 2013-08-19
  • 1970-01-01
  • 2021-09-23
  • 2018-05-19
  • 2017-03-21
  • 2010-09-11
  • 2010-11-12
  • 2017-01-03
相关资源
最近更新 更多