【问题标题】:<IPython.core.display.HTML object> message in Jupyter/Visual Studio CodeJupyter/Visual Studio Code 中的 <IPython.core.display.HTML object> 消息
【发布时间】: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(我正在使用它)或更改此处的情节:

</> ICON

但是当我按下那里时,唯一弹出的菜单如下:

Menu "Select mimetype to render for current output"

有什么办法可以解决这个问题吗?

谢谢! 费尔南多。

【问题讨论】:

  • 尝试%matplotlib inline 而不是%matplotlib notebook。这是你想要得到的吗?这将使情节出现。然后,如果你想禁止文本输出,你可以添加plot.show()

标签: python visual-studio-code jupyter-notebook html-object


【解决方案1】:

%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()

【讨论】:

  • 效果很好!我之前尝试过单独使用这些命令,但它们一起工作。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
  • 1970-01-01
相关资源
最近更新 更多