-2

I am new to python and matplotlib and I am having hard times suppressing scientific notation in pyplot for a few hours now. I tried solutions recomended on prevent scientific notation in matplotlib.pyplot but none worked. I also tried solutions listed on https://code-examples.net/en/q/2677a94 and those did not work too. I am at loss. Please help.

I am using jupyter notebooks.

imported python libraries

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

When I create default line plot with code below I get le7 on y axis.

# 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() 

If I try to suppress scientific notation with below i get NameError: name 'ax' is not defined

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() 

Full error:

---------------------------------------------------------------------------
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

Please help.

Pygirl
  • 12,969
  • 5
  • 30
  • 43
The smell of roses
  • 117
  • 1
  • 2
  • 10
  • https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/simple_plot.html#sphx-glr-gallery-lines-bars-and-markers-simple-plot-py See how to define fig and ax. "'`fig, ax = plt.subplots()`' –  Dec 07 '19 at 10:36
  • @ankii Your comment pushed me in the right direction. :) I managed to fix it. – The smell of roses Dec 07 '19 at 16:23
  • Post an answer then! :) For future, please introduce variables that can be used anywhere.. like random lists, or arrays full of 1 etc. –  Dec 07 '19 at 16:25
  • @ankii my problem was solely due to a dataframe. If I created variables manually to illustrate, my error would not be there. The answer is now posted. Thanks for your help. – The smell of roses Dec 07 '19 at 16:33

1 Answers1

0

I was able to solve this :)

This article helped me understand how to use ax with dataframe - http://queirozf.com/entries/pandas-dataframe-plot-examples-with-matplotlib-pyplot. I was confused because examples for ticklabel_format were using lists for x and y and not 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() 

The smell of roses
  • 117
  • 1
  • 2
  • 10