Create 3D scatter plot with SciPy + matplotlib (Python)

How to create a 3D scatter plot using SciPy (NumPy) and matplotlib in Python. Sample data uses iris.csv (http://aima.cs.berkeley.edu/data/iris.csv)

plot3d.py


# coding: UTF-8

from matplotlib import pyplot
from mpl_toolkits.mplot3d import Axes3D
from scipy import genfromtxt


#File reading
d = genfromtxt("./temp/iris.csv", delimiter=",")

#Graph creation
fig = pyplot.figure()
ax = Axes3D(fig)

#Axis label settings
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
ax.set_zlabel("Z-axis")

#Display range setting
ax.set_xlim(4, 8)
ax.set_ylim(2, 5)
ax.set_zlim(1, 8)

#Extraction condition setting
d1 = d[d[:,0] >= 7]
d2 = d[(d[:,0] < 7) & ((d[:,1] > 3) & (d[:,1] <= 3.5))]
d3 = d[(d[:,0] < 7) & ((d[:,1] <= 3) | (d[:,1] > 3.5))]


#Graph drawing
ax.plot(d1[:,0], d1[:,1], d1[:,2], "o", color="#cccccc", ms=4, mew=0.5)
ax.plot(d2[:,0], d2[:,1], d2[:,2], "o", color="#00cccc", ms=4, mew=0.5)
ax.plot(d3[:,0], d3[:,1], d3[:,2], "o", color="#ff0000", ms=4, mew=0.5)
pyplot.show()

Output result

A separate window will open and the graph will be displayed. The graph can be rotated freely by dragging. You can output the following png file with the save icon on the menu bar.

出力

Recommended Posts

Create 3D scatter plot with SciPy + matplotlib (Python)
Create plot animation with Python + Matplotlib
3D plot with matplotlib
Create 3d gif with python3
3D scatter plot with PyQtGraph
[Python] How to create a 2D histogram with Matplotlib
[Python] How to draw a scatter plot with Matplotlib
Continuously color with matplotlib scatter plot
Lognormal probability plot with Python, matplotlib
2-axis plot with Matplotlib
[Python] limit axis of 3D graph with Matplotlib
Heatmap with Python + matplotlib
2D plot in matplotlib
Create filter with scipy
(Memorandum) Make a 3D scatter plot with matplodlib
Create a 2d CAD file ".dxf" with python [ezdxf]
Stackable bar plot with matplotlib
Create a directory with python
Study math with Python: Draw a sympy (scipy) graph with matplotlib
Let's create a PRML diagram with Python, Numpy and matplotlib.
Harmonic mean with Python Harmonic mean (using SciPy)
A python graphing manual with Matplotlib.
Create Awaitable with Python / C API
Installation of SciPy and matplotlib (Python)
3D skeleton structure analysis with Python
Create folders from '01' to '12' with python
Solve ABC166 A ~ D with Python
Create a virtual environment with Python!
Create an Excel file with Python3
Installation of Python, SciPy, matplotlib (Windows)
Draw Lyapunov Fractal with Python, matplotlib
When matplotlib doesn't work with python2.7
[Python scipy] Upscale / downscale 2D data
How to display legend marks in one with Python 2D plot
Create a Python function decorator with Class
[Python] Set the graph range with matplotlib
Automatically create Python API documentation with Sphinx
Create wordcloud from your tweet with python3
Build a blockchain with Python ① Create a class
Create a dummy image with Python + PIL.
[Python] Create a virtual environment with Anaconda
2D FEM stress analysis program with Python
Let's create a free group with Python
Quickly create an excel file with Python #python
Create Python + uWSGI + Nginx environment with Docker
Create and decrypt Caesar cipher with python
scipy stumbles with pip install on python 2.7.8
Create miscellaneous Photoshop videos with Python + OpenCV ③ Create miscellaneous Photoshop videos
[Python] Let's make matplotlib compatible with Japanese
Create Excel file with Python + similarity matrix
Solve AtCoder ABC168 with python (A ~ D)
Create a word frequency counter with Python 3.4
Plot CSV of time series data with unixtime value in Python (matplotlib)
[Python] Quickly create an API with Flask
Scatter plot
Create an English word app with python
Create a graph with borders removed with matplotlib
Create an elliptical scatter plot in Python without using a multivariate normal distribution
I wanted to visualize 3D particle simulation with the Python visualization library Matplotlib.
[Scatter plot, 3D plot and regression plane] Plotly dynamic visualization [python, scatter, 3D, surface, pair, joint]
Create a frame with transparent background with tkinter [Python]