您好,我正在嘗試查看 Python 中的繪圖練習圖。我在帶有 Jupyter 擴展的 Visual Studio Code 中,當我運行此代碼時:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib notebook
#generate 4 random variables from the random, gamma, exponential, and uniform distributions
x1 = np.random.normal(-2.5, 1, 10000)
x2 = np.random.gamma(2, 1.5, 10000)
x3 = np.random.exponential(2, 10000) 7
x4 = np.random.uniform(14,20, 10000)
#plot the histograms
plt.figure(figsize=(9,3))
plt.hist(x1, density=True, bins=20, alpha=0.5)
plt.hist(x2, density=True, bins=20, alpha=0.5)
plt.hist(x3, density=True, bins=20, alpha=0.5)
plt.hist(x4, density=True, bins=20, alpha=0.5);
plt.axis([-7,21,0,0.6])
plt.text(x1.mean()-1.5, 0.5, 'x1\nNormal')
plt.text(x2.mean()-1.5, 0.5, 'x2\nGamma')
plt.text(x3.mean()-1.5, 0.5, 'x3\nExponential')
plt.text(x4.mean()-1.5, 0.5, 'x4\nUniform')
輸出是
"<IPython.core.display.Javascript object>
<IPython.core.display.HTML object>
Text(15.512406857944477, 0.5, 'x4\nUniform')"
我已經看到有相同問題的其他查詢,并且它們已解決更改為 Jupyter(我正在使用它)或更改此處的情節:
</> 圖示
但是當我按下那里時,唯一彈出的選單如下:
選單“選擇要渲染當前輸出的 mimetype”
任何線索我怎么能解決這個問題?
謝謝!費爾南多。
uj5u.com熱心網友回復:
更改%matplotlib notebook為%matplotlib inline將使情節顯示為輸出單元格。要洗掉不需要的文本輸出,請添加plt.show()到單元格的最后一行,以便它知道所需的輸出是繪圖。單元格應如下所示。
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
#generate 4 random variables from the random, gamma, exponential, and uniform distributions
x1 = np.random.normal(-2.5, 1, 10000)
x2 = np.random.gamma(2, 1.5, 10000)
x3 = np.random.exponential(2, 10000) 7
x4 = np.random.uniform(14,20, 10000)
#plot the histograms
plt.figure(figsize=(9,3))
plt.hist(x1, density=True, bins=20, alpha=0.5)
plt.hist(x2, density=True, bins=20, alpha=0.5)
plt.hist(x3, density=True, bins=20, alpha=0.5)
plt.hist(x4, density=True, bins=20, alpha=0.5);
plt.axis([-7,21,0,0.6])
plt.text(x1.mean()-1.5, 0.5, 'x1\nNormal')
plt.text(x2.mean()-1.5, 0.5, 'x2\nGamma')
plt.text(x3.mean()-1.5, 0.5, 'x3\nExponential')
plt.text(x4.mean()-1.5, 0.5, 'x4\nUniform')
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/418771.html
標籤:
