Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

python - Extracting data from a scatter plot in Matplotlib

I'm writing an interface to do scatter plots in Matplotlib, and I'd like to be able to access the data from a python script.

Right now, my interface is doing:

scat = self.axes.scatter(x_data, y_data, label=label, s=size)

With a standard axes.plot I can do something like:

line = self.axes.plot(x_data, y_data)
data = line[0].get_data()

and that works. What I'd like is something similar, but with the scatter plot.

Can anyone suggest a similar method?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A scatter plot is drawn using PathCollection, so the x, y positions are called "offsets":

import numpy as np
import matplotlib.pyplot as plt

f, ax = plt.subplots()
scat = ax.scatter(np.random.randn(10), np.random.randn(10))

print scat.get_offsets()

[[-0.17477838 -0.47777312]
 [-0.97296068 -0.98685982]
 [-0.18880346  1.16780445]
 [-1.65280361  0.2182109 ]
 [ 0.92655599 -1.40315507]
 [-0.10468029  0.82269317]
 [-0.09516654 -0.80651275]
 [ 0.01400393 -1.1474178 ]
 [ 1.6800925   0.16243422]
 [-1.91496598 -2.12578586]]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...