Extracting data from a 3D scatter plot in matplotlib

2024/11/15 17:52:09

I'm writing an interface for making 3D scatter plots in matplotlib, and I'd like to access the data from a python script. For a 2D scatter plot, I know the process would be:

import numpy as np
from matplotlib import pyplot as pltfig = plt.figure()
ax = fig.add_subplot(111)
h = ax.scatter(x,y,c=c,s=15,vmin=0,vmax=1,cmap='hot')
data = h.get_offsets()

With the above code, I know that data would be a (N,2) numpy array populated with my (x,y) data. When I try to perform the same operation for 3D data:

import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3Dfig = plt.figure()
ax = Axes3D(fig)
h = ax.scatter(x,y,z,c=c,s=15,cmap='hot',vmin=0,vmax=1)
data = h.get_offsets()

The resulting data variable is still an (N,2) numpy array rather than a (N,3) numpy array. The contents of data no longer match any of my input data; I assume that data is populated with the 2D projections of my 3D data, but I would really like to access the 3D data used to generate the scatter plot. Is this possible?

Answer

Indeed, the coordinates obtained via get_offsets are the projected coordinates. The original coordinates are hidden inside the mpl_toolkits.mplot3d.art3d.Path3DCollection which is returned by the scatter in three dimensional axes. You would obtain the original coordinates from the ._offsets3d attribute. (This is a "private" attribute, but unfortunately the only way to retrieve this information.)

import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3Dfig = plt.figure()
ax = Axes3D(fig)
x = [1,2,3,4]
y = [1,3,3,5]
z = [10,20,30,40]
c= [1,2,3,1]
scatter = ax.scatter(x,y,z,c=c,s=15,cmap='hot',vmin=0,vmax=1)
data = np.array(scatter._offsets3d).T
print(scatter)  # prints mpl_toolkits.mplot3d.art3d.Path3DCollection
print(data)# prints
# 
# [[  1.   1.  10.]
#  [  2.   3.  20.]
#  [  3.   3.  30.]
#  [  4.   5.  40.]]
https://en.xdnf.cn/q/71758.html

Related Q&A

How does Pythonic garbage collection with numpy array appends and deletes?

I am trying to adapt the underlying structure of plotting code (matplotlib) that is updated on a timer to go from using Python lists for the plot data to using numpy arrays. I want to be able to lower …

What is the default if I install virtualenv using pip and pip3 respectively?

I used sudo pip install virtualenv, then when I run virtualenv ENV in a directory, I get a Python 2 virtual enviroment.If I use pip3 install virtualenv to install virtualenv again, will it override the…

Flask Admin ModelView different fields between CREATE and EDIT

In a Flask app using Flask Admin I would like to be able to define different form fields in the Edit section of a ModelView than those in the Create section.The form_columns setting applies to both Cre…

Python compilation error: LONG_BIT definition appears wrong for platform

This error message is not unknown, I have already reinstalled many packages, but so far not yet found a solution.I get the following error from the command pip install cryptography/usr/include/python2.…

Randomly sampling lines from a file

I have a csv file which is ~40gb and 1800000 lines. I want to randomly sample 10,000 lines and print them to a new file.Right now, my approach is to use sed as:(sed -n $vars < input.txt) > output…

Is Pythons hashlib.sha256(x).hexdigest() equivalent to Rs digest(x,algo=sha256)

Im not a python programmer, but Im trying to translate some Python code to R. The piece of python code Im having trouble with is:hashlib.sha256(x).hexdigest()My interpretation of this code is that the…

How to do Histogram Equalization on specific area

I have a image and I want to do HE or CLAHE on specific area of the image. I already have a mask for the image. Is there any possible way to do so?

Timing out a multiprocessing function

I need to set a time limit on a python function which use some multiprocessing stuff (I dont know if it matters). Something like this:function(a_list):p1 = Process(a_list[0:len(a_list/2)])p2 = Process(…

How can I get the actual axis limits when using ax.axis(equal)?

I am using ax.axes(equal) to make the axis spacing equal on X and Y, and also setting xlim and ylim. This over-constrains the problem and the actual limits are not what I set in ax.set_xlim() or ax.set…

Python rarfile package: fail to open files

So I was trying to archive a .rar file using rarfile library in Python, but it keeps saying "failed to open". Am using Mac OS X El Capitan, python 2.7. Any help would be appreciated, thanks.O…