Extending ListPointPlot3D with a 3rd variable? - wolfram-mathematica

I am trying to extend a function like this: (a 4x4 square)
ListPointPlot3D[Table[{x, y, 0}, {x, 0, 4, 1}, {y, 0, 4, 1}]]
into something like this: (a 4x4x4 cube)
ListPointPlot3D[Table[{x, y, z}, {x, 0, 4, 1}, {y, 0, 4, 1}, {z, 0, 4, 1}]]
by adding a 3rd dimension.
However, the dimensions of the latter seem to be incorrect. It seems to form a 2x2 matrix of 3d points rather than a list.
Any ideas how to fix this?

If you look a bit more closely you'll see that the expression
Table[{x, y, z}, {x, 0, 4, 1}, {y, 0, 4, 1}, {z, 0, 4, 1}]
returns a structure with 5x5x5 triplets. That is exactly what the expression is supposed to return. You can see this if you apply the Dimensions[] function to the returned structure.
There are several ways to turn the table into a list of 125 triplets, one is to use Flatten like this
Flatten[Table[{x, y, z}, {x, 0, 4, 1}, {y, 0, 4, 1}, {z, 0, 4, 1}], 2]
Or you could simply generate your list of triplets directly; for your example one alternative would be
Tuples[Range[0, 4], 3]

Related

Generating Manipulate 'sliders' on the fly

Mathematica's Manipulate function takes as its final arguments separate lists of the parameters you want sliders for along with their value ranges. But why not a list of lists?
This way I could I easily generate all the sliders for this big list of transformation rules that I have, e.g.:
parms = {a -> 2, b -> 4, c -> 5};
Table[{{parms[[i]][[1]], parms[[i]][[2]]}, 0, 10}, {i, 1,Length[parms]}]
{{{a, 2}, 0, 10}, {{b, 4}, 0, 10}, {{c, 5}, 0, 10}}
What I would like to have, however, is:
{{a, 2}, 0, 10}, {{b, 4}, 0, 10}, {{c, 5}, 0, 10}
This I'm copy pasting now between cells, which is rather messy. I'm sure there's a better way to do this. Please help, thanks!
Please see this and this on similar questions.
What you need is Sequence## to get the list of lists to be treated as your desired output when used as input.
Perhaps something like:
ClearAll[a, b, c];
parms = {a -> 2, b -> 4, c -> 5};
With[{values = Table[parms[[i]][[1]], {i, 1, Length[parms]}],
controls = Sequence ##
Table[{{parms[[i]][[1]], parms[[i]][[2]],
Style[ToString[parms[[i]][[1]]], Red, Bold]}, 0, 10}, {i, 1,
Length[parms]}]},
Manipulate[values, controls]]
which gives

The plot curve goes under the X-axis - fix?

Why when I input the command Plot[E^(-x), {x, 0, 2}] in Mathematica 8 the point (2, e^(-2)) goes under the X-axis? How can I fix that?
Plot[E^(-x), {x, 0, 2}, AxesOrigin -> {0, 0}]
Heike's answer is just fine, but here's an alternative:
Plot[E^(-x), {x, 0, 2}, PlotRange -> {0, Automatic}]

Plot of bars with height data in Mathematica

I have a matrix {{2, 1, 2, 2, 1}, {1, 3, 0, 1, 2}, {3, 3, 0, 3, 1}, {1, 1, 2, 1, 1}}, and I want to generate a 3d plot such as there are a total of 4*5=20 bars.
There is a bar of height 2 based at the little square (1, 1) (i.e. the square formed on the x-y plane by the points {{0,0},{0,1},{1,1},{1,0}}),
another bar of height 1 based at the little square (1,2) (i.e. the square formed on the x-y plane by the points {{0,1},{0,2},{1,2},{1,0}}),
...
another bar of height 3 based at the little square (2,2) (i.e. the square formed on the x-y plane by the points {{1,1},{2,1},{2,2},{1,2}})
...
and another bar of height 1 based at the little square (4,5) (i.e. the square formed on the x-y plane by the points {{3,4},{4,4},{4,5},{3,5}})
I cannot find an easy way to do this. Thanks a lot for your help!
What you want is BarChart3D.
Note, this function exists in two incarnations:
There is a BarChart3D in the BarCharts package. This function does what you want out of the box, but is deprecated in Mathematica 7+.
Then there's a BarChart3D in the main namespace (Mathematica 7+ only), which can do what you want as well, but needs to be passed the option ChartStyle -> "Grid" to display the result you want.
Here is some example code for both of these:
Mathematica 6 and prior
<<BarCharts`;
data = {{2, 1, 2, 2, 1}, {1, 3, 0, 1, 2}, {3, 3, 0, 3, 1}, {1, 1, 2, 1, 1}};
BarChart3D[data]
Mathematica 7 and later
data = {{2, 1, 2, 2, 1}, {1, 3, 0, 1, 2}, {3, 3, 0, 3, 1}, {1, 1, 2, 1, 1}};
BarChart3D[data, ChartLayout -> "Grid"]
data = {{2,1,2,2,1}, {1,3,0,1,2}, {3,3,0,3,1}, {1,1,2,1,1}};
BarChart3D[data, ChartLayout -> "Grid", BarSpacing -> 0]
Edit
Updating after wiki-specifying the question :
BarChart3D[data, ChartLayout -> "Grid", BarSpacing -> {0, 0},
LabelingFunction -> (Row[{#1, Reverse[#2 - 1], Reverse[#2]}] &),
AxesLabel -> {"x", "y", "z"}]
Here the both x and y-spacings vanish. Setting the cursor on a given bar you get z{x_min,y_min}{x_max,y_max}, on the top-red i.e. : 2{4,1}{5,2}

Mathematica: Removing graphics primitives

Given that g is a graphics object with primitives such as Lines and Polygons, how do you remove some of them? To add more primitives to an existing graphics object we can use Show, for instance: Show[g, g2] where g2 is another graphics object with other primitives. But how do you remove unwanted primitive objects? Take a look at the following
ListPlot3D[{{0, 0, 1}, {1, 0, 0}, {0, 1, 0}, {1, 1, 0}}, Mesh -> {1, 1}]
Now, for the input form:
InputForm[
ListPlot3D[{{0, 0, 1}, {1, 0, 0}, {0, 1, 0}, {1, 1, 0}}, Mesh -> {1, 1}]
]
To create a wire frame from this object all we have to do is remove the polygons. As an extra we can also remove the vertex normals since they don't contribute to the wireframe.
Notice that to make a wireframe we can simply set PlotStyle -> None as an option in ListPlot3D. This gets rid of the Polygons but doesn't remove the VertexNormals.
To clarify the question. Given that
g = ListPlot3D[{{0, 0, 1}, {1, 0, 0}, {0, 1, 0}, {1, 1, 0}}, Mesh -> {1, 1}]
How do you remove some of the of the graphics primitives from g and how do you remove some of the options, i.e. VertexNormals? Note: option VertexNormals is an option of GraphicsComplex.
If this is not possible then maybe the next question would be, how do you obtain the data used to generate g to generate a new graphics object with some of the data obtained from g.
One way is to use transformation rules. Given your
im = ListPlot3D[{{0, 0, 1}, {1, 0, 0}, {0, 1, 0}, {1, 1, 0}}, Mesh -> {1, 1}]
You can do
newim = im /. {_Polygon :> Sequence[], (VertexNormals -> _) :> Sequence[]}
or, more compactly using Alternatives:
newim = im /. _Polygon | (VertexNormals -> _) :> Sequence[]
You could also use DeleteCases to get a similar effect:
newim = DeleteCases[im, (_Polygon | (VertexNormals -> _)), Infinity]

Setting all points of a given ListPlot with a given color in Mathematica

How can I make it such that plotting the following function
ListPointPlot3D[points, PlotStyle -> PointSize[0.05]];
the points I see are green or yellow, for instance, instead of the typical dark blue ones?
Thanks
Use Directive to combine styles, ie
ListPointPlot3D[points, PlotStyle -> Directive[{PointSize[0.05], Green}]]
Edit I give you below two possible solutions in a context related to your previous question. Nevertheless, please note that #Yaroslav's code is much better.
f[x_, y_] := x^2 + y^2;
t = Graphics3D[{PointSize[Large], Red, Point#
Flatten[Table[{x, y, f[x, y]}, {x, 0, 10, 1}, {y, 1, 2, 1}], 1]}];
b = Plot3D[f[x, y], {x, -10, 10}, {y, -10, 10},
ColorFunction -> "MintColors"];
Show[{b, t}]
Or
f[x_, y_] := x^2 + y^2;
points = Flatten[Table[{x, y, f[x, y]}, {x, 0, 10, 1}, {y, 1, 2, 1}],
1];
a = ListPointPlot3D[points,
PlotStyle -> Table[{Red, PointSize[0.05]}, {Length#t}]];
b = Plot3D[f[x, y], {x, -10, 10}, {y, -10, 10},
ColorFunction -> "MintColors"];
Show[{b, a}]
Sometimes I find the following approach useful, as it allows me to
manipulate the plot symbol (PlotMarkers does not seem to work with ListPointPlot3D,
at least in Mathematica 7) [originally suggested by Jens-Peer Kuska]:
ListPointPlot3D[{{1,1,1},{2,2,2},{3,3,3}}]/.Point[xy_]:>(Style[Text["\[FilledUpTriangle]",#],Red,FontSize-> 20]&/#xy)

Resources