簡體   English   中英

我如何在 Matplotlib 上為我的 y 軸限制設置一個精確的浮點數?

[英]How do I set an exact float as the scaling factor for my y-axis limits on Matplotlib?

數字1.67845714e-12是我的物理項目的一個重要結果。 我想繪制圖形,其中 y 軸限制圍繞此結果作為縮放因子。

在以下示例中:

import numpy as np
import matplotlib.pyplot as plt
plt.ion()
import random 

x = np.arange(0,100)
y = np.zeros(len(x))

for i in range(len(x)):
    y[i] = 1.67845714e-12*random.random() - (4.20e-14)*x[i]

plt.ylim(float(-3*1.67845714e-12), float(3*1.67845714e-12)) #The important line 
plt.plot(x,y)

結果的 y 軸從-5*e-125*e-12 ,而不是從-3*1.67845714e-123*1.67845714e-12的 y 軸

我怎樣才能讓 Matplotlib 在圖形的左上角顯示1.67845714e-12的步長, 1.67845714e-12像我預期的那樣在 y 軸上顯示 -3 到 3 的比例? 如果這是不可能的,例如,步驟太長,我可以給我的步驟一個昵稱(例如“結果”)來完成這項工作嗎?

我試過使用其他命令,如ax.set_yticks()ax=plt.gta()ax.set_ylim()但我搜索過的似乎沒有任何效果。

我試圖根據我理解的你想要做的來回答。 正如我所看到的,你想要一個從-3*1.67845714e-123*1.67845714e-12y軸,但每個步驟/刻度的大小1.67845714e-12

注意我創建了一個名為scalingFactor的變量來保存1.67845714e-12 我認為這回答了您的問題之一。 然后您可以使用它而不是寫出整數。

好的,生成刻度,以便您可以使用numpy.arange(inf, sup, step)函數。 它返回給定區間[inf;sup)內均勻間隔的值。 因此,我們將使用1.67845714e-12步長生成從-3*scalingFactor3*scalingFactor1.67845714e-12 您可能會在代碼中注意到sup=4*scalingFactor 這是因為numpy.arange()排除了區間的上限。

要獲得軸上的整數而不是四舍五入,您可以使用plt.gca().yaxis.set_major_formatter(mtick.FormatStrFormatter('%.8e'))強制它具有 8 個小數位。 此函數格式化軸刻度的標簽,在這種情況下它使用此字符串格式%.8e

import numpy as np
import matplotlib.pyplot as plt
plt.ion()
import random 
import matplotlib.ticker as mtick

x = np.arange(0,100)
y = np.zeros(len(x))

scalingFactor = 1.67845714e-12

for i in range(len(x)):
    y[i] = scalingFactor*random.random() - (4.20e-14)*x[i]

inf = -3*scalingFactor
sup = 4*scalingFactor

plt.plot(x, y)
plt.ylim(inf, sup)
plt.yticks(np.arange(inf, sup, scalingFactor))
plt.subplots_adjust(left=0.24) # Squash the plot from the left so the ticks labels can be seen
plt.gca().yaxis.set_major_formatter(mtick.FormatStrFormatter('%.8e')) 
plt.show()

輸出

輸出

編輯:嗯,事實上,我實際上正在為你想要的東西而苦苦掙扎,因為我從來不需要那樣做。 但是,我根據您的需要為您的問題提出了一個非常特別的解決方案,因為您的所有數據似乎都在該范圍內,而您想要的比例因子為1.67845714e-12

有格式化程序類可以格式化刻度值和處理偏移值(左上角的縮放值)。 因此,我們可以創建一個ModScalarFormatter從繼承ScalarFormatter和覆蓋一些函數來設置手動偏移和扁虱,我們希望沒有讓matplotlib計算它:

import numpy as np
import matplotlib.pyplot as plt
plt.ion()
import random 
import math
import matplotlib.ticker as mtick

class ModScalarFormatter(mtick.ScalarFormatter):
    def __init__(self, useOffset=None, useMathText=None, useLocale=None):
        mtick.ScalarFormatter.__init__(self, useOffset, useMathText, useLocale)
        # Create the ticks we want
        self.ticks = [i for i in range(-3, 4)]

    def _set_offset(self, text):
        self.offset = text # Set the offset text we want

    def get_offset(self, txt=''):
        return self.offset # Return the offset value

    def __call__(self, x, pos=None):
        # The __call__ returns the tick on position `pos` from the
        # ticks we specified
        return self.ticks[pos]

x = np.arange(0,100)
y = np.zeros(len(x))

scalingFactor = 1.67845714e-12

for i in range(len(x)):
    y[i] = scalingFactor*random.random() - (4.20e-14)*x[i]

inf = -3*scalingFactor
sup = 3*scalingFactor

plt.plot(x, y)
plt.yticks(np.linspace(inf, sup, 7))

# Create and use a Custom Scalar Formatter Class
sf = ModScalarFormatter(useOffset=1.67845714e-12)
plt.gca().yaxis.set_major_formatter(sf)

plt.ylim(inf, sup)

plt.show()

輸出: 臨時解決方案

注意:我很確定應該有一種更優雅的方法來實現這一點,但這是我幫助您解決特定問題的方式。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM