Setting step-size with Locator and Manipulate in Mathematica - graphics

Given this Mathematica code,
Manipulate[Graphics[Line[{{0, 0}, p}], PlotRange -> 2], {{p, {1, 1}}, Locator}]
How do I set the step distance on the locator? And if possible, constrain them?

You could do something like
Manipulate[
Graphics[Line[{{0, 0}, p}],
PlotRange -> 2], {{p, {1, 1}}, {-1, -1}, {1, 1}, {0.4, 0.5}, Locator}]
which would restrict the locator to a rectangular lattice with a horizontal spacing of 0.4 and a vertical spacing of 0.5. The range of the coordinates for the locator is specified by {xmin,ymin} = {-1,-1} and {xmax, ymax} = {1,1}.
If you want more flexibility, e.g. you want to restrict the position of locator to a non-rectangular lattice or to a more general set of coordinates you could do something like
Manipulate[
With[{tab = RandomReal[{-1, 1}, {40, 2}]},
LocatorPane[Dynamic[p, (p = Nearest[tab, #][[1]]) &],
Graphics[{Line[{{0, 0}, Dynamic[p]}], {Red, Point /# tab}}, PlotRange -> 2]]],
{{p, {1, 1}}, ControlType -> None}]

The documentation states:
Manipulate[expr, {u, umin, umax, du}]
allows the value of u to vary between umin and umax in steps du.
and
Manipulate[expr, {u, {u1, u2, u3,...}}]
allows u to take on discrete values.
One of these approaches should work for you.

Related

Mathematica: difficulty using Multinormal Distribution and InverseCDF functions

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}]

How can I make a gradient background for a graphics box in Mathematica?

I have seen an answer to this problem for plots, but I want to do it for a general graphics box.
Graphics[{Disk[], Red, Rectangle[{-.75, 1}, {.5, -.5}]},
Background -> Green]
I would like to make the background of this graphic resemble one of the color palette gradients. Ideally I would type "RainbowColors" somewhere in the code and the background would look like the RainbowColors palette.
I could remove the background and make a rectangle with a gradient fill, but it's not exactly what I'd like. I'm hoping someone has a beautiful bit of knowledge.
Thanks so much!!
The closest I am aware of is this combination of Prolog, Polygon, VertexColors, and Scaled coordinates:
Graphics[
{Disk[], Red, Rectangle[{-.75, 1}, {.5, -.5}]},
Prolog ->
Polygon[
Scaled /# {{0, 0}, {0, 1}, {1, 1}, {1, 0}},
VertexColors -> {Yellow, Green, Magenta, Pink}
]
]
More complicated gradients can be formed by stacking Polygons:
Graphics[
{Disk[], Red, Rectangle[{-.75, 1}, {.5, -.5}]},
Prolog ->
{Polygon[
Scaled /# {{0, 0}, {0, 1/2}, {1, 1/2}, {1, 0}},
VertexColors -> {Red, Yellow, Pink, Blue}
],
Polygon[
Scaled /# {{0, 1/2}, {0, 1}, {1, 1}, {1, 1/2}},
VertexColors -> {Yellow, Green, Magenta, Pink}
]}
]
If you struggle with turning this into a function let me know where you had trouble and I will try to help.
Please ask your future questions on the dedicated Mathematica StackExchange site:

Mathematica Dashing "Up and down"

Is there any reason that underlies mathematica's way of presenting this graph
ListPlot[
Table[{x, x*01}, {x, -5, 5, .08}],
PlotStyle -> White,
Filling -> 0,
FillingStyle -> {Dashed, Brown}]
While the dashing is present for the part of the graph above the zero boundary, another part of the graph has the filling that is solid.
Am I doing something wrong?
Not that wrong. Mathematica is interpreting your filling style as being Dashed below zero and Brown above. You just need another pair of braces, like so:
ListPlot[Table[{x, x*01}, {x, -5, 5, .08}], PlotStyle -> White,
Filling -> 0, FillingStyle -> {{Dashed, Brown}}]
Hope that helps.

Filling in a Graphics3D object

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.

Arrange & Display Parts of Grids in Mathematica

In order to find quickly the location of my data, I display a table with my variables names as well as informations about each.
Since I have a lot of columns (variable) I copy and paste blocks of it in a cell to have them all on 1 screen.
I would like to code this, such that I would input several range of Rows to be extracted and efficiently displayed some how on a Grid that would fit an area of the screen ? I have failed yet to display 2 grid nicely together.
In case I did not express my problem properly above, here is a simple example:
How can I move the blue part to the side of the pink one if the output of laList is what we have to deal with ?
co1 = Range[6];
co2 = Range[11, 16];
co3 = {"A", "B", "C", "D", "E", "F"};
laList = Join[{co1}, {co2}, {co3}] // Transpose;
laListGraph = Grid[laList,
Dividers -> All,
Alignment -> {Left, Center},
ItemSize -> Automatic,
ItemStyle -> Directive[FontSize -> 14, Black, Italic, Bold],
Spacings -> {2, 1},
Background -> {None, None, {
{{1, 3}, {1, 3}} -> LightRed,
{{4, 6}, {1, 3}} -> LightBlue
} } ]
EDIT:
On second thoughts, what I had earlier is not what you wanted... you want it displayed as columns, but with the second half of the rows split and displayed beside the first. The following code should do that. Let me know if this is what you had in mind...
(Grid[#1, Dividers -> All, Alignment -> {Left, Center},
ItemSize -> Automatic,
ItemStyle -> Directive[FontSize -> 14, Black, Italic, Bold],
Spacings -> {2, 1},
Background -> {None,
None, {{1, 3}, {1, 3}} -> #2}] &) ### {{laList[[;; 3, All]],
LightRed}, {laList[[4 ;;, All]], LightBlue}} // Row
As I understood your question, the coloring was there just to show us which portion you want to "move up". So:
showG[l_List] :=
Grid[Join[
l[[ ;; IntegerPart[Length#l/2]]],
l[[IntegerPart[Length#l/2] + 1 ;;]]
, 2], Frame -> All];
showG[laList]
Edit
Or more to #Mr.'s taste:
showG[l_List] :=
Grid[Join[l[[ ;; #]], l[[# + 1 ;;]], 2], Frame -> All] &# Floor[Length#l/2];
Here is how I would do it. Starting with your laList as defined in the question:
laList2 = ArrayFlatten # {Partition[laList, 3]};
Grid[laList2,
Dividers -> All,
Alignment -> {Left, Center},
ItemSize -> Automatic,
ItemStyle -> Directive[FontSize -> 14, Black, Italic, Bold],
Spacings -> {2, 1},
Background -> {None, None, {
{{1, 3}, {1, 3}} -> LightRed,
{{1, 3}, {4, 6}} -> LightBlue
} }
]
Please notice:
the value 3 within Partition will need to be adjusted according to your list.
the region specification for the LightBlue area was reversed.
A variation of yoda's code I like is:
subgrid= Grid[#1,
Dividers -> All,
Alignment -> {Left, Center},
ItemSize -> Automatic,
ItemStyle -> Directive[FontSize -> 14, Black, Italic, Bold],
Spacings -> {2, 1},
Background-> #2] &;
MapThread[subgrid, {Partition[laList, 3], {LightRed, LightBlue}}] //Row
Also, with this method you can partition a list that does not divide evenly:
MapThread[subgrid, {
Partition[laList, 4, 4, 1, {}],
{LightRed, LightBlue}
}] //Row

Resources