Animation of xy data in matplotlib [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
So essentially I have a few numbered files like output_0000.dat ... output_1999.dat that each correspond to a consecutive timestep in the simulation of a wave on a string. The files contain the x and y positions for each point on the string at that timestep.
My goal is to plot these files one after another to make an animation, ideally using matplotlib but other software will also do.
If you could also direct me to a helpful tutorial, I'll be very greatful.
Thanks!

The animation tutorial suggested by #tom are probably the ideal solution, however the simplest way to do what you want is to use interactive plotting with filenames padded by zeros and numpy's genfromtxt,
import numpy as np
import matplotlib.pyplot as plt
plt.ion()
plt.show()
for i in range(2000):
filename = "output_{0:04d}.dat".format(i)
print("printing file = ", filename)
xy = np.genfromtxt(filename)
x = xy[:,0]; y = xy[:,1]
plt.plot(x,y)
plt.pause(0.01)
You may need to add delimiter to genfromtxt depending on the format of the data in your files.

Related

Calculating overlap of polygons using Python, unsure of which package to use [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed last month.
Improve this question
I am relatively new to Python generally and GIS specifically and wondered if I could get some advice on the best package to use to calculate the overlap of two polygons I have calculated and stored in GeoJson format.
Basically, I have two lists, one is a few large polygons and the other is a lot of small polygons. I would like to know for each of the large polygons which of the smaller ones are wholly or partially within the large one, storing the percentage of the smaller polygon contained in each case.
From an Udemy course I have completed, I think that GeoPandas might be suitable but would it be better to just use Shapely, given I have the GeoJson of the two polygons I wish to check, or is there a better package?
Thanks in advance.
if you have your data in geojsons or geopandas dataframes, this is the easiest way to do it:
import geopandas as gp
import matplotlib.pyplot as plt
gdf1 = gp.read_file(jsonfile) #one set of polygons
gdf2 = gp.read_file(otherjsonfile) #another set of polygons
gdf3 = gdf1.intersection(gdf2) #this calculates the intersection between all shapes in gdf1 and gdf2
gdf1.plot(ax=ax, color='r', alpha=0.5):
gdf2.plot(ax=ax, color='b', alpha=0.5):
and now, the intersection..
gdf3.plot(ax=ax, color='g', alpha=0.5)

What toolkit in Python can achieve the effect as shown in the figure [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
I'd like to draw a three-dimensional trajectory that can show the attitude of the plane as shown in the figure. What toolkit in Python I should learn?
What you are looking for is 3D Line plots of Matplotlib.
To help you get started, you can refer to this Blog: An easy introduction to 3D plotting with Matplotlib
The official Tutorial of matplotlib 3D Plots can be found here: Matplotlib 3D Tutorial - Lines
If you want to explore more, a very good resource to different types of Graph Plots along with code examples can be found here: Python Graph Gallery
the standard plotting library in python (pretty much) is Matplotlib. It contains a 3D API called mplot3d.
For an example, see here: https://matplotlib.org/stable/gallery/mplot3d/scatter3d.html
The example you are showing does look a lot like it was created with Matplotlib. Other options include seaborn and bokeh. Your choice what to use, really.

Natural combination between Python and TeX [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
As a graduate, I often plot various kind of figures and have found many plotting software.
I appreciate the high quality drawings in TeX, using tools like Asymptote and Tikz. As a poor programmer, I also need the flexibility brought by Python when I need do some complex calculations during plotting. The combination from both side, which I deeply believe, will be a perfect choice.
So, is there any chance for me to find some software like this, with both Simple and Beatiful in mind.
There are several python libraries which excel at various types of graphics such as MayaVi.
Here is an example of simple TeX integration with matplotlib in python (not showing all the math stuff, but just the plotting code:
figure(2)
plot(x,Ni[:,1], 'b.', x, y, 'r.', x, z, 'g.', markersize=10)
xlabel('nm')
ylabel('Sulfur normalized intensity')
legend(['Ni/S', r'exp profile $\tau$ = 200 nm', r'exp profile $\tau$ = 98 nm'], loc='best', numpoints=1)
pylab.subplots_adjust(bottom=0.15, left=0.15)
show()
which produces:
Notice the tau is actually just a snippet of TeX embedded into the image. Most of the matplotlib commands allow this so you can draw equations into your image or whatever you want to do.

Contour from 2D image [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm wondering if it's possible to plot a contour (level curves) graphic from a 2D image in Python. More specific, I'm wondering how could I read an image data (the set of the grayscale pixels, for example), and use it for the z input
I'm reading some articles about the matplotlib but I couldn't find an example which the input is an image.
You can use the function contour from matplotlib.
import numpy as np
import pylab as plt
# Sample data
row = np.linspace(-2,2,20)
X,Y = np.meshgrid(row,row)
Z = np.exp(-((X-1.5)**2+(Y+1)**2))
Z += np.exp(-((X)**2+(Y)**2))
plt.subplot(121)
plt.imshow(Z,interpolation='none',origin='lower')
plt.subplot(122)
plt.contour(X,Y,Z)
plt.show()
print X,Y
You can also fill them in with contourf instead

plot Planetary Boundary layer height [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I would like to know if someone has an easy program to plot Planetary Boundary Layer height on a map (2D in lat/lon) in fortran 90 or python (or NCL).
I am using a program in F.90 but it is not working so I would like to compare with a second program.
Thank you
Here's a Python example with matplotlib and netCDF4 modules:
import matplotlib.pyplot as plt
from netCDF4 import Dataset
nc = Dataset('mydatafile.nc','r')
lon = nc.variables['lon'][:]
lat = nc.variables['lat'][:]
pblh = nc.variables['pblh'][:]
nc.close()
plt.contourf(lon,lat,pblh)
plt.colorbar()
plt.savefig('pblh.png')
ptl.clf()
You may need to edit this example to match your data, e.g. filename, variable names etc.

Categories