【发布时间】:2022-01-22 00:57:02
【问题描述】:
您好,我正在尝试查看 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(我正在使用它)或更改此处的情节:
但是当我按下那里时,唯一弹出的菜单如下:
Menu "Select mimetype to render for current output"
有什么办法可以解决这个问题吗?
谢谢! 费尔南多。
【问题讨论】:
-
尝试
%matplotlib inline而不是%matplotlib notebook。这是你想要得到的吗?这将使情节出现。然后,如果你想禁止文本输出,你可以添加plot.show()。
标签: python visual-studio-code jupyter-notebook html-object