Update line in matplotlib plot in loop
You need to use matplotlib's interactive mode (see documentation).
Especially, you need to use plt.ion() to turn the interactive mode on, fig.canvas.draw() to update the canvas with the latest changes, and ax.clear() to remove what has been plotted before.
Your code would look like:
plt.ion()
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.scatter(xs, ys)
plt.plot(xs, thetas[0] + thetas[1] * xs, color='red')
# Draw.
fig.canvas.draw()
# Do your updates on thetas.
# ...
# Clear the current plot.
ax1.clear()
# Plot your data again.
ax1.scatter(xs, ys)
plt.plot(xs, thetas[0] + thetas[1] * xs, color='red')
# Update the figure.
fig.canvas.draw()
Drawing and updating line between two images
You can easily do it by subclassing the parent UIView
. Just create a class and copy-paste the code for
DrawView.h
#import <UIKit/UIKit.h>
@interface DrawView : UIView
-(void)refreshWithPointA:(CGPoint)pointA andPointB:(CGPoint)pointB;
@end
DrawView.m
#import "DrawView.h"
@interface DrawView()
@property(nonatomic, strong) UIBezierPath *linePath;
@end
@implementation DrawView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)refreshWithPointA:(CGPoint)pointA andPointB:(CGPoint)pointB{
_linePath = [UIBezierPath bezierPath];
[_linePath setLineWidth:2.0];
[_linePath moveToPoint:pointA];
[_linePath addLineToPoint:pointB];
[self setNeedsDisplay];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
[[UIColor blackColor] setStroke];
[_linePath stroke];
}
@end
Now simply change the parent UIView class on which you have added UIImageView
. And assign tag 1
and 2
to two imageviews. And use the method below in your pangesture selectors
-(void)refreshLine{
CGPoint centreA=[self.view viewWithTag:1].center;
CGPoint centreB=[self.view viewWithTag:2].center;
[self.drawView refreshWithPointA:centreA andPointB:centreB];
}
Put the method in both the gesture recognizers last line like below
- (void)panWasRecognized:(UIPanGestureRecognizer *)panner {
//YOUR OLD CODE
[self refreshLine];
}
- (void)panWasRecognized2:(UIPanGestureRecognizer *)panner {
//YOUR OLD CODE
[self refreshLine];
}
and as well where you are adding the UIImageViews, it will draw the lines immediately. Create an IBOutlet
of your UIView
too with new class assigned.
Thats it.
Cheers.
Draw and update PictureBox
you can try this, it worked for me:
Graphics graph = Graphics.FromImage(image);
if (e.Button == MouseButtons.Left)
{
graph.DrawLine(Pens.Red, From.X, From.Y, e.X, e.Y);
pictureBox1.Image = image;
}
Additionally, I'd recommend you use the MouseMove
event, to make lines more smooth (don't really know if thats what you want, but anyway) like this:
int x, y;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
x = e.X;
y = e.Y;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
Graphics graph = Graphics.FromImage(image);
if (e.Button == MouseButtons.Left)
{
graph.DrawLine(Pens.Red, x, y, e.X, e.Y);
pictureBox1.Image = image;
x = e.X;
y = e.Y;
}
}
hope I could help you :D
Updating line in 3D plot Animation
Check this code:
import matplotlib; matplotlib.use("TkAgg")
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
dotinline = 101
nframe = 7
fig = plt.figure(figsize=(16, 8))
ax = p3.Axes3D(fig)
ax.view_init(0, 90)
def init():
ax.plot([-10, 10], [0, 0], [0, 0], "k")
ax.plot([0, 0], [-10, 10], [0, 0], "k")
ax.plot([0, 0], [0, 0], [-10, 10], "k")
ax.plot([], [], [], "b")
def update_lines(index):
ax.cla()
init()
seg = []
x = np.linspace(-10, 10, dotinline)
power = x**(-(nframe-1)/2+index)
for i in range(len(x)-1):
a = [x[i+1], x[i]]
b = [0, 0]
c = [power[i+1], power[i]]
seg.append([a, b, c])
for data in seg:
ax.plot3D(data[0], data[1], data[2], "-b")
ax.set_xlim3d([-10, 10])
ax.set_xlabel('X')
ax.set_ylim3d([-10, 10])
ax.set_ylabel('Y')
ax.set_zlim3d([-10, 10])
ax.set_zlabel('Z')
ax.set_title('3D Test')
line_ani = animation.FuncAnimation(fig, update_lines, init_func=init, frames=nframe, interval=2000, blit=False)
plt.show()
To erase the previous lines, just add ax.cla()
.
However, this erase also the black line axis; so, after ax.cla()
I run again init()
, in order to re-draw the axes lines.
Finally, it is better to move the block:
ax.set_xlim3d([-10, 10])
ax.set_xlabel('X')
ax.set_ylim3d([-10, 10])
ax.set_ylabel('Y')
ax.set_zlim3d([-10, 10])
ax.set_zlabel('Z')
ax.set_title('3D Test')
inside the update_lines()
function, in order to keep these options even when the lines are erased.
With this code, I get the following animation:
Draw a line graph with constantly updating data?
An alternative to GraphView is AndroidPlot. It supports real time data in a line chart. There is a sample app in the play store. Take a look at the Real time orientation plot example in the app.
Related Topics
What Are the Differences Between the Xmlserializer and Binaryformatter
How Are Nullable Types Implemented Under the Hood in .Net
JSON.Net Adding Backslash While Returning JSON Serialized String
How to Expand a String That Contains C# Literal Expressions at Runtime
Microsoft Visual Studio and C#: How to Visually Add Events to Controls
Eager , Lazy and Explicit Loading in Ef6
C# Marking Class Property as Dirty
Getting Correct Image Rotation
Convert Datatable to Ienumerable<T>
Peculiar Overload Resolution with While (True)
Which Files in a Visual C# Studio Project Don't Need to Be Versioned
ASP.NET Dynamically Button with Event Handler
JSONconvert.Deserializer Indexing Issues
Some Help Understanding "Yield"
Returning a String from a C# Dll with Unmanaged Exports to Inno Setup Script
Fastest Way to Remove Duplicate Value from a List<> by Lambda