I'm trying to create a matplotlib graph that shows how to a certain data set changes over time. What I've been trying to do is create a plot and show it, pause for one second, clear the plot, and then show the next one in the array. I've gotten pretty close with the code below, but sadly it just crashes as is.
for expo in sorted_data:
plt.plot(expo["x"], expo["y"])
plt.show(block=False)
time.sleep(1)
plt.gcf().clear()
sorted_data contains the data sorted by when the data was collected.
Use matplotlib.animation. You can find many examples here: http://matplotlib.org/examples/animation/index.html
Related
I am using Python for processing large datasets.
Each part of the data in the dataset is processed step by step in a cycle.
I need to add some visualization during processing.
Now I do plotting using matplotlib for current portion of data. For each step of the cycle I am executing function plot which plots new figure for each step..
The problem is I have a lot of data and plots... E.g. 1000000 plots, and if I will output it in Jupyter it just crashes. I want to use one figure and update data on it through a cycle and show it to see processing?
I want to create a ONE plot but the data on that figure will be changed on each step of processing - for current portion of data.
How to do that - to see that image during processing (e.g. using jupyter notebook)?
(So running through a cycle in one cell will show only one figure with one plot but the scatter on that plot will change cycle by cycle updating the existing figure)
something like:
update_plot(plot_name: str, datax: np.array, datay: np.array)
that will CHANGE the existing plot (important - I need to see it on the screen on each step - to understand that there is progress and it works correctly) It's like animation where each step - is a frame that needs to be changed. E.g. I am adding a special point on a plot for each step.
I am solving the Cahn hilliard equation, and it's already working. If i plot the figures at different iterations I obtain the correct result at the end. However, I need to show the change in concentration over the course of time in a video and not in multiple plots. I have tried using FuncAnimation but I can't figure out how to do it. I can't create a function that includes the frames. My concentration matrix is already updating after each iteration so how can I just tell the code to plot every update on the video?
I am trying to use a forloop to produce figures for each set of data I have, but while the .show() command produces the correct figure, .savefig() keeps adding the previous plotted values to the new figure.
In my forloop, this is the relevant sample of the code.
import matplotlib.pyplot as plt
plt.plot(X,Y[:,0],'o-')
plt.xlabel('x')
plt.savefig('plot'+str(i)+'.png')
As a comparison, here is the savefig plot and here is that shown by show(). As can be seen, the savefig() plot also plotted the previous result.
You have to close current figure after saving with function
plt.close(): http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.close
Or you have to clean current figure after saving by plt.clf(): http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.clf
I made some beautiful charts before I included plt.clf() to clear the plot each time through the loop.
scatterplot1
scatterplot2
In other words, my previous plots were being added to a single figure as shown in the lots above, within my for loop as well. adding [plt.clf()] to clear the plot each time through the loop fixed this problem being clearing the figure before starting the loop iteration at the top to create a new figure with new plots.
TLDR; I included plt.clf() to clear the plot each time through the loop.
I’ve been working on bokeh plots and I’m trying to plot a line graph taking values from a database. But the plot kind of traces back to the initial point and I don’t want that. I want a plot which starts at one point and stops at a certain point (and circle back). I’ve tried plotting it on other tools like SQLite browser and Excel and the plot seems ok which means I must be doing something wrong with the bokeh stuff and that the data points itself are not in error.
I’ve attached the images for reference and the line of code doing the line plot. Is there something I’ve missed?
>>> image = fig.line(“x”, “y”, color=color, source=something)
(Assume x and y are integer values and I’ve specified x and y ranges as DataRange1d(bounds=(0,None)))
Bokeh does not "auto-close" lines. You can see this is the case by looking at any number of examples in the docs and repository, but here is one in particular:
http://docs.bokeh.org/en/latest/docs/gallery/stocks.html
Bokeh's .line method will only "close up" if that is what is in the data (i.e., if the last point in the data is a repeat of the first point). I suggest you actually inspect the data values in source.data and I believe you will find this to be the case. Then the question is why is that the case and how to prevent it from doing that, but that is not really a Bokeh question.
I am using Python 2.7.9 with matplotlib to display live data taken from a microcontroller in a FigureCanvasTKAgg. The figure itself must always display the last 100 points and resize the Y axis to display and 'snug fit' all points. X axis is updated as well to show the correct time stamp of each point.
This figure update involves redrawing all artists (as all of them change) at every figure update which is called using an after method every 100ms. I am using line.set_data(x,y) to update the actual data points and the required methods for updating the Y and X axes limits, followed by canvas.draw(). This works OK, at almost 5fps(the drawing itself takes ~100ms).
My question is: is there a faster way of doing this? From what I understand the draw method redraws the whole canvas including the subplots. Is there a way to update only the lines displayed and the splines and hence increase the fps?
I tried implementing a solution using ax.draw_artist(lines) and the rest, however this adds new artists and do not delete the previous ones.