When I tried to use 3D Scatter plot with matplotlib, the version on the server was old, Hajimaruyo (slow voice)
I wanted to make the visualization of particle numerical calculations cool in 3D. In particular ↑ I wanted to make something like this. Even if I was a first-class gnuplot teacher, I wanted to do it with gnuplot, but On the other hand, I wanted to use a different plotter once in a while, so I tried my best to make it with matplotlib.
matplotlib is a visualization library that runs on Python. Click here for script.
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
box_size = 4.0
R = 6400.0e+3
print mpl.__version__
fig = plt.figure()
#if version > 1.0.0
#ax = plt.add_subplot(111, projection='3d')
start = 0
stop = 1
step = 1
Nnode = 4
for step in range(start, stop, step):
print step
tag = []
x = []
y = []
z = []
for n in range(Nnode):
data = np.loadtxt("./result/%05d_%05d_%05d.dat" % (step, Nnode, n), skiprows=2, delimiter="\t");
tag.extend(data[:,1])
x.extend(data[:,3])
y.extend(data[:,4])
z.extend(data[:,5])
x_tag = [[], [], [], []]
y_tag = [[], [], [], []]
z_tag = [[], [], [], []]
size_tag = [[], [], [], []]
for i in range(len(x)):
x[i] = x[i]/R
y[i] = y[i]/R
z[i] = z[i]/R
x_tag[int(tag[i])].append(x[i])
y_tag[int(tag[i])].append(y[i])
z_tag[int(tag[i])].append(z[i])
size_tag[int(tag[i])].append(1.0)
clr = ["orange", "gray", "red", "black"]
ax = Axes3D(fig)
for tag in range(4):
ax.scatter(x_tag[tag], y_tag[tag], z_tag[tag], s=size_tag[tag], c=clr[tag], edgecolor=clr[tag], alpha=0.1)
ax.set_aspect('equal')
ax.set_xlim3d(-box_size, box_size)
ax.set_ylim3d(-box_size, box_size)
ax.set_zlim3d(-box_size, box_size)
ax.view_init(9.0, 45.0)
#plt.show()
plt.savefig("./img/%05d.png " % step)
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
First, import.
box_size = 4.0
R = 6400.0e+3
Declaration of a constant.
print mpl.__version__
fig = plt.figure()
#if version > 1.0.0
#ax = plt.add_subplot(111, projection='3d')
I output the version of matplotlib. Since matplotlib uses different 3D plot methods depending on the version ...
start = 0
stop = 1
step = 1
Create a graph by incrementing step by step from start to stop.
tag = []
x = []
y = []
z = []
Array initialization.
for n in range(Nnode):
data = np.loadtxt("./result/%05d_%05d_%05d.dat" % (step, Nnode, n), skiprows=2, delimiter="\t");
tag.extend(data[:,1])
x.extend(data[:,3])
y.extend(data[:,4])
z.extend(data[:,5])
Since the calculation results are distributed in Nnode files, load them into data one by one with loadtxt. Use the extend method to plunge into the array defined earlier. In this case, the first column is the tag, the third column is the x coordinate, the fourth column is the y coordinate, and the fifth column is the z coordinate.
x_tag = [[], [], [], []]
y_tag = [[], [], [], []]
z_tag = [[], [], [], []]
size_tag = [[], [], [], []]
Create a two-dimensional array to plot by changing the color for each tag.
for i in range(len(x)):
x[i] = x[i]/R
y[i] = y[i]/R
z[i] = z[i]/R
x_tag[int(tag[i])].append(x[i])
y_tag[int(tag[i])].append(y[i])
z_tag[int(tag[i])].append(z[i])
size_tag[int(tag[i])].append(1.0)
clr = ["orange", "gray", "red", "black"]
After dividing the coordinates by the standardized constant, the data is plunged into the corresponding array of tags. After that, define the color corresponding to the tag.
ax = Axes3D(fig)
for tag in range(4):
ax.scatter(x_tag[tag], y_tag[tag], z_tag[tag], s=size_tag[tag], c=clr[tag], edgecolor=clr[tag], alpha=0.1)
Create an Axes3D class and flow data into scatter. This needs to be done for each tag. This time size_tag is fixed. Set the edge color to clr [tag]. This makes it possible to draw with borderless particles. Transmittance alpha is consistently 0.1.
ax.set_aspect('equal')
ax.set_xlim3d(-box_size, box_size)
ax.set_ylim3d(-box_size, box_size)
ax.set_zlim3d(-box_size, box_size)
ax.view_init(9.0, 45.0)
This time, the aspect ratios are all equal. Also, set the range for each axis. Set the camera angle with view_init.
#plt.show()
plt.savefig("./img/%05d.png " % step)
Output. You can show () normally, but this time I will make it png with savefig and later make it an anime gif.
Right? Isn't it easy?
Recommended Posts