Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have the following python script that is no longer working. It ran when I first did this, but I ran it again after a few months of not touching it, and now I am getting the following error: ImportError: No module named mpl_toolkits.mplot3d
Like I said, I didn't edit anything in this script since it has worked months ago. I checked the import statements, and they seem to be correct. I'm not sure what is going on. I tried finding a solution online, but I was unsuccessful. I realize that this is a pretty hard question to answer, but I thought I would give it a shot. I am totally stumped. Thank you for any help you can give.
This script just makes a window pop up with a multi variable function plotted. What it does isn't really important though. I think I adapted it from a tutorial I found online.
#!/usr/bin/python
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import random
def fun(x, y):
return x**2 + y
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = y = np.arange(-3.0, 3.0, 0.05)
X, Y = np.meshgrid(x, y)
zs = np.array([fun(x,y) for x,y in zip(np.ravel(X), np.ravel(Y))])
Z = zs.reshape(X.shape)
ax.plot_surface(X, Y, Z)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 hours ago.
Improve this question
I try executer in jupiter notebook and i didn't
import matplotlib.pyplot as plt
from math import *
from numpy import *
from pylab import *
t = arange(0.1, 20, 0.1)
y1 = sin(t)/t
y2 = sin(t)*exp(-t)
p1, p2 = plot(t, y1, t, y2)
So I found many questions on plotting vector fields.
Some great sources on making these plots would be:
Chapter 4 Graphics with Matplotlib
matplotlib.axes.Axes.quiverkey
Visualizing a vector field with matplotlib
Vector Fields
I found many questions on creating these plots on stack exchange. The difference is my question is particularly with regards to adding a title to these plots. The old plt.title() did not work to do this. The best solution I had so far was creating a legend with the desired title then forcing it up using the coordinates. My solution however looks horrible. My question is how does one add a title to a quiver plot/vector plot.
My code so far was
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x,y = np.meshgrid(np.linspace(-20,20,15),np.linspace(-20,20,15))
xdot = x
ydot = x+y
# Plot
fig, ax = plt.subplots(figsize=(8,6))
Q = ax.quiver(x, y, xdot, ydot, scale=500, angles='xy') # Quiver key
ax.quiverkey(Q,-10,22,30,"5.1.7",coordinates='data',color='k')
xl = plt.xlabel("x")
yl = plt.ylabel("y")
plt.show()
Summary
So to summarize my question. How does one add a title to the quiver plot above. Could you please provide a solution with the plot.
Sorry if this is a trivial question I am relatively new to both Python and plotting quiver plots. I am using google colab rather than jupyter if that could influence anything.
plt.title() should work but the trick is the position that you add it. A good rule of thumb is to add it last just before plt.show() always.
This code only produces one figure with two plots on it
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
def squareX(x):
return x*x
fig,ax=plt.subplots()
i=np.arange(5)
j=squareX(i)
print(i,j)
figure0=plt.plot(i,j)
#plt.show()
#fig,ax=plt.subplots()# if you omit this then a new plot wont show-you therefore need to show the plot before
x = np.random.rand(10)
y = np.random.rand(10)
figure1 = plt.plot(x,y)
Question 1. The first call to
fig,ax=plt.subplots()
returns two variables that are never used. Rather, the two plots are made using
figure0 = plt.plot(x,y)
and
figure1 = plt.plot(x,y)
Why does matplotlib decide to output them both after the second call? What makes the first call to figure0 = plt.plot(x,y) invisible until the end?
Question 2) If first commented line is UNCOMMENTED, then matplotlib displays two figures. I can understand that plt.show() after the first call to figure0 = plt.plot(x,y) causes matplotlib to draw a figure but there is no call to plt.show() after the call to figure1 = plt.plot(x,y) but figure1 still gets drawn.
Question 3) If the second commented line is UNCOMMENTED then matplotlib displays two figures. But still, the second uncommented line seems almost irrelevant as its variables are never used.
I realize this is a VERY BASIC question and refers to the 'underworkings'of matplotlib. Please forgive me if it has been CLEARLY addressed somewhere but there seems to be a lot of disconnected documentation about matplotlib. If you have a link for a simpleton like me I would appreciate it.
I am using Python and the matplotlib library.
I run through a very long code creating multiple figures along the way.
I do something like this, many many times:
plt.figure()
plt.plot(x, y)
plt.grid('on')
plt.close()
Then, at some point, I get the error:
More than 20 figures have been opened. Figures created through the pyplot interface (matplotlib.pyplot.figure) are retained until explicitly closed and may consume too much memory.
The error is clear to me, but: I do call "plt.close()".
Now that I am writing this I am realizing that maybe plt.close() has to take a specific qualifier for what figure to close? Like plt.close(1) etc. I guess I can use plt.close('all'), but what if I just want to close the most recent figure?
The code from the question should work fine. Since you close the figure in every loop step, there will only ever be one single figure open.
Minimal example, which does not produce any error:
import matplotlib.pyplot as plt
for i in range(30):
plt.figure()
plt.plot(range(i+3), range(i+3))
plt.grid('on')
plt.close()
plt.show() # doesn't show anything since no figure is open
So the reason for the error must be somewhere else in the code.
You should operate on matplotlib objects directly. It's so much less ambiguous:
fig, ax = plt.subplots()
ax.plot(x, y)
...
plt.close(fig)
I'm writing some python functions that deals with manipulating sets of 2D/3D coordinates, mostly 2D.
The issue is debugging such code is made difficult just by looking at the points. So I'm looking for some software that could display the points and showing which points have been added/removed after each step. Basically, I'm looking for a turn my algorithm into an animation.
I've seen a few applets online that do things similar what I was looking for, but I lack the graphics/GUI programming skills to write something similar at this point, and I'm not sure it's wise to email the authors of things whose last modified timestamps reads several years ago. I should note I'm not against learning some graphics/GUI programming in the process, but I'd rather not spend more than 1-3 days if it can't be helped, although such links are still appreciated. In this sense, linking to a how-to site for writing such a step-by-step program might be acceptable.
With the matplotlib library it is very easy to get working animations up. Below is a minimal example to work with. The function generate_data can be adapted to your needs:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def generate_data():
X = np.arange(25)
Y = X**2 * np.random.rand(25)
return X,Y
def update(data):
mat[0].set_xdata(data[0])
mat[0].set_ydata(data[1])
return mat
def data_gen():
while True:
yield generate_data()
fig, ax = plt.subplots()
X,Y = generate_data()
mat = ax.plot(X,Y,'o')
ani = animation.FuncAnimation(fig, update, data_gen, interval=500,
save_count=10)
ani.save('animation.mp4')
plt.show()
This example was adapted from a previous answer and modified to show a line plot instead of a colormap.