如何禁用悬停图形坐标的科学计数法

时间:2019-04-15 16:56:21

标签: python matplotlib

当我将鼠标悬停在图形上方时,左下角(图形下方,而不是标签下方)中的x和y图形坐标以科学计数法显示。如何禁用此功能以标准形式显示数字?我认为这不是重复的问题(有人标记了它)。 enter image description here

这是我的代码,我也将保留坐标。

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

这是坐标

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]

1 个答案:

答案 0 :(得分:1)

您可以根据自己的喜好修改坐标格式器。例如。在这种最简单的情况下,只需打印出数字即可。

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

这可以很好地工作到10 ^ 16左右。对于更高的数字,您可以这样使用numpy.format_float_positional

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

有关格式化浮点的更多深入讨论,请参见Convert float to string without scientific notation and false precision