2016-02-23 49 views
1

我想繪製一個公式在特定值(mn)更改的函數,但我無法弄清楚如何比較x軸的值與此數字來確定使用哪個公式。Python ValueError - 從matplotlib軸中提取一個浮點數

import matplotlib.pyplot as plt 
import numpy as np 

mn = 10312054216.046213 
mz = 91.1876 
a1z = 98.330 
a2z = 29.571 
a3z = 8.396 
b1, b2, b3 = -13/(4*pi), 19/(12*pi), 7/(2*pi) 
b1p, b2p, b3p = -157/(36*pi), 11/(12*pi), 17/(6*pi) 
c1, c2, c3 = 3/5, 1, 1 

ms = np.linspace(100, 10e18, num = 100, endpoint = True) 

def a1(ms, mn): 
    if (ms < mn): 
     return (c1*(a1z + b1*np.log(ms/mz)))**(-1) 
    if (ms >= mn): 
     a1n = (c1*(a1z + b1*log(mn/mz)))**(-1) 
     return (c1*(a1n + b1p*log(ms/mn)))**(-1) 

plt.xscale('log') 
plt.plot(ms, 1/a1(ms, mn)) 

plt.show() 

只需使用MS>明尼蘇達給

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

我如何可以提取被用於MS的當前值時,A1被調用?

回答

0

不知道你是什麼努力來計算,而是因爲當你調用該函數作爲

plt.plot(ms, 1/a1(ms, mn)) 

然後ms是numpy的陣列和mn是一個數字的比較if (ms < mn)失敗,你不能比較那兩個。相反,如果你想從ms情節a1價值爲每個單獨的元素,那麼你可以試試這個:

plt.plot(ms, [1/a1(x, mn) for x in ms]) 
+0

謝謝你的解釋,這種方法確實我需要的東西。 – user2150358

相關問題