How to plot a circle in julia - julia

I am working on an optimization problem in Julia in which I find the 2D Chebyshev center. I can find the optimal solution but I am at a loss on plotting it.
Lets say the center is c = (x1,y1) and I have radius r of the circle. I need to plot the Chebyshev circle at a center of c and radius r inside of the polygon:

Looking at your plot you are using PyPlot.jl. In this case this is what you can do:
using PyPlot
x = [0,500,600,300,0,0]
y = [0,0,300,500,500,0]
plot(x,y)
plt.gcf().gca().add_artist(plt.Circle((264.978,264.978), 214.976, fill=false))
The general rule for PyPlot.jl is that you can access methods of Python's objects using a getindex with a Symbol passed as method name.

Related

Is there any ax.view_init(elev, azim) equivalent function in plots.jl?

I am using Plots for visualizing 3d-plots with Julia, and I am trying to change camera angle of my plot. In matplotlib in Python, I know that I can use ax.view_init(elev, azim) to change the camera angle, but on Plot.jl, I could not find solution to change the angle.
Is there any equivalent function with ax.view_init(elev, azim) in Python in Julia ?
Example of Plot
using Plots
plot()
for i in 1 : 5
a = rand(10); b= rand(10); c = rand(10);
plot!(a,b,c, seriestype=:scatter)
end
plot!()
As you can read in the manual you can use the camera keyword argument (aliases are: cam, cameras, view_angle, viewangle). This argument sets the view angle for 3D plots. Its value is required to be a tuple (azimuthal, elevation) and the default setting is (30, 30).

Drawing an arrow with specified direction on a point in scatter plot in Julia

Is there a way to draw a scatter plot in Julia (preferably with gr backend), in which every point has an arrow pointing to a specified direction on it?
Specifically, my task is to create a gif image with multiple moving points with a small arrow on every point pointing to the direction of its velocity.
So, you want to plot a vector field, right?
The "arrow plot" you are looking for, is usually called quiver-plot in many programming languages. In Julia, too.
If you use Plots.jl the syntax is quiver(x,y,quiver=(u,v)), where x and y are the coordinate vectors and u and v the arrow magnitude vectors.
If you use GR or PyPlot directly the syntax is possibly a bit different.
Small Example
using Plots
gr()
N = 10
x = rand(1:10,N)
y = rand(1:10,N)
u = rand(N)
v = rand(N)
scatter(x,y)
quiver!(x,y,quiver=(u,v))

Why is my plot3d white in SciLab?

t = 0:%pi/50:10*%pi;
plot3d(sin(t),cos(t),t)
When I execute this code the plot is done but the line is not visible, only the box. Any ideas which property I have to change?
Thanks
The third argument should, in this case, be a matrix of the size (length arg1) x (length arg2).
You'd expect plot3d to behave like an extension of plot and plot2d but it isn't quite the case.
The 2d plot takes a vector of x and a vector of y and plots points at (x1,y1), (x2,y2) etc., joined with lines or not as per style settings. That fits the conceptual model we usually use for 2d plots - charting the relationship of one thing as a function of another, in most cases (y = f(x)). THere are other ways to use a 2d plot: scatter graphs are common but it's easy enough to produce one using the two-rows-of-data concept.
This doesn't extend smoothly to 3d though as there are many other ways you could use a 3d plot to represent data. If you gave it three vectors of coordinates and asked it to draw a line between them all what might we want to use that for? Is that the most useful way of using a 3d plot?
Most packages give you different visualisation types for the different kinds of data. Mathematica has a lot of 3d visualisation types and Python/Scipy/Mayavi2 has even more. Matlab has a number too but Scilab, while normally mirroring Matlab, in this case prefers to handle it all with the plot3d function.
I think of it like a contour plot: you give it a vector of x and a vector of y and it uses those to create a grid of (x,y) points. The third argument is then a matrix whose dimensions match those of the (x,y) grid holding the z-coordinates of each point. The first example in the docs does what I think you're after:
t=[0:0.3:2*%pi]';
z=sin(t)*cos(t');
plot3d(t,t,z);
The first line creates a column vector of length 21
-->size(t)
ans =
21. 1.
The second line computes a 21 x 21 matrix of products of the permutations of sin(t) with cos(t) - note the transpose in the cos(t') element.
-->size(z)
ans =
21. 21.
Then when it plots them it draws (x1,y1,z11), (x1,y2,x12), (x2,y2,z22) and so on. It draws lines between adjacent points in a mesh, or no lines, or just the surface.

Plotting lines between two points in 3D

I am writing an regression algorithm which tries to "capture" points inside boxes. The algorithm tries to keep the boxes as small as possible, so usually the edges/corners of the boxes go through points, which determines the size of the box.
Problem: I need graphical output of the boxes in R. In 2D it is easy to draw boxes with segments(), which draws a line between two points. So, with 4 segments I can draw a box:
plot(x,y,type="p")
segments(x1,y1,x2,y2)
I then tried both the scatterplot3d and plot3d package for 3D plotting. In 3D the segments() command is not working, as there is no additional z-component. I was surprised that apparently (to me) there is no adequate replacement in 3D for segments()
Is there an easy way to draw boxes / lines between two points when plotting in three dimensions ?
The scatterplot3d function returns information that will allow you to project (x,y,z) points into the relevant plane, as follows:
library(scatterplot3d)
x <- c(1,4,3,6,2,5)
y <- c(2,2,4,3,5,9)
z <- c(1,3,5,9,2,2)
s <- scatterplot3d(x,y,z)
## now draw a line between points 2 and 3
p2 <- s$xyz.convert(x[2],y[2],z[2])
p3 <- s$xyz.convert(x[3],y[3],z[3])
segments(p2$x,p2$y,p3$x,p3$y,lwd=2,col=2)
The rgl package is another way to go, and perhaps even easier (note that segments3d takes points in pairs from a vector)
plot3d(x,y,z)
segments3d(x[2:3],y[2:3],z[2:3],col=2,lwd=2)

Polar transform in CSS3?

Turning a line into a ring is a simple task in graphics programs such as GIMP:
(source: adamhaskell.net)
I'm trying to work out if it's possible to produce the same effect in CSS.
So I worked out the following:
The above algorithm maps x to r and y to θ
To do this, x is scaled to the range of [0,w/2], with w being the width of the image
Also, y is scaled to the range of [0,2π]
To transform polar coordinates back into Cartesian: xc = rp*cos(θp) and yc = rp*sin(θp)
The result is then translated so the origin is in the centre of the image.
So we have:
 
x' = (x/2)*cos(y/h*2π) + w/2;
y' = (x/2)*sin(y/h*2π) + h/2;
This is all fine and dandy, but how can I produce such a transform in CSS? Presumably none of the keywords are useful, so it has to be a Matrix transform. Well, I have no idea how to build a matrix from the two equations above, much less how to represent it in a CSS transform.
Can anyone help me on this last step?
I never worked with CSS transform matrices, but I think what you want is not possible.
Using transform matrices you do a linear transformation. Linear transformations ALWAYS map a straight line to a straight line or to 0. Take a look at Wikipedia for more information.
So it's impossible to map a straight line to a circle using matrices.
At least you can make 2 symmetrical third order Bezier curve
Using
Y(t) = (t^3,t^2,t,1) * M * (P0,P1,P2,P3)
t - time
P0 - P3 control points coordinates. This vector must be vertical.I do not know how to make vertical vector in this editor.
Y(t) - curve coordinate
M - 4*4 matrix row 1 (-1,3,-3,1) row 2 (3,-6,-3,0) row 3 (-3,3,0,0) row 4 (1,0,0,0)
Now you need only a function what defines control points from your line coordinate.

Resources