Trying to update graph using CSV - animation

I have a question. I am trying to plot a live graph while continuously updating a CSV file continuously from a LIDAR sensor. However when I call animation.FuncAnimation(... the graph does not continuously update. If I rerun, I see the graph updated. When I view the CSV file, I see has been updating.
class SecondGraph:
def animate(i):
graph_data = open(NameofCSV,'r').read()
lines = graph_data.split('\n')
xs = []
ys = []
for line in lines:
if len(line) > 0:
x, y = line.split(' ')
xs.append(float(x))
ys.append(float(y))
ax1.clear()
ax1.plot(ys, xs)
ani = animation.FuncAnimation(fig, SecondGraph.animate, interval=10)
When someone has a chance, can someone guide me in the right direction here. I do not think I need to plot my full code, but if you need to see it. Let me know.

Disregard. I think I know the answer. The CSV does not update automatically, which may be the problem. I do not think there is anything wrong with my code that I posted.

Related

My toplevel window in tkinter is no longer being destroyed. It was working fine until I tried changing other aspects of my function

I'm trying to get a popup window to display random text and a picture every time a button is pressed in tkinter. My original code was going to use an if/elif statement to do this. It worked as intended but I thought it might be easier to pair the data in a dictionary since there would be 50 elif statements otherwise (is it frowned upon to use so many? I actually found it easier to read).I was able to get this working but now the toplevel window in tkinter is not being destroyed like it was in the original function. A new Label is just being created on top of it and I can't figure out why. The function code is below. Thanks in advance, any help would be appreciated!
def Add_Gemstone2():
global Addstone
#destroy the previous window if there is one.
try:
AddStone.destroy()
except(AttributeError, NameError):
pass
#create the window.
AddStone=Toplevel()
AddStone.configure(bg='White', height=200, width=325)
AddStone.geometry('325x180+10+100')
# add gemstones to list from file.
gem_stones = open('gemstones.txt')
all_gem_stones = gem_stones.readlines()
gemstones = []
for i in all_gem_stones:
gemstones.append(i.rstrip())
# Add pictures to list.
path = r'C:\Users\Slack\Desktop\PYTHON WORKS\PYTHON GUI PROJECT\gems'
gempictures = []
# r=root, d=directories, f = files
for r,d,f in os.walk(path):
for file in f:
if '.gif' in file:
gempictures.append(os.path.join(r, file))
#create dictionary from lists.
gemdiction = dict(zip(gemstones, gempictures))
key, val = random.choice(list(gemdiction.items()))
# create the labels.
glbl1 = Label(AddStone, text=key, bg='gold', wraplength=300)
glbl1.pack()
image = ImageTk.PhotoImage(Image.open(val))
glbl2 = Label(AddStone, image=image)
glbl2.image = image
glbl2.pack()

Terminal dashboard in golang using "termui"

I am working on drawing graphs on the terminal itself from inside a go code.I found this (https://github.com/gizak/termui) in golang. And used this(https://github.com/gizak/termui/blob/master/_example/gauge.go) to draw graph in my code.
Problem is this , as we can see in the code( https://github.com/gizak/termui/blob/master/_example/gauge.go ), they are passing g0,g1,g2,g3 all together in the end "termui.Render(g0, g1, g2, g3, g4)".
In my case I don't know how many gauges to draw before hand so I used a list to store gauge objects and then tried to pass list to render.
termui.Render(chartList...)
But it creates only one gauge.
This is how I am appending elements in the list.
for i := 0; i < 5; i++ {
g0 := termui.NewGauge()
g0.Percent = i
g0.Width = 50
g0.Height = 3
g0.BorderLabel = "Slim Gauge"
chartList = append(chartList, g0)
}
what I am getting is a gauge for i=4 only. when I am doing termui.Render(chartList...)
Am I doing something wrong?
PS - I have modified question based on the answer I got in this question.
Here is a good read on Variadic Functions
Take a look at the function signature of Render, https://github.com/gizak/termui/blob/master/render.go#L161
func Render(bs ...Bufferer) {
All you need to do is
termui.Render(chatList...)
assuming chartList is a []Bufferer
Edit
You are only seeing one because they are stacking on top of one-another. To see this add
g0.Height = 3
g0.Y = i * g0.Height // <-- add this line
g0.BorderLabel = "Slim Gauge"
From a quick review of the project, it appears there are ways for auto-arranging that have to do with creating rows (and probably columns). So you might want to explore that, or you will need to manually position your elements.

mayavi volume animation not updating

I’m trying to animate a Mayavi pipeline volume:
src = mlab.pipeline.volume(mlab.pipeline.scalar_field(data),vmin=.1*np.max(data),vmax=.2*np.max(data))
that is combined in the pipeline by another dataset represented as a cut plane.
However, I can’t get the volume visualization to update - only the first frame shows up. The animation is stepping through the data correctly (I get different values of the np.max(data[t]) below) but nothing in the visualization changes.
My understanding is that mlab_source_set should re-render correctly, and there’s nothing on the web anywhere that describes this (as far as I can tell).
The animation looks like:
#mlab.show
#mlab.animate(delay=250,ui=True)
def anim(src,data,tax,fig):
"""Animate."""
t = 0
nt = len(tax)
while 1:
vmin = .1*np.max(data[t])
vmax = .2*np.max(data[t])
print 'animation t = ',tax[t],', max = ',np.max(data[t])
src.mlab_source.set(scalar = mlab.pipeline.scalar_field(data[t]), vmin=vmin,vmax=vmax)
t = mod(t+1,nt)
yield
Any thoughts?

Creating Movie for each Generation of Data [duplicate]

This question already has an answer here:
How to create movies on each generation of a for loop in Matlab plot
(1 answer)
Closed 9 years ago.
I have the following code:
figure;
contour(X1,X2,f);
hold on
plot(top(1:size(top,1)), 'rx');
EDIT
figure;
for i = 1: G
contour(X1,X2,f);
hold on
plot(top(1:size(top,1)), 'rx');
end
NB: G is the maximum generation.
This is supposed to plot contours of sphere superimposed with selected individuals. In each iteration of the individuals, the best individuals is selected and these going on until the global optimum is reached. I need to show this in a movie form as shown in this below:
When you runs each stage of the iteration is indicated in the slides attached. This is what i am trying to do. Any idea please?
OK, I am just copying and pasting now, from here.
However I added FrameRate (per second) since you might want to use (or ask) it later.
writerObj = VideoWriter('Your_video.avi');
writerObj .FrameRate = 1; % 1 frames per second animation.
open(writerObj);
fig_h = figure;
for i = 1: G
contour(X1,X2,f);
hold on
plot(top(1:size(top,1)), 'rx');
frame = getframe(fig_h); % or frame = getframe; since getframe gets gcf.
writeVideo(writerObj, frame);
end
close(writerObj);
Now you will have a Your_video.avi file in your working directory.
If VideoWriter is not supported by your matlab, you could use use avifile same as mentioned in this answer (or in mathwork documentaion example here) like this:
aviobj = avifile('Your_video.avi','compression','None', 'fps', 1);
fig_h = figure;
for i = 1:G
contour(X1,X2,f);
hold on
plot(top(1:size(top,1)), 'rx');
frame = getframe(fig_h); % or frame = getframe; since getframe gets gcf.
aviobj = addframe(aviobj, frame);
end
aviobj = close(aviobj);
EDIT
A problem may occur as pointed out by this question also, which is the captured frame is a constant image. If you are running Matlab on windows, this problem may be caused by conjunction of windows in with certain graphics drivers, and may be solved as mentioned in this answer.

problem saving pdf file in R with ggplot2

I am encountering an odd problem. I am able to create and save pdf file using R/ggplot2 and view them while the R Console is running. As soon as I exit the R console, Preview on Mac OS X will no longer display the PDF. I have been able to save .png files w/o problem, but for reasons beyond my control, I need to save in pdf files. The code I am using to save is as follows:
pdfFile <-c("/Users/adam/mock/dir/structure.pdf")
pdf(pdfFile)
ggplot(y=count,data=allCombined, aes(x=sequenceName, fill=factor(subClass))) + geom_bar()
ggsave(pdfFile)
Has anyone encountered a similar problem? If so, what do I need to do to fix it?
Thank you very much for your time.
The problem is that you don't close the pdf() device with dev.off()
dat <- data.frame(A = 1:10, B = runif(10))
require(ggplot2)
pdf("ggplot1.pdf")
ggplot(dat, aes(x = A, y = B)) + geom_point()
dev.off()
That works, as does:
ggplot(dat, aes(x = A, y = B)) + geom_point()
ggsave("ggplot1.pdf")
But don't mix the two.
It is in the R FAQ, you need a print() around your call to ggplot() -- and you need to close the plotting device with dev.off() as well, ie try
pdfFile <-c("/Users/adam/mock/dir/structure.pdf")
pdf(pdfFile)
ggplot(y=count,data=allCombined,aes(x=sequenceName,fill=factor(subClass)))
+ geom_bar()
dev.off()
Edit: I was half-right on the dev.off(), apparently the print() isn;t needed. Gavin's answer has more.
The following plot
pdf("test.pdf")
p <- qplot(hp, mpg, data=mtcars, color=am,
xlab="Horsepower", ylab="Miles per Gallon", geom="point")
p
dev.off()
works in the console but not in a function or when you source this from a file.
myfunc <- function() {
p <- qplot(hp, mpg, data=mtcars, color=am,
xlab="Horsepower", ylab="Miles per Gallon", geom="point")
p
}
pdf("test.pdf")
myfunc()
dev.off()
Will produce a corrupt pdf file and the way to fix it us use
print(p)
within a function.
In a console. "p" is automatically printed but not in a function or when you source the file.
You can also change the filename of your pdf plot within ggsave if you want to call it something other than "ggplot1" or whatever concise object name you chose; just give the filename first and then tell it which plot you're referring to, for example:
a <- ggplot(dat, aes(x = A, y = B)) + geom_point()
ggsave("Structure.pdf",plot=a)

Resources