在matplotlib中刻度轴的格式 [英] ticks format of an axis in matplotlib

查看:115
本文介绍了在matplotlib中刻度轴的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用matplotlib绘制干净的图形. 这是我的代码:

I'm trying to draw clean graphs using matplotlib. Here is my code:

fig = plt.figure(figsize = (6,6))
plt.grid(True)
plt.xlabel('time (s)',fontweight='bold')
plt.ylabel('density',fontweight='bold')
plt.plot(data1, data2, color = 'y', linewidth = 2)
plt.show()

data2中的浮点数介于0.0001和0.001之间,所以当我这样做时,y轴具有类似'0.0001''0.0002'等的刻度. 我如何强制刻度线采用科学计数法("1e-3","1e-4"等)? thx:)

the floats in data2 lies between 0.0001 and 0.001, so When I do this, the y axis has ticks like '0.0001' '0.0002' etc. How can I force the ticks to be in scientific notation ('1e-3', '1e-4' etc. ) ? thx :)

推荐答案

将其设置为1e-04:

import matplotlib.pyplot as plt
import matplotlib.ticker as mtick

data1 = [1,2,3,4,5]
data2 = [1e4,3e4,4e4,2e4,5e4]
fig = plt.figure(figsize = (6,6))
plt.grid(True)
plt.xlabel('time (s)',fontweight='bold')
plt.ylabel('density',fontweight='bold')
plt.plot(data1, data2, color = 'y', linewidth = 2)
plt.gca().yaxis.set_major_formatter(mtick.FormatStrFormatter('%.0e')) 
plt.show()

这篇关于在matplotlib中刻度轴的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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