Hello, I am trying to see a graph of an exercise in Python of plots. I am in Visual Studio Code with the Jupyter extension and when I run this 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')
The output is
"<IPython.core.display.Javascript object>
<IPython.core.display.HTML object>
Text(15.512406857944477, 0.5, 'x4\nUniform')"
I have seen that there are other queries of the same problem and they are solved changing to Jupyter (I am using it) or changing the plot here:
But when I press there the only menu that pops up is the following:
Menu "Select mimetype to render for current output"
Any clue how could I solve this?
Thank you! Fernando.
Changing
%matplotlib notebookto%matplotlib inlinewill get the plot to show up as an ouptut cell. To remove the unwanted text output, addplt.show()to the last line of the cell so it knows the desired output is the plot. The cell should then look like this.