我可以在matplotlib条形图中转换科学计数法吗? [英] Can I turn of scientific notation in matplotlib bar chart?

查看:433
本文介绍了我可以在matplotlib条形图中转换科学计数法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个条形图,除了y轴的科学记号外,它看起来像我想要的样子.

I have a bar chart that looks like how I want it to look, except for the scientific notation on the y-axes.

使用

ax.yaxis.set_major_formatter(tick)

这不起作用.另外,我尝试检查这是否是一个偏移问题,但它应该显示一个"+"号,在这种情况下不是.

which didn't work. Also, I tried checking whether this was an offset-problem, but it should have shown a '+' sign, which it doesn't in this case.

每当我使用:

plt.ticklabel_format(style='plain')

我收到一条错误消息:

Traceback (most recent call last):
  File "C:\Python\lib\site-packages\matplotlib\axes\_base.py", line 2831, in ticklabel_format
    self.xaxis.major.formatter.set_scientific(sb)
AttributeError: 'FixedFormatter' object has no attribute 'set_scientific'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Python/Projects/Kaggle 1.py", line 13, in <module>
    plt.ticklabel_format(style='plain')
  File "C:\Python\lib\site-packages\matplotlib\pyplot.py", line 2982, in ticklabel_format
    useMathText=useMathText)
  File "C:\Python\lib\site-packages\matplotlib\axes\_base.py", line 2856, in ticklabel_format
    "This method only works with the ScalarFormatter.")
AttributeError: This method only works with the ScalarFormatter.

我已经研究了这个ScalarFormatter,但是我对它为什么不起作用没有任何明智的认识.我试图将其明确包含在代码中,但是它不起作用.

I've looked into this ScalarFormatter, but I couldn't get any wiser as to why it doesn;t work. I've tried to explicitly include it in the code, but it doesn't work.

我使用的代码是:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.read_csv("100 Sales Records.csv")
df_new = df.groupby(['Region']).sum().sort_values("Total Profit", ascending=False)
regions = ('Sub-Saharan Africa', 'Europe', 'Asia', 'Middle East and North Africa', 'Australia and Oceania', 'Central America and the Caribbean', 'North America')
profit = df_new['Total Profit']
y_pos = np.arange(len(profit))

plt.bar(y_pos, profit)
plt.xticks(y_pos, regions)
plt.ticklabel_format(style='plain')
plt.title('Sum of Sales')

plt.show()

当前图表如下:

推荐答案

您可以使用matplotlib.ticker中的FuncFormatter来更新当前图上的刻度.在下面的示例中,使用自定义的scientific_formatter更新滴答声,我定义了scientific_formatter以2个精度数字-%2E用科学计数法更新滴答声.

You can use FuncFormatter of the matplotlib.ticker to update the ticks as you wish on your current plot. In my example below, the ticks are updated using a custom scientific_formatter, that I defined to update the ticks in scientific notation with 2 precision digits - %2E.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter

profit = pd.Series(np.random.randint(1e2, size=5))
ax = profit.plot(kind="bar")

def scientific(x, pos):
    # x:  tick value - ie. what you currently see in yticks
    # pos: a position - ie. the index of the tick (from 0 to 9 in this example)
    return '%.2E' % x

scientific_formatter = FuncFormatter(scientific)
ax.yaxis.set_major_formatter(scientific_formatter)

这篇关于我可以在matplotlib条形图中转换科学计数法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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