Considering the following example (from Sjoerd Solution on plotting a ConvexHull)
Needs["ComputationalGeometry`"]
pts = RandomReal[{0, 10}, {60, 2}];
dtpts=DelaunayTriangulation[pts]
I would now like to plot the DelaunayTriangulation for a set of point but can`t figure out the Plot syntax using Graphics.
Thoughts ?
Method one, using polygons like Sjoerd, but without the problem caused by points on the convex hull:
Graphics[{FaceForm[], EdgeForm[Black],
Polygon[pts[[#]] & /#
DeleteCases[dtpts, {i_, _} /; MemberQ[ConvexHull[pts], i]][[All,
2]]], Red, Point[pts]}]
Method two, using lines connecting the adjacent points:
edges[pts_, {a_, l_List}] := {pts[[a]], #} & /# pts[[l]]
Graphics[{Line[edges[pts, #]] & /# dtpts, Red, Point[pts]}]
Both of these methods result in duplicated primitives (three polygons or two lines, from using each point as a starting point.)
We can modify the data slightly and use built in visualization functions:
Graphics[{FaceForm[], EdgeForm[Black],
Cases[Normal[
ListDensityPlot[{##, 0.} & ### pts, Mesh -> All]], _Polygon,
Infinity], Red, Point[pts]}, ImageSize -> 175]
Graphics[
GraphicsComplex[
pts,
{
Function[{startPt, finishPts},Line[{startPt, #}] & /# finishPts] ### dtpts,
Red, Point#Range[Length#pts]
}
]
]
And if you need real polygons:
Graphics[
GraphicsComplex[
pts,
{EdgeForm[Black],
Function[{startPt, finishPts},
{FaceForm[RGBColor[RandomReal[], RandomReal[], RandomReal[]]],
Polygon[{startPt, ##}]} & ###
Transpose[{Drop[finishPts, 1],
Drop[RotateRight#finishPts, 1]
}
]
] ### dtpts,
Red, Point#Range[Length#pts]
}
]
]
I like Sjoerd's use of GraphicsComplex, but I don't see the need for the baroque code in the middle.
This appears to work just fine:
Needs["ComputationalGeometry`"]
pts = RandomReal[{0, 10}, {60, 2}];
dtpts = DelaunayTriangulation[pts];
Lines
Graphics[GraphicsComplex[
pts,
{Line /# Thread /# dtpts, Red, Point#Range#Length#pts}
]]
Polygons
Graphics[GraphicsComplex[
pts,
{
EdgeForm[Black],
( {FaceForm[RGBColor ## RandomReal[1, 3]], Polygon##} & /#
Append ### Thread#{Partition[#2, 2, 1], #} & ) ### dtpts,
Red,
Point#Range[Length#pts]
}
]]
Related
HW problem:
Use matplotlib to plot 𝑠𝑖𝑛(𝑥) for x ∈ [ 0 , 𝜋/4 , 𝜋/2 , 3𝜋/4 ... 2𝜋 ]. Use both orange points and a green line.
notes in class don't talk about how to set axes or colours; I'm still trying to wrap my head around how to use Stack Overflow, please help!
Following code is what I have so far, but I want to add the orange points... If I need to totally change my code, I am okay with that.
x = [ (n*(pi/4)) for n in range(10) ]
a = [ sin(theta) for theta in x ]
plt.plot(x, a, 'g-', label='sine')
plt.axis([0, (2*pi), -1, 1.0])
plt.show()
Take a look at the documentation for plot. In particular, look at the "Other Parameters" section, and the list of keyword arguments. Every option you need is listed there.
im having trouble plotting the results of simplex method used for linear programing in Maxima
i used
minimize_lp(-4*x-5*y,[5*x+4*y<=40, x+4*y<=32, 5*x<=30]);
to solve the problem but now i need to plot this
tried this:
wxplot2d(minimize_lp(-4*x-5*y,[5*x+4*y<=40, x+4*y<=32, 5*x<=30]),[x,-10,10],[y,-10,10]);
all i get is "expression evaluates to non-numeric value everywhere in plotting range" and "nothing to plot" messages
i dont really use Maxima, im not very familiar with it,
its an assigment for a course, any help would be great
Graphical solution for a linear optimization problem subject to constraints
To plot the inequalities and the feasible set I did the following:
Step 1 – solving algebraically using minimize_lp()
load("simplex")$
minimize_lp(
-4*x-5*y,[
5*x+4*y<=40,
x+4*y<=32,
5*x<=30,
x>=0,y>=0
]
);
which yields
%o2) [-91/2,[y=15/2,x=2]]
Step 2 – extracting the coordinates of the optimizing point from %o2
soln: %o2$
c4sol: soln[2]$
x_c: rhs(c4sol[2])$
y_c: rhs(c4sol[1])$
pOpt: [x_c,y_c];
This outputs
(%o7) [2,15/2]
the coordinates of the optimizing point pOpt in a list that we later need for plotting.
Step 3 – defining the feasible set
We know from minimize_lp() that f(x,y) is minimized at (x=2,y=15/2) with
f=-91/2. So we rewrite the target function as -4x-5y=-91/2
tfm: solve(-4*x-5*y=-91/2,y)$
Next we use the restriction r1,r2,r3 to define the the bounderies of the feasible set by turning the inequalities into equalities and solving for the dependent variables:
r1: solve(5*x+4*y=40,y)$
r2: solve(x+4*y=32,y)$
r3: solve(5*x=30,x)$
We then create a list L containing:
L: [ev(y,tfm), ev(y,r1), ev(y,r2), ev(x,r3)];
which yields:
(%o12) [-(8*x-91)/10,-(5*x-40)/4,-(x-32)/4,6]
Step 4 – The plots
We use plot2d() for quick'n'dirty sketches:
plot2d(L,[x,1.5,5.25],[y,5,8.25])$
For the final plot we use draw2d
draw2d(
proportional_axes=xy ,
xrange = [1.5,5.25],
yrange = [5.00,8.25],
point_type = filled_circle, point_size=1.25,
color = black,
points(pOpt),
label(["pOpt = (2,15/2)", 2.50, 7.65]),
key = "tfMin=-91/2",
line_width = 2,
color = steelblue4,
explicit(ev(y,tfm ), x,0,12.25),
key = "r1",
line_width = 1.5,
color = DeepPink3,
explicit(ev(y,r1), x,0,12.25),
key = "r2",
line_width = 1.5,
color = cyan4,
explicit(ev(y,r2), x,0,12.25),
key = "r3",
line_width = 1.5,
color = orange,
explicit(ev(x,r3), x,0,12.25),
key = "feasible set",
border = false,
fill_color = LightPink2,
polygon([[3.20,6],[2,7.5],[3.9,6]])
)$
Hope this helps, after all these years.
Cheers
Tilda
I'm struggling to use the functions MultinormalDistribution and InverseCDF in MultivariateStatistics package. Essentially
<< MultivariateStatistics`
sig = .5; u = .5;
dist = MultinormalDistribution[{0, 0}, sig*IdentityMatrix[2]];
delta=InverseCDF[dist, 1 - u]
The output is
InverseCDF[
MultinormalDistribution[{0, 0}, {{0.5, 0}, {0, 0.5}}], {0.5}]
can someone correct the above code? If I've understood this correctly, delta should be a single number.
1) MultinormalDistribution is now built in, so don't load MultivariateStatistics it unless you are running version 7 or older. If you do you'll see MultinormalDistribution colored red indicating a conflict.
2) this works:
sig = .5; u = .5;
dist = MultinormalDistribution[{0, 0}, sig IdentityMatrix[2]];
delta = CDF[dist, {xx, yy}]
(*1/4 Erfc[-1. xx] Erfc[-1. yy]*)
note it is a 2d distribution so that CDF needs two variables in its second argument. The "inverse" of this is a curve in {xx,yy} space. I don't think InverseCDF works for such multivariate distributions however.
You can visualize your inverse like this:
ContourPlot[delta == 1/2 , {xx, -2, 4}, {yy, -2, 4}]
I am trying to assign a label to a specific point that I draw with
set object circle at first 3,3 radius char 1.5 fill color rgb "red" fillstyle solid no border
I can't find any way to actually draw a circle with set object, and also assign a label to it, without being forced to create a separate label and place it manually beside the point.
IS there a simple way to just plot a single point with a label, beside set object circle and a separate label? Thanks.
For your special case of a circle, you can use set label ... point pointtype ... to attach a point from the available pointtypes to the label:
set label 'mylabel' at 0,0 left offset char 2,0 point pointtype 7 pointsize 5 lc rgb 'blue'
plot x
As far as I know, its not possible. Even the demo script uses a label and a object separately. You could define your own macros though (this example uses boxes not circles):
#!/usr/bin/gnuplot -p
set macro
labelMacro(i,x,y,l) = sprintf('set obj %d rect at %f,%f size char strlen("%s"), char 1; set label %d at %f,%f "%s" front center', i, x, y, l, i, x, y, l)
label1 = labelMacro(1, 0, 0, "Hello World")
label2 = labelMacro(2, -2, -2, "Hello World")
label3 = labelMacro(3, 2, 2, "Hello World")
label4 = labelMacro(4, -2, 2, "Hello World")
#label1; #label2; #label3; #label4;
set xrange [-5:5]
plot x
The above code produces the following graph:
In gnuplot labels and objects are different things, they occupy different "namespaces".
#ilent2's answer is workable, with the small modification that instead of macros you can use eval:
labelMacro(i,x,y,l) = sprintf('set obj %d rect at %f,%f size char strlen("%s"), char 1; set label %d at %f,%f "%s" front center', i, x, y, l, i, x, y, l)
eval labelMacro(1, 0, 0, "Hello World")
Relatively new versions of gnuplot also have boxed labels:
set label 137 "foo" boxed
Here is a problem that I don't know if can be solved in Mathematica.
(* Courtesy to Lunchtime Playground Blog *)
to3d[plot_, height_, opacity_] :=
Module[{newplot}, newplot = First#Graphics[plot];
newplot = N#newplot /. {x_?AtomQ, y_?AtomQ} -> {x, y, height} /.
Arrowheads[List[List[x_, y_, notz_]]] ->
Arrowheads[List[List[x, y]]];newplot /.GraphicsComplex[xx__] -> {Opacity[opacity], GraphicsComplex[xx]}];
(* A function to combine 2D Graphics object in Mathematica *)
test[list_]:=VectorQ[list,SameQ[Head[#],Graphics]&];
My3DPlot[list_?(test[#]&),height_?(VectorQ[#,NumberQ]&),opacity_?(VectorQ[#,NumberQ]&),opts:OptionsPattern[]]:=Block[{a},a=MapThread[Graphics3D[to3d[#1,#2,#3]]&,{list,height,opacity}];
Show[a,opts]
]
(* List of 2D graphics *)
list=Table[ContourPlot[y+Sin[x^i+i y],{x,-3,3},{y,-3,3},Contours->15,ContourLines->False,ColorFunction->RandomChoice[ColorData["Gradients"]]],{i,{1,2,3,4}}];
(* List of heights where you want to place the images *)
height={-.5,0,.5,1};
(* List of opacities you want to apply to your 2D layers *)
opacity={1,.8,.7,.5};
(* The function inherits all the options of standard Graphics3D as they are passed through the Show command *)
My3DPlot[Reverse#list,height,opacity,Lighting->"Neutral",BoxRatios->{1,1,.9},Axes->True]
Now this returns a cool picture like this one.
Here my question is if it is possible to create a filling for this 2D layers using the same color functions as are used with in the contour plots for example? Target is to fill the hollow between these 2D layers with some light or color that continuously changes according to the neighboring layer color-function.
I hope this can be done in Mathematica but my limited knowledge in Mathematica graphics is making it a difficult hurdle for me.
It should be possible. Texture can be used to generate a 3D texture. The example given in the documentation:
data = Table[{r, g, b}, {r, 0, 1, 1/20}, {g, 0, 1, 1/20}, {b, 0, 1, 1/20}];
Graphics3D[
{
Opacity[1/3],
Texture[data],
EdgeForm[],
Polygon[Table[{{0, 0, z}, {1, 0, z}, {1, 1, z}, {0, 1, z}}, {z, 0, 1, 1/20}],
VertexTextureCoordinates ->
Table[{{0, 0, s}, {1, 0, s}, {1, 1, s}, {0, 1, s}}, {s, 0, 1, 1/20}]]
},
Lighting -> "Neutral"
]
This simulates a volume by using a large set of planes. You can do the same. All you have to do is describe the 3D texture, which should interpolate between the planes you already have.Blend would be the function to be used here. For each pixel column in your cube the color varies as Blend[{col1,col2,col3,...},x] with x going from 0 to 1 and coli the color of the pixel in the ith plane given by the contour plots.
The main problem will be that a 3D semi-transparant object with fuzzy color gradients is not something that visualizes very well.