I've been trying to make a scatter plot with the following code
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(x, y)
plt.show()
If I type these commands line by line into ipython console, there is no graph displayed after the plt.show() command. However, if I copy and paste the whole code block into the console, the graph is displayed.
Has anyone had this issue before? What could be the reason for this?
Related
I am trying to render an animation of rotating a 3d graph, but the animation is not working, even if I try to use the code given here: https://matplotlib.org/3.5.0/gallery/mplot3d/rotate_axes3d_sgskip.html
The output I get is one static image and <Figure size 432x288 with 0 Axes> repeated multipe times over and over again.
I am using anaconda, jupyter notebooks and also tried using google colab... what is going wrong and how can I fix this? Thank you very much!
I ran into a similar issue before, replacing "%matplotlib inline" with "%matplotlib notebook" in the given code solved it for me.
Since you are using Jupyter Notebook, you need to install ipympl and execute the following command on a cell at the top of your notebook: %matplotlib widget. This will enable an interactive frame which is going to wrap the matplotlib figure in the output cell.
Then, you need to use matplotlib's FuncAnimation:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
# load some test data for demonstration and plot a wireframe
X, Y, Z = axes3d.get_test_data(0.1)
ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5)
def animate(angle):
ax.view_init(30, angle)
ani = FuncAnimation(fig, animate, frames=range(360))
Add %matplotlib notebook in the cell of Jupyter notebook for an interactive backend.
Execute the below cell, it should work
%matplotlib notebook
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
# load some test data for demonstration and plot a wireframe
X, Y, Z = axes3d.get_test_data(0.1)
ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5)
# rotate the axes and update
for angle in range(0, 360):
ax.view_init(30, angle)
plt.draw()
plt.pause(.001)
I managed to generate an interactive matplotlib figure that changes the position of an ellipse using some ipywidget slider.
However the plot is displayed in a new window, I could not find a working solution with the plot in the notebook.
I could generate other interactive figures within a jupyter notebook, like line plots, but with the ellipse, no way..
%matplotlib
from matplotlib import patches
import matplotlib.pyplot as plt
from ipywidgets import interact
fig = plt.figure()
ax = fig.add_subplot(111)
ellipse = patches.Ellipse((0,0), width=50, height=25, angle=15)
ax.add_patch(ellipse)
ax.set_xlim(-100, 100)
ax.set_ylim(-100, 100)
#interact
def update(xcenter=(-50,50), ycenter=(-50,50)):
ellipse.set_center((xcenter, ycenter))
#fig.canvas.draw()
plt.plot()
Using % matplotlib notebook fixed the issue in most cases.
I run code from this answer How to put the legend out of the plot in PyCharm in console (Anaconda environment) and it didn't produced right output
I recreated a conda env, but it still does not work.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,2*np.pi)
colors=["#7aa0c4","#ca82e1" ,"#8bcd50","#e18882"]
fig, axes = plt.subplots(ncols=2)
for i in range(4):
axes[i//2].plot(x,np.sin(x+i), color=colors[i],label="y=sin(x+{})".format(i))
fig.legend(loc=7)
fig.tight_layout()
fig.subplots_adjust(right=0.75)
plt.show()
Expected: what's presented in answer.
Actual in PyCharm (SciView)
Actual in prompt
I have the code below, which works great with a single plot, but I'm trying to create a new plot with 1x2 subplots. The second plot will be identical to the first, just in another subplot.
# This code works fine as a single plot
%matplotlib inline
import time
import pylab as pl
from IPython import display
for i in range(10):
pl.clf()
pl.plot(pl.randn(100))
display.display(pl.gcf())
display.clear_output(wait=True)
time.sleep(1.0)
I'm not familar with pylab, but the above plot runs so smoothly compared to the pyplot code I found on the nex, that I'm trying to figure out how to implement this code with subplots.
#can't implement it to a plot with subplots
%matplotlib inline
import time
import pylab as pl
from IPython import display
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True, sharex = True, figsize = (10,5))
for i in range(10):
pl.clf()
ax1.plot(pl.randn(100),)
ax2.plot(pl.randn(50))
display.display(pl.show())
display.clear_output(wait=True)
time.sleep(1.0)
However, no graph is being outputted with my attempt.
I'm played around with this code, but I can't seem to make it work cleanly.
thank you.
To visualize the plot with subplots, you should know the differences between Figure and Axes in matplotlib. Basically, axes belong to the figure, and you want to plot your data in the axes, but display the figure. Both Figure and Axes instances can be obtained with a single call to pl.subplots(nrow, ncol). See if the code below does what you want:
%matplotlib inline
import time
import pylab as pl
from IPython import display
for i in range(10):
pl.clf()
f, ax = pl.subplots(1, 2)
ax[0].plot(pl.randn(100))
ax[1].plot(pl.randn(100))
display.display(f)
display.clear_output(wait=True)
time.sleep(1.0)
I'm a novice with Python and working through "Machine Learning In Action" by P. Harrington. I'm having issues seeing my plot when using the pyplot.show() function. Below is the code that I'm entering in the IPython console on the bottom right of Spyder.
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(datingDataMat[:,1], datingDataMat[:,2])
plt.show()
After I enter this last line, nothing happens. Does anyone have an idea why it doesn't appear?