亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

xx 格式科學風格 y-ticks matplotlib

xx 格式科學風格 y-ticks matplotlib

慕娘9325324 2021-12-17 16:48:07
我正在使用 matplotlib 創建一些圖。對于我使用的地塊style='sci'與scilimits=(0,0)這是代碼:for key in urbs_values.keys():    # y-Axis (values)    u[key] = np.array(urbs_values[key])    o[key] = np.array(oemof_values[key])    # draw plots    plt.plot(i, u[key], label='urbs_'+str(key), linestyle='None', marker='x')    plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0))    plt.plot(i, o[key], label='oemof_'+str(key), linestyle='None', marker='.')    plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0))這通常是一致的,但有時我會在格式中獲取 y 軸上的值x.x,有時我會得到x.xx,但我并不覺得它很優雅。有沒有辦法強制 matplotlib 以一種格式始終為我提供 y 值,例如x.x通過動態縮放科學符號?這是我不喜歡的示例圖:
查看完整描述

1 回答

?
慕森王

TA貢獻1777條經驗 獲得超3個贊

您可以根據數據中的最大值動態設置限制:


def grabMaxExponent(values, precision=1):

    """Given a list of numericals, returns the exponent of the max

    value in scientific notation, adjusted to have no sig figs in

    decimal places.


    e.g. 190 > 1.9E+02 > 19E+01

    returns exponent (1)"""


    # Grab max value and convert to scientific notation

    value = format(max(values), f"5.{precision}E")


    # Pull exponent

    a,m = value.split('E+')

    a,b = a.split('.')

    a,b,m = map(int, (a,b,m))


    # If significant figures non-zero, increase exponent to bring them before decimal point

    if b > 0:

        m-=precision


    return m


m = grabMaxExponent(y)


# Set scilimits to m

plt.ticklabel_format(axis='y', style='sci', scilimits=(m,m))

https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.ticklabel_format.html


查看完整回答
反對 回復 2021-12-17
  • 1 回答
  • 0 關注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號