如何在 Julia 中绘制向量场? [英] How to plot a vector field in Julia?

查看:26
本文介绍了如何在 Julia 中绘制向量场?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Julia 中绘制一个向量场.我在

解决方案

简短的回答:使用 Plots.jl 中的 quiver.

quiver(x, y, quiver=(u, v))

<小时>

在下文中,我将尝试完全重新创建您在 Matlab 中显示的示例.

首先,我们将导入 Plots 并启用 plotly 后端.

使用绘图情节()

我们需要定义一个类似于Matlab的meshgrid的函数.由于 Plots 将对我们的点数组进行操作,而不管它们的维度如何,我选择简单地使用 repeat 并使用扁平化"输出.

 meshgrid(x, y) = (repeat(x, outer=length(y)), repeat(y, inner=length(x)))

现在,我们可以创建 xyuvMatlab代码.为简洁起见,我们可以使用 @. 宏来向量化给定表达式中的所有调用.

x, y = meshgrid(0:0.2:2, 0:0.2:2)你=@.cos(x) * yv = @.罪(x)* y

从这里,我们可以简单地使用 Plots 中的 quiver 函数,将 uv 作为 2 元组传递给关键字参数颤抖.

quiver(x, y, quiver=(u, v))

结果接近于 Matlab 输出,但似乎 Plots.jl 将箭头缩放为比它们在 Matlab 中的更长.不过,这很容易解决;我们可以简单地将 uv 乘以一个比例常数.

比例 = 0.2你=@.比例 * cos(x) * yv = @.尺度 * sin(x) * y

I want to plot a vector field in Julia. I could not find an example here. Here there are some examples using plotly, however, they do not work for me. I would like to plot the vector field by plotlyjs or plotly.

Here is an example code in Julia:

using Plots
pyplot()
x = collect(linspace(0,10,100));
X = repmat(x,1,length(x));
Y = repmat(x',length(x),1);
U = cos.(X.*Y);
V = sin.(X.*Y);
streamplot(X,Y,U,V)

Here is the Matlab example:

[x,y] = meshgrid(0:0.2:2,0:0.2:2);
u = cos(x).*y;
v = sin(x).*y;

figure
quiver(x,y,u,v)

解决方案

The short answer: use quiver from Plots.jl.

quiver(x, y, quiver=(u, v))


In the following, I'll attempt to fully recreate the example you showed in Matlab.

First, we'll import Plots and enable the plotly backend.

using Plots
plotly()

We need to define a function similar to Matlab's meshgrid. Since Plots will operate on our arrays of points regardless of their dimensionality, I chose to simply use repeat and use the "flattened" outputs.

 meshgrid(x, y) = (repeat(x, outer=length(y)), repeat(y, inner=length(x)))

Now, we can create x, y, u, and v using the same logic as the Matlab code. For the sake of brevity, we can use the @. macro to vectorize all calls in the given expression.

x, y = meshgrid(0:0.2:2, 0:0.2:2)
u = @. cos(x) * y
v = @. sin(x) * y

From here, we can simply use the quiver function from Plots, passing u and v as a 2-tuple to the keyword argument quiver.

quiver(x, y, quiver=(u, v))

The result is close to the Matlab output, but it seems that Plots.jl scales the arrows to be longer than they are in Matlab. This is easily fixable, though; we can simply broadcast-multiply u and v by a scale constant.

scale = 0.2
u = @. scale * cos(x) * y
v = @. scale * sin(x) * y

这篇关于如何在 Julia 中绘制向量场?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆