如何在matplotilb条形图中打开科学计数法?

问题描述 投票:1回答:1

我正在尝试在此图中打开科学计数法,以使y轴上的数字不会占用太多空间。

当前我的代码是:

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' : ['Count Min', 'Flajolet Martin'],
       '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()

这是对应的情节:

The corresponding plot

基本上我的问题是opposite of this one

我有相同的错误消息,并通过更改]解决了它>

ax.ticklabel_format(style='sci')

to

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

[我尝试使用FuncFormatter来生成自定义的科学符号,但我不喜欢该结果,因为轴上的每个刻度都用指数标记了

exponent_on_each_image

而不是像下面的图像一样(从互联网上)仅在轴的顶部标记指数/偏移量

enter image description here

如何获得我的图以使用matplotlib中的默认科学计数法?

我正在尝试在此图中打开科学计数法,以使y轴上的数字不会占用太多空间。当前我的代码是:将plt导入matplotlib.pyplot作为mpl导入matplotlib ...

python pandas matplotlib scientific-notation
1个回答
1
投票

您可以在plt.show()之前添加这3行:

© www.soinside.com 2019 - 2024. All rights reserved.