防止matplotlib.pyplot中的科学计数法。尝试了所有在网上找到的选项,但没有一个对我有用。 ticklabel_format是否损坏?

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

我是python和matplotlib的新手,并且现在很难在pyplot中抑制科学记数法了几个小时。我尝试了建议在prevent scientific notation in matplotlib.pyplot上使用的解决方案,但没有一个有效。我还尝试了https://code-examples.net/en/q/2677a94上列出的解决方案,但这些解决方案也无法正常工作。我很茫然。请帮助。

我正在使用Jupyter笔记本。

导入的python库

import pandas as pd
import numpy as np
import requests
import zipfile
import io
import xlrd

%matplotlib inline 

import matplotlib as mpl
import matplotlib.pyplot as plt


mpl.style.use('ggplot') # optional: for ggplot-like style

# check for latest version of Matplotlib
print ('Matplotlib version: ', mpl.__version__) # >= 2.0.0
# Matplot version: 3.1.0

当我使用下面的代码创建默认线图时,在y轴上显示le7。

# Draw Line Plot for Imigration from all countries

# years columns after the ones with no data got removed
years = list(map(str, range(1998, 2014)))

# total is a dataframe
total = df_uk.loc['Column_Total', years] # passing in years 1998 - 2013 

total.plot(kind='line')

plt.title('Immigration in UK')
plt.ylabel('Number of Immigrants')
plt.xlabel('Years')

plt.show() 

如果我尝试用下面的方法来抑制科学计数法则出现NameError:未定义名称'ax'

ax.ticklabel_format(useOffset=False, style='plain')
total.plot(kind='line')


plt.title('Immigration in UK')
plt.ylabel('Number of Immigrants')
plt.xlabel('Years')

plt.show() 

完整错误:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-17-576bd02a7dac> in <module>
      1 # Draw Line Plot for Imigration from all countries
      2 
----> 3 ax.ticklabel_format(useOffset=False, style='plain')
      4 total.plot(kind='line')
      5 

NameError: name 'ax' is not defined

请帮助。

python matplotlib jupyter-notebook scientific-notation
1个回答
0
投票

我能够解决这个问题:)

本文帮助我了解了如何将斧头与数据框一起使用-http://queirozf.com/entries/pandas-dataframe-plot-examples-with-matplotlib-pyplot。我很困惑,因为ticklabel_format的示例使用的是x和y的列表,而不是dataframe。


# Draw Line Plot for Imigration from all countries 

fig, ax = plt.subplots()

ax.ticklabel_format(useOffset=False, style='plain')

total.plot(kind='line', ax=ax)

plt.title('Immigration in UK')
plt.ylabel('Number of Immigrants')
plt.xlabel('Years')

plt.show() 

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