如何在matplotlib中的轴上设置科学计数法 [英] How to set scientific notation on axis in matplotlib

查看:1928
本文介绍了如何在matplotlib中的轴上设置科学计数法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制带有两个单独的x轴的图形.一个是打开一些阀门,另一个是相应的泄漏率.我设法使它工作得很好,尽管该副轴的格式并不总是像下面的图所示那样显示出科学的记号. 糟糕的重叠标签,请参见上轴 如何强制科学表示法显示,以使标签不会重叠? 这是我正在使用的脚本:

I am trying to plot a graph with two separate x-axis. One being some valve openning and the other the corresponding leak rate. I managed to make it work pretty well, though the format of that secondary axis doesn't always show scientific notations as seen on the figure down bellow Awful overlapping labels, see the upper axis How to force scientific notation display so that the labels wont overlap? Here is the script I am using:

#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.

I'm using Python 3.6.

感谢您的帮助.

推荐答案

由于您覆盖了刻度线,因此您可以自行设置格式并旋转它们以留出更多空间:

Since you override the ticks, you can format them yourself and rotate them as well for more space:

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)

结果:

这篇关于如何在matplotlib中的轴上设置科学计数法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆