[英]Why is it not possiple to write from matplotlib import * and then matplotlib.pyplot.plot?
我知道導入 matplotlib 和 matplotlib.pyplot 有什么區別? 類似。 但是根據 Plasma 在那里評論的內容,它應該能夠從 matplotlib import * 寫入,然后是 matplotlib.pyplot.plot 。 但正如你所看到的,我只收到了 NameError。 我知道你可以從 matplotlib import pyplot 中編寫。 但我只想知道為什么你不能像下面的程序圖片那樣寫:
提前致謝!
簡單地說, pyplot
是matplotlib
一個子模塊,它不會因為您導入matplotlib
而自動導入。
當您運行import matplotlib as mpl
,只會import matplotlib as mpl
一組特定的方法,即您可以在之后使用mpl.use()
或mpl.rc_params
。 如果您對導入后可用的內容感興趣,請嘗試:
print(dir(mpl))
當您from matplotlib import *
運行時,可以使用與以前相同的方法,只是沒有mpl.
字首。 這就是為什么您通常不應該這樣做的原因,因為您可能已經計划編寫自己的名為use()
的函數。
如果您想實際繪圖,則需要顯式導入pyplot
,即添加:
from matplotlib import pyplot
或者:
import matplotlib.pyplot as plt
重要的旁注:導入matplotlib.pyplot
實際上進一步填充了mpl
命名空間; 只有在將matplotlib as mpl
以及matplotlib.pyplot
導入時,您才能訪問mpl.ticker
模塊。 如果你沒有明確地導入pyplot
,你會得到一個AttributeError: "module 'matplotlib' has no attribute 'ticker'"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.