python画图坐标轴不出现小数,在matplotlib图的轴上显示小数位和科学计数法

在使用matplotlib进行Python绘图时,遇到坐标轴数值过大,希望以科学计数法并保留两位小数的形式展示。通过使用`matplotlib.ticker.FormatStrFormatter`,可以轻松实现这一需求,例如:`ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.2e'))`。这将确保Y轴的每个刻度都以两位小数的科学计数法形式显示。
摘要由CSDN通过智能技术生成

I am plotting some big numbers with matplotlib in a pyqt program using python 2.7. I have a y-axis that ranges from 1e+18 to 3e+18 (usually). I'd like to see each tick mark show values in scientific notation and with 2 decimal places. For example 2.35e+18 instead of just 2e+18 because values between 2e+18 and 3e+18 still read just 2e+18 for a few tickmarks. Here is an example of that problem.

import numpy as np

import matplotlib.pyplot as plt

fig = plt.figure()

ax = fig.add_subplot(111)

x = np.linspace(0, 300, 20)

y = np.linspace(0,300, 20)

y = y*1e16

ax.plot(x,y)

ax.get_xaxis().set_major_formatter(plt.LogFormatter(10, labelOnlyBase=False))

ax.get_yaxis().set_major_formatter(plt.LogFormatter(10, labelOnlyBase=False))

plt.show()

解决方案

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值