首页 > 解决方案 > python matplotlib - How to plot 3+1 dimension data?

问题描述

I've got an array of 4 dimensions. The first 3 dimension, x, y, z are categorical integer variables with value range [-3,3]. The combination of them are unique, i.e. no same x, y, z values in 2 rows. The last dimension value, is a continuous positive float variable. For example:

x    y    z    value
0    0    0    1.0
1    0    1    2.5
-1   1    -1   0.3
3    2    1    0.06

I'd like to visualise the array with matplotlib in python. The plot will be a 3D plot with x, y, z as axes, and each datapoint will be a sphere, the radius of the sphere will be value. I have only done this in Tableau previously. How can I do this with python?

P.S. In the case that value is not necessarily positive, I'd love to know how to use color as indicator of sign of value in addition to above question. Though I suppose an argument of color=sign(value) will do the work.

标签: pythonmatplotlib

解决方案


我已经根据@tnknepp 在Plotting 4d-data中的回答对其进行了整理

代码很简单:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111, projection='3d')

sp = ax.scatter(x,y,z, s=value, c=color)
plt.colorbar(sp)

其中s是气泡的大小,c是颜色。


推荐阅读