0

I am new to Matplotlib and that's why there might be a more efficient way to run my program.

It is plotting a bunch of points with different colours (depending on some factors). It is constantly producing new pictures in a loop of the current colour state. Basically it looks like this:

import matplotlib.pyplot as plt

def getColour():
#calculate some stuff with x and y and the changing factors

while True: 
   fig = plt.figure(figsize=(17,10))
   plt.scatter(x, y , c=getColour())
   plt.show()
   plt.close(fig)

I was trying out clf() as well. However, it didn't change the pace at all. Does anyone have ideas? What am I doing wrong?

Thank you!

Edit: The target is to produce a picture each time it goes through the loop. Since my program is doing this quite slowly, my question is whether there is a way to make it run faster. I am working with python 2.7

pythusiast
  • 11
  • 5
  • 1
    This question lacks a sufficient problem description. What is the desired outcome and what is the problem? What is the environment where you run this? – ImportanceOfBeingErnest Mar 20 '17 at 16:06
  • 1
    You might want to look into [animation examples](http://matplotlib.org/2.0.0/examples/animation/index.html), as it's probably what you are trying to do. However, I must agree with @ImportanceOfBeingErnest - too vague a question, what are you really trying to accomplish? – berna1111 Mar 20 '17 at 16:12
  • Do you want a completely new figure, with new data, scales and everything at each iteration of the loop? Or are you trying to change the data on an existing plot (animation on comment above)? You can [save the figures](http://stackoverflow.com/questions/9622163/save-plot-to-image-file-instead-of-displaying-it-using-matplotlib-so-it-can-be) instead of showing them if you really want so many distinct figures. – berna1111 Mar 20 '17 at 16:22
  • 1
    The speed of the loop mainly depends on three things. **(1)** How long it takes for the colorcomputation to deliver a new color **(2)** How long it takes for the points to be plotted (which is mainly a question of how many points there are) **(3)** How long it takes the user to close the window such that the next one can pop up. Obviously, we cannot judge on (1). We also have no information about the number of points, so we cannot say anything on (2). This leaves (3), but how to improve human reactivity is off-topic here. – ImportanceOfBeingErnest Mar 20 '17 at 16:23
  • Also, if the colour state does not change, how are you keeping track of how many calls to `getColour()` you've already made so you know how those should change? – berna1111 Mar 20 '17 at 16:24
  • @ImportanceOfBeingErnest I am using jupyter notebook which shows the pictures constantly. getColour takes a new factor from a list each time it's been called. I am pretty sure the getColour() function is working fine, cause it's only multiplying stuff. Would it be possible for example to have the figure with all those points (and note remove it and create a new one again and again) to just change their colour dynamically? – pythusiast Mar 20 '17 at 16:34
  • @berna1111 Actually I just need to change the colour of the points for each going-through. Is there a way to do this without creating a new figure again? – pythusiast Mar 20 '17 at 16:36
  • 1
    In my first comment I pointed you to [animation examples](http://matplotlib.org/2.0.0/examples/animation/index.html) in matplotlib (notice the blue text is a link). You can try something similar. – berna1111 Mar 20 '17 at 16:37
  • 1
    Possible duplicate of [Change RGB color in matplotlib animation](http://stackoverflow.com/questions/40080248/change-rgb-color-in-matplotlib-animation) – ImportanceOfBeingErnest Mar 20 '17 at 16:52
  • 1
    It turns out that this question is simply about how to change the color of a scatter plot in an animation. I therefore vote to close it as duplicate of http://stackoverflow.com/questions/40080248/change-rgb-color-in-matplotlib-animation – ImportanceOfBeingErnest Mar 20 '17 at 16:54
  • @ImportanceOfBeingErnest Alright. Thank you. I didn't see this question before! – pythusiast Mar 20 '17 at 17:00

1 Answers1

0

Something like an animation:

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np

ms_between_frames = 100
n_points = 100
x = np.arange(n_points, dtype=float) #EDIT
y = np.random.random(n_points)
z = np.random.random(n_points)

def getColour(x, y, z):
    c = np.empty((len(x),3))
    for i in range(len(x)):
        c[i] = [x[i]/n_points, z[i], 1.-z[i]]
    return c

def update(frame_number):
    global x, y
    z = np.random.random(n_points)
    c = getColour(x, y, z)
    graph.set_color(c)

fig = plt.figure(figsize=(17,10))
ax = fig.add_subplot(111)
graph = ax.scatter(x, y , c=getColour(x, y, z))
animation = FuncAnimation(fig, update, interval=ms_between_frames)
plt.show()

EDIT: made x hold floats so the division inside getColour would not return 0 (could also have made /float(n_points))

By the way, it should be possible to define only one function to update the colours, depending on the arguments you require to do so, to avoid the call overhead.

berna1111
  • 1,811
  • 1
  • 18
  • 23