I'm a beginner programmer studying as a student part-time job at a game company called Techcross. This time, when I was studying at graduate school, I had a little trouble drawing a 3D graph with python to analyze the data, so I will make a note for myself. If this helps someone ...
Draw a graph like this. The time overlaps with the axis label, but I don't care. Use matplotlib and Axes3D.
I referred to [Matplotlib] Creating a 3D line graph! Thank you very much.
# -*- coding: utf-8 -*-
import numpy as np ###I don't want to use numpy this time.
import matplotlib.pyplot as plt ###Necessary for drawing graphs regardless of 2D3D
from mpl_toolkits.mplot3d import Axes3D ###Required for 3D
# (x, y, z)
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
z = [1, 2, 3, 4, 5]
#Plot in 3D
fig = plt.figure()
ax = Axes3D(fig)
ax.plot(x, y, z)
#Axis label
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
#display
plt.show()
If you execute it, it will be like this
Let's go crispy.
If you use the for statement, it's a moment.
# -*- coding: utf-8 -*-
import numpy as np ###This time I will use a numpy array so it is necessary
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
###Prepare a 3D graph box(Perhaps)
fig = plt.figure()
ax = Axes3D(fig)
###Make the x-axis common
x = [1, 2, 3, 4, 5]
###This time, we will make three data into one graph.
for i in range(3):
###With 5 elements, all elements are i
y = np.full(5,i)
###Random number with 5 elements and 0 or more and 1 or less elements
z = np.random.rand(5)
#Plot in 3D
ax.plot(x, y, z)
#Axis label
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
#display
plt.show()
When you run
I feel like this. Let's go crispy.
For example, an image that sets the y-axis to "January, February, March".
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
###Array to put y-axis numbers
y_num = np.array([])
###Array to put the character string you want to set on the y-axis
y_axis = np.array([])
x = [1, 2, 3, 4, 5]
for i in range(3):
y = np.full(5,i)
z = np.random.rand(5)
ax.plot(x, y, z)
y_num = np.append(y_num,i)
y_axis = np.append(y_axis,"{}data".format(i))
### y_num is[0,1,2]
### y_axis is[0data,1data,2data]
plt.yticks(y_num,y_axis)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
When you run
Complete~~! !! !! It's easy. At first I couldn't use ax.yticks () instead of plt.yticks () ...
In the second graph, if the numerical spacing of the axes is too small or too large, you can use the function ax.set_xticks (). Let's set the y-axis to date and time in the last graph.
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
###Necessary if you want to display the date and time
from datetime import datetime
import time
fig = plt.figure()
ax = Axes3D(fig)
y_num = np.array([])
y_axis = np.array([])
x = [1, 2, 3, 4, 5]
for i in range(3):
y = np.full(5,i)
z = np.random.rand(5)
ax.plot(x, y, z)
y_num = np.append(y_num,i)
### y_Add date and time as string to axis
y_axis = np.append(y_axis,"2019-1-1 11:11:0{}".format(i))
###The time when it became a string is changed to datetime type
temp_datetime = datetime.strptime(y_axis[i],'%Y-%m-%d %H:%M:%S')
###Extract only time and make it string type
y_axis[i] = temp_datetime.strftime('%H:%M:%S')
plt.yticks(y_num,y_axis)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
When you run
I was able to draw the graph I was aiming for! !! Congratulations.
Oh, ** If you want to process the date and time in python, use datetime. ** ** People who are addicted to this are addicted to it. When dealing with research data, the date and time type may not be datetime, but it is easier to change it to datetime.
Was it a little too crispy? Please comment if you want an explanation. Append. It may be written in a missing way for the date and time. Please let me know if you can write elegantly.
Now that I can draw graphs, I have to study early. ..
Recommended Posts