如何在matplotilb条形图中打开科学记数法? [英] How to turn on scientific notation in matplotilb bar chart?

查看:60
本文介绍了如何在matplotilb条形图中打开科学记数法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在此图中启用科学记数法,以便 y 轴上的数字不会占用太多空间.

当前我的代码是:

 将matplotlib.pyplot导入为plt将Matplotlib导入为mpl将熊猫作为pd导入mpl.rcParams.update({'font.size':15})mpl.rcParams.update({'legend.columnspacing':0.5})energy_cm = 1550835.86856494energy_fm = 1456129.29966378energy_cm_trad = 1393026.50949191energy_fm_trad = 1314814.95236864energy_cm_hw = 1200000energy_fm_hw = 1100000data_energy = {'Algorithm':['Algorithm 1','Algorithm 2'],'SW' : [energy_cm, energy_fm],'HW':[energy_cm_hw,energy_fm_hw],'传统' : [energy_cm_trad, energy_fm_trad]}df_energy = pd.DataFrame(data_energy)宽度 = 0.7fig = plt.figure(figsize=(8, 8))斧= plt.axes()df_energy [[''Algorithm','SW','Trad','HW']].set_index('Algorithm').plot(kind ='bar',legend = True,width = width,rot = 0,ax =ax, color=('sandybrown','rosybrown', 'goldenrod','indianred','tomato','r'))ax.set_ylabel('nJ 中的能量')ax.ticklabel_format(style ='sci',axis ='y')# ax.yaxis.set_major_formatter(scientific_formatter)# ax.ticklabel_format(useOffset=True,axis='y')fig.tight_layout()plt.show()

这是相应的情节:

基本上我的问题是

而不是简单地将指数/偏移量标记在轴的顶部,如下图(来自互联网)

如何让我的绘图使用 matplotlib 中的默认科学记数法?

解决方案

你可以在plt.show()前加上这3行:

mf = mpl.ticker.ScalarFormatter(useMathText=True)mf.set_powerlimits((-2,2))plt.gca().yaxis.set_major_formatter(mf)

另请查看此

I am trying to turn on scientific notation in this plot so that the numbers on the y-axis don't take up so much space.

Currently my code is:

import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd

mpl.rcParams.update({'font.size':15})
mpl.rcParams.update({'legend.columnspacing':0.5})

energy_cm = 1550835.86856494
energy_fm = 1456129.29966378
energy_cm_trad = 1393026.50949191
energy_fm_trad = 1314814.95236864
energy_cm_hw = 1200000
energy_fm_hw = 1100000

data_energy = { 'Algorithm' : ['Algorithm 1', 'Algorithm 2'],
       'SW' : [energy_cm, energy_fm],
       'HW' : [energy_cm_hw, energy_fm_hw],
       'Trad' : [energy_cm_trad, energy_fm_trad]
    }

df_energy = pd.DataFrame(data_energy)

width = 0.7
fig = plt.figure(figsize=(8, 8))
ax = plt.axes()
df_energy[['Algorithm', 'SW', 'Trad', 'HW']].set_index('Algorithm').plot(kind='bar', legend=True, width=width, rot=0, ax=ax, color=('sandybrown','rosybrown', 'goldenrod','indianred','tomato','r'))

ax.set_ylabel('Energy in nJ')

ax.ticklabel_format(style='sci', axis='y')
# ax.yaxis.set_major_formatter(scientific_formatter)
# ax.ticklabel_format(useOffset=True, axis='y')

fig.tight_layout()
plt.show()

And this is the corresponding plot:

Basically my question is the opposite of this one.

I had the same error message and resolved it by changing

ax.ticklabel_format(style='sci')

to

ax.ticklabel_format(style='sci', axis='y')

I tried using FuncFormatter to produce customized scientific notation, but I didn't like the result because each tick on the axis was labeled with the exponent

rather than the exponent/offset simply being marked at the top of the axis like in the following image (from the internet)

How can I get my plot to use the default scientific notation from matplotlib?

解决方案

You can add these 3 lines before plt.show():

mf = mpl.ticker.ScalarFormatter(useMathText=True)
mf.set_powerlimits((-2,2))
plt.gca().yaxis.set_major_formatter(mf)

Check also this link for set_powerlimits()

这篇关于如何在matplotilb条形图中打开科学记数法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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