Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FR] Allow plotting function data in quiver() #4890

Open
calumapplepie opened this issue Feb 19, 2024 · 1 comment · May be fixed by #4979
Open

[FR] Allow plotting function data in quiver() #4890

calumapplepie opened this issue Feb 19, 2024 · 1 comment · May be fixed by #4979
Labels
enhancement improving existing functionality good first issue

Comments

@calumapplepie
Copy link

calumapplepie commented Feb 19, 2024

The easiest mathematical way to define a 2D vector field is as a function with two arguments that outputs a 2 item vector

julia> vecField(x,y) = [x^2, y+1x]

Unfortunately, quiver can't plot this

julia> quiver(vecField)  
ERROR: vecField is not a Function, or is not defined at any of the values [-5.0, -1.0, 0.0, 0.01]

It'd be pretty neat if we could add some recipie that evaluates a 2d function given to quiver at reasonable points, making plotting a vector field into a nice, neat one-liner, instead of the current solution which is somewhat more involved. Such a recipe would also probably want to apply scaling to arrows, perhaps by dividing all outputs by the norm of the largest output.

@BeastyBlacksmith BeastyBlacksmith added enhancement improving existing functionality good first issue labels Feb 19, 2024
@calumapplepie
Copy link
Author

calumapplepie commented Feb 24, 2024

Here's some code that does what I want the recipie to do; however, I don't understand the recipie syntax well enough to integrate it directly.

function plotVecField(
   f = (x,y)->[x,y], xlims=[-2,2], ylims = [-2,2], steps = 10
   )
	xSteps = steps;
	ySteps = steps;
	
	xAxis = range(xlims[1],xlims[2],xSteps)
	yAxis = range(ylims[1],ylims[2],ySteps)
	
	xData = [x for x=xAxis for y=yAxis];
	yData = [y for x=xAxis for y=yAxis];
	u = [f(x,y)[1] for x=xAxis for y=yAxis ];
	v = [f(x,y)[2] for x=xAxis for y=yAxis ];

	xScaleFactor = step(xAxis)/maximum(filter(!isnan,vcat(u,v)))
	yScaleFactor = step(yAxis)/maximum(filter(!isnan,vcat(u,v)))
	
	return quiver(xData,yData,quiver=(xScaleFactor .* u,yScaleFactor .* v))
   return;
end;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement improving existing functionality good first issue
Projects
None yet
2 participants