2

When I hover over my graph, the x and y graph coordinates in the lower left corner(below my graph, not the labels) are displaying in scientific notation. How would I disable this to display the numbers in standard form? I don't think this is a duplicate question (as someone marked it). enter image description here

Here is my code and I'll leave the coordinates as well.

import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
from scipy.interpolate import *
import MySQLdb

# connect to MySQL database
def mysql_select_all():
    conn = MySQLdb.connect(host='localhost',
                           user='root',
                           passwd='####',
                           db='world')
    cursor = conn.cursor()
    sql = """
        SELECT
            GNP, Population
        FROM
            country
        WHERE
            Name LIKE 'United States'
                OR Name LIKE 'Canada'
                OR Name LIKE 'United Kingdom'
                OR Name LIKE 'Russian Federation'
                OR Name LIKE 'Germany'
                OR Name LIKE 'Poland'
                OR Name LIKE 'Italy'
                OR Name LIKE 'China'
                OR Name LIKE 'India'
                OR Name LIKE 'Japan'
                OR Name LIKE 'Brazil';
    """

    cursor.execute(sql)
    result = cursor.fetchall()

    list_x = []
    list_y = []

    for row in result:
        list_x.append(row[0])

    for row in result:
        list_y.append(row[1])

    list_x = list(map(float, list_x))                          # before this statment each item in list_x was a string. This converts those string items into floats
    list_y = list(map(float, list_y))
    print(list_x)
    print(list_y)

    fig = plt.figure()

    ax1 = plt.subplot2grid((1,1), (0,0))
    p1 = np.polyfit(list_x, list_y, 1)                          #p1 has my slope @ index 0 and my intercept @ index 1


    ax1.xaxis.labelpad = 50
    ax1.yaxis.labelpad = 50

    plt.xlim(15000, 10510700)    
    plt.plot(list_x, np.polyval(p1,list_x),'r-')                # using p1 to plot line of best fit
    plt.scatter(list_x, list_y, color = 'darkgreen', s = 100)
    plt.xlabel("GNP (US dollars)", fontsize=30)
    plt.ylabel("Population(in billions)", fontsize=30)
    plt.xticks([1000000, 2000000, 3000000, 4000000, 5000000, 6000000, 7000000, 8000000, 9000000],  rotation=45, fontsize=14)
    plt.yticks(fontsize=14)

    plt.show()
    cursor.close()


mysql_select_all()

Here are the coordinates

x = [776739.0, 598862.0, 982268.0, 2133367.0, 1378330.0, 447114.0, 1161755.0, 3787042.0, 151697.0, 276608.0, 8510700.0]

y = [170115000.0, 31147000.0, 1277558000.0, 82164700.0, 59623400.0, 1013662000.0, 57680000.0, 126714000.0, 38653600.0, 146934000.0, 278357000.0]
Nick T
  • 643
  • 2
  • 9
  • 22
  • 1
    Possible duplicate of [prevent scientific notation in matplotlib.pyplot](https://stackoverflow.com/questions/28371674/prevent-scientific-notation-in-matplotlib-pyplot) – m13op22 Apr 15 '19 at 17:44
  • 2
    Not a duplicate. Theyre referring to sci notation in the labels. I'm taking about in the coordinates below the graph. I added a picture to show – Nick T Apr 15 '19 at 19:00

1 Answers1

3

You can modify the coordinate formatter to your liking. E.g. in this simplest case, to just print out the numbers.

ax.format_coord = lambda x,y: f"x={x}, y={y}"

This will work nicely up to 10^16 or so. For higher numbers you may use numpy.format_float_positional, like so

import numpy as np
ax.format_coord = lambda x,y: f"x={np.format_float_positional(x)}, y={np.format_float_positional(y)}"

enter image description here

For more in depth discussion about formatting floats see Convert float to string without scientific notation and false precision

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712