Matplotlib memory plotting loop - python

Does anyone know why memory use keeps increasing? An idealised case is below, I can't see why.
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
Lon = np.linspace(-180,180,1440)
Lat = np.linspace(-90,90,721)
Lon,Lat = np.meshgrid(Lon,Lat)
m = Basemap()
X, Y = m(Lon, Lat)
matrix = np.random.rand(721,1440)
for i in range(0,100):
cs = m.contourf(X,Y,matrix)
plt.clf()
plt.close()
print i

May of been a memory leak - cannot replicate problem with the latest Matplotlib library.

Related

I am not able to display graph in matplotlib

I'm trying to print a logistic differential equation and I'm pretty sure the equation is written correctly but my graph doesn't display anything.
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
def eq(con,x):
return con*x*(1-x)
xList = np.linspace(0,4, num=1000)
con = 2.6
x= .4
for num in range(len(xList)-1):
plt.plot(xList[num], eq(con,x))
x=eq(con,x)
plt.xlabel('Time')
plt.ylabel('Population')
plt.title("Logistic Differential Equation")
plt.show()
You get nothing in your plot because you are plotting points.
In plt you need to have x array and y array (that have the same length) in order to make a plot.
If you want to do exactly what you are doing I suggest to do like this:
import matplotlyb.pyplot as plt # just plt is sufficent
import numpy as np
def eq(con,x):
return con*x*(1-x)
xList = np.linspace(0,4, num=1000)
con = 2.6
x= .4
y = np.zeros(len(xList)) # initialize an array with the same lenght as xList
for num in range(len(xList)-1):
y[num] = eq(con,x)
x=eq(con,x)
plt.figure() # A good habit is always to use figures in plt
plt.plot(xList, y) # 2 arrays of the same lenght
plt.xlabel('Time')
plt.ylabel('Population')
plt.title("Logistic Differential Equation")
plt.show() # now you should get somthing here
I hope that this helps you ^^

How to plot coordinates (1,2) against time (0.5) in python

I am trying to plot vehicle position (coordinates - x,y) against time(1s,2s,3s...). I tried with matplotlib but could not succeed. I am new in python. Could anyone help me please.
My code:
import matplotlib.pyplot as plt
import numpy as np
coordinate = [[524.447876,1399.091919], [525.1377563,1399.95105], [525.7932739,1400.767578], [526.4627686,1401.601563],
[527.2360229,1402.564575], [527.8989258,1403.390381], [528.5689697,1404.224854]]
timestamp =[0,0.05,0.1,0.15,0.2,0.25,0.3]
plt.plot(coordinate,timestamp)
Plot comes like: But this is wrong one. I did wrong.
Plot supposed to become, in particular, timestamp (1s) the vehicle position is (x,y). So there should be one line just like vehicle trajectory.
Thanks.
I believe this is the output you're looking for:
import matplotlib.pyplot as plt
import numpy as np
coordinate = [[524.447876,1399.091919],
[525.1377563,1399.95105],
[525.7932739,1400.767578],
[526.4627686,1401.601563],
[527.2360229,1402.564575],
[527.8989258,1403.390381],
[528.5689697,1404.224854]]
v1 = [y[1] for y in coordinate]
v2 = [y[0] for y in coordinate]
x = [0,0.05,0.1,0.15,0.2,0.25,0.3]
plt.plot(x,v1)
plt.plot(x,v2,'--')
plt.ylim(0,1500)
plt.show()
Does something simple like this meet your needs:
import matplotlib.pyplot as plt
coordinates = [
(524.447876,1399.091919),
(525.1377563,1399.95105),
(525.7932739,1400.767578),
(526.4627686,1401.601563),
(527.2360229,1402.564575),
(527.8989258,1403.390381),
(528.5689697,1404.224854),
]
timestamp = [0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3]
x, y = zip(*coordinates)
ax = plt.axes(projection="3d")
ax.plot(x, y, timestamp);
plt.show()
Matplotlib will let you rotate the image with the mouse to view it from various angles.
Hi I think the problem over here is that you are using a two-dimensional list, so matplotlib plots the coordinates and not the timestamp.
Code:
import matplotlib.pyplot as plt
import numpy as np
coordinate = np.array([[524.447876,1399.091919], [525.1377563,1399.95105], [525.7932739,1400.767578], [526.4627686,1401.601563], [527.2360229,1402.564575], [527.8989258,1403.390381], [528.5689697,1404.224854]])
timestamp =np.array([0,0.05,0.1,0.15,0.2,0.25,0.3])
plt.plot(coordinate)
Output:
You have to convert it into a single dimension list like this:
coordinate_new = np.array([524.447876,525.1377563,1399.95105, 525.7932739,1400.767578, 526.4627686,1401.601563])
timestamp =np.array([0,0.05,0.1,0.15,0.2,0.25,0.3])
plt.plot(coordinate_new, timestamp)
Then the output will be:
Hope I could help!!
If you want to plot it in 3-d, here is what you can do:
import matplotlib.pyplot as plt
#importing matplotlib
fig = plt.figure() #adding figure
ax_3d = plt.axes(projection="3d") #addign 3-d axes
coordinate_x = [524.447876, 525.137756, 525.7932739, 526.4627686, 527.2360229, 527.8989258, 528.5689697]
coordinate_y = [1399.091919, 1399.95105,1400.767578,1401.601563,1402.564575,1403.390381,1404.224854]
timestamp =[0,0.05,0.1,0.15,0.2,0.25,0.3]
# defining the variables
ax.plot(coordinate_x, coordinate_y, timestamp)
#plotting them
Output:
All the Best!

Matplotlib does not show some graphs

Trying to plot a simple graph in Jupyter Notebook with the package matplotlib, I came accross a strange problem that I had never had before.
I've seen that it has hapenned before to other people, and the answers talk about backends and other complicated stuff that I can't understand, me having only a rather basic knowledge of Python.
Here comes the code:
import numpy as np
import matplotlib.pyplot as plt
time_samples = np.arange(17000)
force_samples = np.arange(17000)
plt.plot(time_samples,force_samples)
plt.show()
time_samples2 = np.random.rand(1,1000)
force_samples2 = np.random.rand(1,1000)
plt.plot(time_samples2,force_samples2)
plt.show()
And this is what I get:
I have no clue why this is happenning.
I think the array dimension is the issue. x and y should be a 1D array.
import numpy as np
import matplotlib.pyplot as plt
time_samples = np.arange(17000)
force_samples = np.arange(17000)
plt.plot(time_samples,force_samples)
plt.show()
time_samples2 = np.random.rand(1000)
force_samples2 = np.random.rand(1000)
plt.plot(time_samples2,force_samples2)
plt.show()

How to use numpy to build a 3D-model?

Original(2018.11.01)
I have 3 numpy:x、y、z,created by my laser scanner(40 degree / 1 step).
I want to used them to build a 3D model.
I think it must should be use matplotlib.tri
But I have no idea to decide triangulated data
Here is my data :https://www.dropbox.com/s/d9p62kv9jcq9bwh/xyz.zip?dl=0
And Original model:https://i.imgur.com/XSyONff.jpg
Code:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.tri as mtri
x_all=np.load("x.npy")
y_all=np.load("y.npy")
z_all=np.load("z.npy")
tri = #I have no idea...
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_trisurf(x_all,y_all,z_all,triangles=tri.triangles)
Thank so much.
Update(2018.11.02)
I try this way to decide triangulated data
Delaunay Triangulation of points from 2D surface in 3D with python?
code:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.tri as mtri
from stl import mesh
x_all=np.load("x.npy")
y_all=np.load("y.npy")
z_all=np.load("z.npy")
model=np.vstack((x_all,y_all,z_all))
model=np.transpose(model)
model -= model.mean(axis=0)
rad = np.linalg.norm(model, axis=1)
zen = np.arccos(model[:,-1] / rad)
azi = np.arctan2(model[:,1], model[:,0])
tris = mtri.Triangulation(zen, azi)
plt.show()
And my model looks like:
https://i.stack.imgur.com/KVPHP.png
https://i.stack.imgur.com/LLQsQ.png
https://i.stack.imgur.com/HdzFm.png
Even though it has better surface on it,but there is a big hole over my model.Any idea to fixs it?
Assuming you want to reduce the complexity, i.e find triangles in your files to reduce the complexity. You may look into fitting a convex hull to your points, see here fore more info
Based on the file you provided this produces a surf plot of the object.
from numpy import load, stack
from matplotlib.pyplot import subplots
from mpl_toolkits.mplot3d import Axes3D
from scipy import spatial
x = load("x.npy")
y = load("y.npy")
z = load("z.npy")
points = stack((x,y,z), axis = -1)
v = spatial.ConvexHull(points)
fig, ax = subplots(subplot_kw = dict(projection = '3d'))
ax.plot_trisurf(*v.points.T, triangles = v.simplices.T)
fig.show()

Python3.6 - Plotting lat/long co-ordinates on Matplotlib [duplicate]

This question already has an answer here:
Difference in plotting with different matplotlib versions
(1 answer)
Closed 4 years ago.
This is my first time using Matplotlib. I have a series of latitude and longitude co-ordinates in two lists, and I want to represent these in a meaningful manner. I would not like to use Basemap for several reasons.
lat = ['35.905333', '35.896389', '35.901281', '35.860491', '35.807607', '35.832267', '35.882414', '35.983794', '35.974463', '35.930951']
long = ['14.471970', '14.477780', '14.518173', '14.572245', '14.535320', '14.455894', '14.373217', '14.336096', '14.351006', '14.401137']
I am trying to represent these in a meaningful manner using Matplotlib.
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
plt.scatter(lat, long)
plt.show()
However my figure is as follows:
I am unable to set the axis in order to obtain a meaningful representation of these coordinates. How can this be done? What am I doing wrong?
I am looking for something like this:
import numpy as np
import matplotlib.pyplot as plt
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
plt.scatter(x, y)
plt.show()
I get the expected outcome.
I have also tried to plot on a cartesian coordinate system.
EDIT:
As per the comment below:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
plt.scatter(lat, long)
plt.axis('square')
plt.show()
As mentioned in the comment, change the type to float:
import numpy as np
import matplotlib.pyplot as plt
lat = np.array(['35.905333', '35.896389', '35.901281', '35.860491', '35.807607',
'35.832267', '35.882414', '35.983794', '35.974463', '35.930951'], dtype=float)
long = np.array(['14.471970', '14.477780', '14.518173', '14.572245', '14.535320',
'14.455894', '14.373217', '14.336096', '14.351006', '14.401137'], dtype=float)
fig, ax = plt.subplots(figsize=(10, 6))
ax.scatter(lat, long)
# ax.axis('equal')
plt.show()

Categories