How to solve Axes3D error when plotting a function? - python

I have a code to graph mi function f(x,y)=(x^4 + y^4). I already imported all the necessary libraries, but when i run it, the "MatplotlibDeprecationWarning: Axes3D(fig) adding itself to the figure is deprecated since 3.4. Pass the keyword argument auto_add_to_figure=False and use fig.add_axes(ax) to suppress this warning. The default value of auto_add_to_figure will change to False in mpl3.5 and True values will no longer work in 3.6. This is consistent with other Axes classes." error shows.
This is my code:
`
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
figura = plt.figure()
ejes = Axes3D(figura)
plt.show()
def f(x,y):
return ((x**4)+(y**4))
x = np.linspace(-2,2,40)
y = np.linspace(-2,2,40)
x,y = np.meshgrid(x,y)
z= f(x,y)
ejes.plot_wireframe(x,y,z)
`

On my environment it actually works. Anyway you can obtain the same plot using the "3d" projection of pyplot:
import numpy as np
import matplotlib.pyplot as plt
figura = plt.figure(figsize=(7,7))
ejes = plt.subplot(111, projection="3d")
def f(x,y):
return ((x**4)+(y**4))
x = np.linspace(-2,2,40)
y = np.linspace(-2,2,40)
x,y = np.meshgrid(x,y)
z= f(x,y)
ejes.plot_wireframe(x,y,z)
plt.show()

Related

how can i get this graph by python?

i have written this code
import matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()
r=np.arange(1,2)
theta=np.linspace(0,np.pi/2)
x=r*np.cos(theta)
y=r*np.sin(theta)
plt.plot(x,y)
plt.show()
and get this graph.
but i want to get the graph below.
i'm confused about how to set a proper range of r.
i want to set r's range 1<=r<=2, but don't know how to do that.
how can i modify my code?
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
theta = np.linspace(0,np.pi/2)
for i in range(1,3):
x = []
y = []
for t in theta:
if t == 0:
y.append(0)
x.append(3-1)
elif t == np.pi/2:
y.append(3-1)
x.append(0)
else:
x.append(i*np.cos(t))
y.append(i*np.sin(t))
plt.plot(x,y)
plt.show()

Plotly plot_trisurf isn't working with arange arrays

I've basically just copied the example code found on the Matplotlib website, but I replaced their radii and angles with simple arange arrays.
I've tried different array functions and I can't seem to figure out anything.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from Equation import Expression
x = np.arange(0,100,0.01)
y = np.arange(0,100,0.01)
x2 = np.append(0,x.flatten())
y2 = np.append(0,y.flatten())
z = x2 + y2
print(z)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True)
plt.show()
I'm just trying to make a graph of z = x + y but I'm getting a confusing error.
"RuntimeError: Error in qhull Delaunay triangulation calculation: singular input data (exitcode=2); use python verbose option (-v) to see original qhull error."
Edit: I've also tried it without calling flatten() but I get the same result though.
The error you are getting is because your z is not a surface but a line. You need to use at least 3 points that would define a plane. One option could be to use np.meshgrid to create your surface for plotting and then flatten everything to insert into the function. Try going back to some example code here. Note you may also want to change your resolution depending on the detail of your surface.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,100,1)
y = np.arange(0,100,1)
x2 = np.append(0,x.flatten())
y2 = np.append(0,y.flatten())
x2,y2 = np.meshgrid(x2,y2) #This is what you were missing
z = x2 + y2
fig = plt.figure(figsize=(12,12))
ax = fig.gca(projection='3d')
ax.plot_trisurf(x2.flatten(), y2.flatten(), z.flatten(), linewidth=0.2, antialiased=True) #flatten all the arrays here
plt.show()

Modify line / plot in IPython notebook "interact"

Is there a way to modify the plot inside IPython "interact" function rather than replotting it?
In case when the plot contains a lot of heavy graphics (or some parts require heavy calculations) it would be much faster than doing the plot from the scratch.
I'm trying the following code, but it does not work: after changing the slider the whole plot becomes blank.
%matplotlib inline
import matplotlib.pyplot as plt
from IPython.html.widgets import interact
import numpy as np
x = np.arange(0,11.)
y1 = x / 10.
y2 = np.random.rand(len(x))
plt.plot(x,y1)
plt.plot(x,y2)
plt.ylim([0,1])
ax = plt.gca()
def replot_it(a):
ax.lines.pop(1)
y = (x/10.)**a
ax.plot(x,y)
interact(replot_it, a=(0.,5.))
I would appreciate any suggestions.
If you have IPython > 3.0 and mpl > 1.4: run this is one cell:
%matplotlib notebook
import matplotlib.pyplot as plt
from IPython.html.widgets import interact
import numpy as np
x = np.arange(0,11.)
y1 = x / 10.
y2 = np.random.rand(len(x))
fig, ax = plt.subplots()
ln1, = ax.plot(x,y1)
ln2, = ax.plot(x,y2)
ax.set_ylim([0,1])
def replot_it(a):
y = (x/10.)**a
ln2.set_ydata(y)
ax.figure.canvas.draw()
and
interact(replot_it, a=(0.,5.))
in the cell below it.

Strange behavior of matplotlib's griddata

I have a .txt file with values
x1 y1 z1
x2 y2 z2
etc.
With my previous little experience I was trying to draw a contourf, with this code
import numpy as np
import matplotlib
from matplotlib import rc
import matplotlib.mlab as ml
from pylab import *
rc('font', family='serif')
rc('font', serif='Times New Roman')
rc('font', size='9')
rc('text', usetex=True)
from matplotlib.mlab import griddata
import matplotlib.pyplot as plt
import numpy.ma as ma
from numpy.random import uniform
from matplotlib.colors import LogNorm
matplotlib.use('pgf')
fig = plt.figure()
data = np.genfromtxt('Velocidad.txt')
matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'
rc('text', usetex=True)
rc('font', family='serif')
x = data[:,0]
y = data[:,1]
z = data[:,2]
xi = np.linspace(0,3000.0, 400)
yi = np.linspace(0,4.0, 200)
zi = griddata(x,y,z,xi,yi,interp='nn')
CS = plt.contourf(xi,yi,zi,200,cmap=plt.cm.jet,rasterized=True)
plt.colorbar()
plt.xlim(0,3000)
plt.ylim(0,4.0)
plt.ylabel(r'$t$')
plt.xlabel(r'$x$')
plt.title(r' Contour de $v(x,t)$')
plt.savefig("CampoVel.png", dpi=100)
plt.show()
the problem is the output:
When I see this picture and I look at the data (which is here, in this link) and I don't understand those discontinuities in x=750 and x=1875. And those strange vertical lines all over the plot. Looking at the data I would expect something smooth, at least in those positions, but the output obviously isn't. Is this a problem of griddata()? How can I solve it?
I have been told that as my data is regularly spaced on X and Y, I shouldn't use griddata(), but I have looked examples and I can't get the code to work.
If you simply reshape your data after loading it and skip the griddata thing, doing this:
data = data.reshape(81, 201, 3)
x = data[...,0]
y = data[...,1]
z = data[...,2]
CS = plt.contourf(x,y,z,200,cmap=plt.cm.jet,rasterized=True)
plt.colorbar()
plt.show()
You get this:

Plotting A Hyperboloid

Teacher in class gave this formula
w = x**2 + y**2 - z**2
and showed its 3d graphic in class seen below. How do I plot this using Matplotlib (minus the intersecting plane)? I guess first a specific value for w needs to be selected, for example 10, otherwise 3d plotting would not be possible. Then should I convert to polar coordinates because of the z**2 in the formula? I tried this and failed. Any help would be appreciated. Also, does this shape have a name?
Got it. Found some good stuff here, and following the formulas presented, I have the Python code below.
http://msenux.redwoods.edu/Math4Textbook/Plotting/ParametricSurfaces.pdf
from __future__ import division
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=plt.figaspect(1)) # Square figure
ax = fig.add_subplot(111, projection='3d')
r=1;
u=np.linspace(-2,2,200);
v=np.linspace(0,2*np.pi,60);
[u,v]=np.meshgrid(u,v);
a = 1
b = 1
c = 1
x = a*np.cosh(u)*np.cos(v)
y = b*np.cosh(u)*np.sin(v)
z = c*np.sinh(u)
ax.plot_surface(x, y, z, rstride=4, cstride=4, color='b')
plt.show()

Categories