【问题标题】:How to disable scientific notation of the hover graph cordinates如何禁用悬停图坐标的科学计数法
【发布时间】:2019-09-05 17:16:58
【问题描述】:

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

这是我的代码,我也会留下坐标。

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]

【问题讨论】:

标签: python matplotlib


【解决方案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)}"

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

【讨论】:

  • 谢谢欧内斯特。这非常有效,所以我接受了你的回答并投了赞成票。有没有办法可以将它缩短到小数点后 2 位?
  • f"x={x:.2f}, y={y:.2f}" 将显示 2 位小数
  • 谢谢欧内斯特。做到了!
猜你喜欢
  • 2011-07-18
  • 1970-01-01
  • 1970-01-01
  • 2021-01-27
  • 1970-01-01
  • 2021-05-01
  • 2013-09-27
相关资源
最近更新 更多