[Python] [3D line graph] Multiple data in one graph, axis values in characters

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 ...

Introduction

スクリーンショット 2019-11-29 22.56.27.png

Draw a graph like this. The time overlaps with the axis label, but I don't care. Use matplotlib and Axes3D.

For the time being, try plotting something in 3D

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 スクリーンショット 2019-11-29 23.18.35.png

Let's go crispy.

Draw multiple arrays on one graph

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 スクリーンショット 2019-11-29 23.34.54.png

I feel like this. Let's go crispy.

I want to set the value of the axis to string! !!

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

スクリーンショット 2019-11-29 23.48.20.png

Complete~~! !! !! It's easy. At first I couldn't use ax.yticks () instead of plt.yticks () ...

By the way

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 スクリーンショット 2019-11-29 23.57.46.png

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.

At the end

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

[Python] [3D line graph] Multiple data in one graph, axis values in characters
Fizzbuzz in Python (in one line)
Make python segfault in one line
Handle multiple python versions in one jupyter
CGI server (1) python edition in one line
Easily graph data in shell and Python
Decompose command arguments in one line in Python
[Python] Invert bool value in one line
[Python] (Line) Extract values from graph images
[Python] limit axis of 3D graph with Matplotlib
Make a rock-paper-scissors game in one line (python)
Multiple graphs are displayed in one window (python)
One liner in Python
Python text reading for multiple lines and one line
Graph drawing in python
Until drawing a 3D graph in Python on windows10
Swapping values in Python
Draw graph in python
Decrypt one line of code in Python lambda, map, list
Graph time series data in Python using pandas and matplotlib
Multiple regression expressions in Python
Handle Ambient data in Python
php continuous characters in one
Display UTM-30LX data in Python
DMD in Python one dimension
Try LINE Notify in Python
Solve ABC175 D in Python
Avoid multiple loops in Python
Prohibit multiple launches in python
Transfer parameter values in Python
How to display legend marks in one with Python 2D plot
Extract multiple list duplicates in Python
Get Leap Motion data in Python.
[Python] Show multiple windows in Tkinter
Read Protocol Buffers data in Python3
Get data from Quandl in Python
Handle NetCDF format data in Python
Display characters like AA in python
Hashing data in R and Python
Prime number enumeration in one line
Statistical test (multiple test) in Python: scikit_posthocs
Ant book in python: Sec. 2-5 Graph
Delete multiple elements in python list
I tried Line notification in Python
[Introduction] Insert line breaks in Python 3
[Python scipy] Upscale / downscale 2D data
Implemented in 1 minute! LINE Notify in Python