Python 3.x 如何在matplotlib中设置轴上的科学符号

Python 3.x 如何在matplotlib中设置轴上的科学符号,python-3.x,matplotlib,axis-labels,Python 3.x,Matplotlib,Axis Labels,我试图用两个独立的x轴绘制一个图形。一个是阀门打开,另一个是相应的泄漏率。我设法使它工作得很好,尽管次轴的格式并不总是显示如下图所示的科学符号 如何强制显示科学符号,使标签不会重叠? 以下是我正在使用的脚本: #HEADERS import numpy as np import matplotlib.pyplot as plt import matplotlib.ticker from matplotlib import rc rc('font', **{'family':'sans-se

我试图用两个独立的x轴绘制一个图形。一个是阀门打开,另一个是相应的泄漏率。我设法使它工作得很好,尽管次轴的格式并不总是显示如下图所示的科学符号 如何强制显示科学符号,使标签不会重叠? 以下是我正在使用的脚本:

#HEADERS
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.ticker

from matplotlib import rc
rc('font', **{'family':'sans-serif','sans-serif':['Helvetica']})
rc('text', usetex=True)
#/HEADERS

turns = np.array([11.000, 11.500, 11.750, 12.000, 12.250, 12.375])
leak = np.array([3.89e-05, 4.63e-05, 1.67e-04, 1.45000000e-03, 8.61e-03, 1.71e-02])
pressure1 = np.array([7.9e-07, 3.0e-06, 3.5e-05, 6.1e-04, 5.1e-03, 1.8e-02])
pressure2 = np.array([8.22e-07, 8.22e-07, 8.71e-07, 1.8e-06, 1.150e-05, 7.24e-05])
pressure3 = np.array([2e-06, 2e-06, 2e-06, 1.2e-05, 1.2e-04, 6e-04])

fig = plt.figure(num='valve', figsize = (6.68, 6.68*1.3))
fig, ax1 = plt.subplots()
ax1.plot(turns, pressure1, 'r.', label= '$P_1$')
ax1.plot(turns, pressure2, 'b.', label= '$P_2$')
ax1.plot(turns, pressure3,'k.', label= '$P_3$')

plt.legend()

plt.minorticks_on()
plt.grid(b = True, which = 'major', axis = 'both')

ax1.errorbar(turns, pressure1, yerr = .4*pressure1, fmt='none', ecolor = 'k', elinewidth = 1, capsize = 1, label= '$P_{1err}$')
ax1.errorbar(turns, pressure2, yerr = .15*pressure2, fmt='none', ecolor = 'k', elinewidth = 1, capsize = 1, label= '$P_{2err}$')

plt.rc('text', usetex=True)
plt.rc('font', family='serif')

ax1.set_yscale('log', nonposy = 'mask')
ax1.set_ylabel(r'$P$')
ax1.set_xscale('linear')
ax1.set_xlabel('Opening (turns)')
plt.minorticks_on()
#plt.grid(b = True, which = 'major', axis = 'both')

#adding a secondary x-axis above
ax2 = ax1.twiny()
ax2.set_xlim(ax1.get_xlim())
new_tick_locations = turns
new_tick_label = leak #dtype here ?
ax2.set_xticks(new_tick_locations)
ax2.set_xticklabels(new_tick_label)
# I tried those commands from other threads but they all result in an error.
#ax2.xaxis.set_scientific(True)
#ax2.get_xaxis().set_major_formatter((matplotlib.ticker.Formatter(set_scientific(True)))
#ax2.get_xaxis().set_major_formatter().set_scientific(True)

ax2.set_xlabel(r'Leak rate (mbar$\times$L/s)')

plt.tight_layout()

#export png
plt.savefig(('export.png'), format = 'png', transparent=False, dpi = 300)
plt.show()
我正在使用Python 3.6


谢谢您的帮助。

因为您覆盖了刻度,所以您可以自己格式化刻度,也可以旋转刻度以获得更多空间:

new_tick_label = ['{:5.2e}'.format(x) for x in leak] 
ax2.set_xticks(new_tick_locations)
ax2.set_xticklabels(new_tick_label, rotation=30)
结果:


由于您覆盖了记号,因此您可以自己格式化记号,也可以旋转记号以获得更多空间:

new_tick_label = ['{:5.2e}'.format(x) for x in leak] 
ax2.set_xticks(new_tick_locations)
ax2.set_xticklabels(new_tick_label, rotation=30)
结果:


谢谢,我刚试过,但现在它显示了以下标签作为科学数字。好像ax2标签被忽略了。用另一种方法修复了此问题。谢谢,我刚刚尝试过,但现在它将下面的标签显示为科学数字。好像忽略了ax2标签。使用不同的方法修复了此问题。对于一般解决方案,请查看。对于一般解决方案,请查看的可能重复项。对于一般解决方案,请查看的可能重复项