I want to work with a scatter plot within a FigureCanvasQTAgg. The scatter plot may have 50,000 or more data points. The user wants to draw a polygon in the plot to select the data points within the polygon. I've realized that by setting points via mouse clicks and connect them with lines using Axis.plot(). When the user has set all points the polygon is drawn. Each time I add a new point I call FigureCanvasQTAgg.draw() to render the current version of the plot. This is slow, because the scatter plot has so much data.
Is there a way to make this faster?
-
You should decimate your data points before drawing them in the scatter plot. If decimation is not possible, then you should only show the region where the user wants to draw the polygon. This should speed up your rendering.siavashk– siavashk2017-03-07 19:47:08 +00:00Commented Mar 7, 2017 at 19:47
-
4There are 409 questions for matplotlib "slow" and 354 for matplotlib "faster". Does none of them help? In that case, be more specific and tell us what you have tried and why it didn't help.ImportanceOfBeingErnest– ImportanceOfBeingErnest2017-03-07 19:47:59 +00:00Commented Mar 7, 2017 at 19:47
-
why do you need that many data points? what's the end goal of this code?Mohammad Athar– Mohammad Athar2017-03-07 19:55:04 +00:00Commented Mar 7, 2017 at 19:55
-
@MohammadAthar the goal is to cut out certain points and write the reminder to a file. This is done easiest by visual inspection.Michael– Michael2017-03-07 20:10:26 +00:00Commented Mar 7, 2017 at 20:10
-
1Not providing the necessary code and not narrowing down the scope of the question simply lowers the chances of getting a satisfying answer. At the end, it's your choice. Personnally, I find it very unsatifactory to write a complete minimal example and do the reseach myself, thereby spending more time than the questioner on the solution.ImportanceOfBeingErnest– ImportanceOfBeingErnest2017-03-09 12:59:06 +00:00Commented Mar 9, 2017 at 12:59
|
Show 3 more comments
2 Answers
Two possible solutions:
- Don't show a scatter plot, but a hexbin plot instead.
- Use blitting.
(In case someone wonders about the quality of this answer; mind that the questioner specifially asked for this kind of structure in the comments below the question.)
1 Comment
Michael
Interesting idea, but I do have more than one colour. How about that: (I put it inot a seperate answer)
I can try to convert the scatter plot into an image using matplotlib to render it and display the image with imshow:
import matplotlib
matplotlib.use('QT4AGG')
import matplotlib.pyplot as plt
import Image # PIL
from io import BytesIO
from matplotlib import image
plt.scatter(xdata, ydata)
plt.axis('off')
plt.subplots_adjust(0, 0, 1, 1, 0, 0)
stream = BytesIO()
plt.savefig(stream, format='raw')
pilImage = Image.fromstring('RGBA',size=(640, 480), data = stream.getvalue())
plt.imshow(image.pil_to_array(pilImage))