Make Axis and ticks invisible in mathematica plot, but keep labels - wolfram-mathematica

I want to make a mathematica plot with no visible y-axis, but retaining the tick labels.
I've tried AxesStyle -> {Thickness[.001], Thickness[0]} with no effect, and setting the opacity to 0 also makes the tick labels fully transparent (and thus invisible).
Any help would be very much appreciated...

p = Plot[Sin[x], {x, 0, 6 Pi},
AxesStyle -> {Black, Opacity[0]},
TicksStyle -> Directive[Opacity[1], Black]]
ticks = AbsoluteOptions[p, Ticks];
ticks[[1, 2, 2]] = DeleteCases[ticks[[1, 2, 2]], {_, "", __}];
ticks[[1, 2, 2, All, 3]] = ConstantArray[{0, 0},
Length[ticks[[1, 2, 2, All, 3]]]];
ticks[[1, 2, 2, All, 2]] = Map[ToString,
ticks[[1, 2, 2, All, 2]]] /. a_String :>
If[StringTake[a, -1] == ".", a <> "0", a];
Plot[Sin[x], {x, 0, 6 Pi},
AxesStyle -> {Black, Directive[Opacity[0], Red]},
TicksStyle -> Directive[Opacity[1], Black],
Ticks -> {Automatic, ticks[[1, 2, 2]]}]
To get the exact original ticks you can use
Cases[Charting`FindTicks[{0, 1}, {0, 1}] ## PlotRange[p][[2]], {_, _}]
{{-1.,-1.0},{-0.5,-0.5},{0.,0},{0.5,0.5},{1.,1.0}}
as implemented here:
p = Plot[Sin[x], {x, 0, 6 Pi},
AxesStyle -> {Automatic, Opacity[0]},
TicksStyle -> Opacity[1]];
ticks = AbsoluteOptions[p, Ticks];
onestyledtick = ticks[[1, 2, 2, 1]];
labels = Cases[Charting`FindTicks[{0, 1}, {0, 1}] ##
PlotRange[p][[2]], {_, _}];
yticks = Map[Join[#, {{0, 0}},
Take[onestyledtick, -1]] &, labels];
Plot[Sin[x], {x, 0, 6 Pi},
AxesStyle -> {Automatic, Opacity[0]},
TicksStyle -> Opacity[1],
Ticks -> {Automatic, yticks}]

Related

PlotMarkers disappear when plotting exactly two polylines in Mathematica?

Not sure if this is a MMA bug or me doing something wrong.
Consider the following function:
plotTrace[points_] :=
ListPlot[points,
Joined -> True,
PlotMarkers -> Table[i, {i, Length#points}]]
now consider passing it values generated by RandomReal. Namely, consider
RandomReal[1, {nTraces, nPointsPerTrace, 2(*constant = nDimensions*)}].
If nTraces is 1, then PlotMarkers are displayed for all values of nPointsPerTrace that I tried:
Manipulate[
plotTrace[RandomReal[1, {1, nPointsPerTrace, 2}]],
{nPointsPerTrace, 1, 20, 1}]
If nTraces is 3 or greater, then plot markers are displayed for all values of nPointsPerTrace that I tried
Manipulate[plotTrace[RandomReal[1, {nTraces, nPointsPerTrace, 2}]],
{nTraces, 3, 20, 1}, {nPointsPerTrace, 1, 20, 1}]
But if nTraces is exactly 2, I don't see plot markers, no matter the value of nPointsPerTrace:
Manipulate[plotTrace[RandomReal[1, {2, nPointsPerTrace, 2}]],
{nPointsPerTrace, 1, 20, 1}]
Hints, clues, advice would be greatly appreciated!
It's treating PlotMarkers -> {1,2} as a marker and size, instead of as two markers:
In[137]:= ListPlot[{{1, 2, 3}, {4, 5, 6}}, PlotMarkers -> {1, 2}] // InputForm
Out[137]//InputForm=
Graphics[GraphicsComplex[{{1., 1.}, {2., 2.}, {3., 3.}, {1., 4.}, {2., 5.}, {3., 6.},
{1., 1.}, {2., 2.}, {3., 3.}, {1., 4.}, {2., 5.}, {3., 6.}},
{{{Hue[0.67, 0.6, 0.6], Inset[Style[1, FontSize -> 2], 7],
Inset[Style[1, FontSize -> 2], 8], Inset[Style[1, FontSize -> 2], 9]},
{Hue[0.9060679774997897, 0.6, 0.6], Inset[Style[1, FontSize -> 2], 10],
Inset[Style[1, FontSize -> 2], 11], Inset[Style[1, FontSize -> 2], 12]}, {}}}],
{AspectRatio -> GoldenRatio^(-1), Axes -> True, AxesOrigin -> {0, 0},
PlotRange -> {{0, 3.}, {0, 6.}}, PlotRangeClipping -> True,
PlotRangePadding -> {Scaled[0.02], Scaled[0.02]}}]
Things get even stranger when you try different things for PlotMarkers. The following does not display the plot markers, as in your examples above.
pts = RandomReal[1, {2, 10, 2}];
(* No markers *)
ListPlot[pts,
Joined -> True,
PlotMarkers -> {1, 2}
]
However, when you change the 2 to b, it does:
pts = RandomReal[1, {2, 10, 2}];
(* Has markers *)
ListPlot[pts,
Joined -> True,
PlotMarkers -> {1, b}
]
If you try to change the 1 to something, it doesn't work:
pts = RandomReal[1, {2, 10, 2}];
(* No markers *)
ListPlot[pts,
Joined -> True,
PlotMarkers -> {a, 2}
]
It does indeed sound like a bug, but I'm not sure if this is version dependent or some behavior that's not obvious.

Combining Plots in Mathematica is not giving the expected result

I'm trying to combine 3 functions graphed on a Plot[] and 1 function graphed on a ParametricPlot[]. My equations are as follows:
plota = Plot[{-2 x, -2 Sqrt[x], -2 x^(3/5)}, {x, 0, 1}, PlotLegend -> {"-2 x", "-2 \!\(\*SqrtBox[\(x\)]\)", "-2 \!\(\*SuperscriptBox[\(x\), \(3/5\)]\)"}]
plotb = ParametricPlot[{2.4056 (u - Sin[u]), 2.4056 (Cos[u] - 1)}, {u,0, 1.40138}, PlotLegend -> {"Problem 3"}]
Show[plota, plotb]
This is the image it gives:
As yoda said, PlotLegends is terrible. However, if you don't mind setting the plot styles manually and repeating them lateron, ShowLegend can help.
plota = Plot[{-2 x, -2 Sqrt[x], -2 x^(3/5)}, {x, 0, 1},
PlotStyle -> {{Red}, {Blue}, {Orange}}];
plotb = ParametricPlot[{2.4056 (u - Sin[u]), 2.4056 (Cos[u] - 1)}, {u, 0, 1.40138},
PlotStyle -> {{Black}}];
And now
ShowLegend[Show[plota, plotb],
{{{Graphics[{Red, Line[{{0, 0}, {1, 0}}]}], Label1},
{Graphics[{Blue, Line[{{0, 0}, {1, 0}}]}], Label2},
{Graphics[{Orange, Line[{{0, 0}, {1, 0}}]}], Label3},
{Graphics[{Black, Line[{{0, 0}, {1, 0}}]}], Label4}},
LegendSize -> {0.5, 0.5}, LegendPosition -> {0.5, -0.2}}]
which will give you this:
You can also write some simple functions to make this a little less cumbersome, if you deal with this problem often.
Well, the root cause of the error is the PlotLegends package, which is a terrible, buggy package. Removing that, Show combines them correctly:
plota = Plot[{-2 x, -2 Sqrt[x], -2 x^(3/5)}, {x, 0, 1}]
plotb = ParametricPlot[{2.4056 (u - Sin[u]), 2.4056 (Cos[u] - 1)}, {u,
0, 1.40138}]
Show[plota, plotb]
You can see Simon's solution here for ideas to label your different curves without using PlotLegends. This answer by James also demonstrates why PlotLegends has the reputation it has...
You can still salvage something with the PlotLegends package. Here's an example using ShowLegends that you can modify to your tastes
colors = {Red, Green, Blue, Pink};
legends = {-2 x, -2 Sqrt[x], -2 x^(3/5), "Problem 3"};
plota = Plot[{-2 x, -2 Sqrt[x], -2 x^(3/5)}, {x, 0, 1},
PlotStyle -> colors[[1 ;; 3]]];
plotb = ParametricPlot[{2.4056 (u - Sin[u]), 2.4056 (Cos[u] - 1)}, {u,
0, 1.40138}, PlotStyle -> colors[[4]]];
ShowLegend[
Show[plota,
plotb], {Table[{Graphics[{colors[[i]], Thick,
Line[{{0, 0}, {1, 0}}]}], legends[[i]]}, {i, 4}],
LegendPosition -> {0.4, -0.15}, LegendSpacing -> 0,
LegendShadow -> None, LegendSize -> 0.6}]
As the other answers pointed out, the culprit is PlotLegend. So, sometimes is useful to be able to roll your own plot legends:
plotStyle = {Red, Green, Blue};
labls = {"a", "b", "Let's go"};
f[i_, s_] := {Graphics[{plotStyle[[i]], Line[{{0, 0}, {1, 0}}]},
ImageSize -> {15, 10}], Style[labls[[i]], s]};
Plot[{Sin[x], Sin[2 x], Sin[3 x]}, {x, 0, 2 Pi},
PlotStyle -> plotStyle,
Epilog ->
Inset[Framed[Style#Column[{Grid[Table[f[i, 15], {i, 1, 3}]]}]],
Offset[{-2, -2}, Scaled[{1, 1}]], {Right, Top}],
PlotRangePadding -> 1
]

How to avoid asymptotes when I ListPlot a table of data using Mathematica?

I am plotting a table of data using ListPlot in Mathematica. I notice that there are a few asymptotes on the graph which I do not want it to be plotted (i.e. the straight lines between the curves). What should I do to remove the straight lines?
A method from Mark McClure's post here: How to annotate multiple datasets in ListPlots
t = Table[Tan[i], {i, -Pi, Pi, .01}];
plot = ListLinePlot[t];
DeleteCases[plot, Line[_?(Length[#] < 4 &)], Infinity]
Perhaps:
t = Table[Tan[i], {i, -Pi, Pi, .01}];
ListPlot[#, Joined -> True] & /# {t, t /. x_ /; Abs#x > 10 -> None}
Edit
More robust:
t = Table[Tan[i], {i, -Pi, Pi, .01}];
ao = AbsoluteOptions[ListPlot[t, Joined -> True],PlotRange]/. {_ -> {_,x_}} ->x;
ListPlot[t /. x_ /; (x < ao[[1]] || x > ao[[2]]) -> None, Joined -> True]
t = Table[Tan[i], {i, -Pi, Pi, .01}];
plot = ListLinePlot[t];
Using Position
Position[plot, Line[___], Infinity]
{{1, 1, 3, 2}, {1, 1, 3, 3}, {1, 1, 3, 4}, {1, 1, 3, 5}, {1, 1, 3, 6}}
Using Part:
plot[[1, 1, 3, 5 ;; 6]] = Sequence[]; Show[plot]

Plotting points in Mathematica

I am trying to plot a few points on the following picture in Mathematica:
ParametricPlot3D[
{{u, v, (Cos[u] + Cos[v])/3}, {u, -1, (Cos[u] + Cos[0])/3},
{5, v, (Cos[4] + Cos[v])/3}}, {u, -4, 4}, {v, 0, 8}, Axes -> False,
Boxed -> False, BoxRatios -> {8, 8, 1.5}]
(they should just look like dots on the surface)
What I was trying to do is enter the coordinates of the points manually on another graph using ListPointPlot3D, and then combine them using Show. But for some reason that isn't working. Suggestions?
Also, I would like to add small vectors tangent to the surface in the x directions for the points I have plotted, but I have no idea on how to do that, so suggestions would be very much appreciated!
Perhaps this will help you get started on a solution. It plots 3 random points on the surface. You can change the number of points by setting nPoints. I don't know how to plot tangents along x. But when you figure that out you can use Arrows, as suggested by #Verbeia.
nPoints = 3;
Show[ParametricPlot3D[{
{u, v, (Cos[u] + Cos[v])/3},
{u, -1, (Cos[u] + Cos[0])/3}, {5, v, (Cos[4] + Cos[v])/3}},
{u, -4, 4}, {v, 0, 8}, Axes -> False,
Boxed -> False, BoxRatios -> {8, 8, 1.5},
PlotStyle -> Directive[Opacity[0.5]]],
Graphics3D[{Red, PointSize[.025],
Point[Table[{u1 = RandomReal[{-3, 3}], v1 = RandomReal[{1, 7}],
(Cos[u1] + Cos[v1])/3}, {nPoints}]]}]]
Edit
The following dynamic variation makes use of #belisarius 's contribution:
Manipulate[
Show[ParametricPlot3D[{{u, v, (Cos[u] + Cos[v])/3} },
{u, -4, 4}, {v, 0, 8}, Axes -> False, Boxed -> False,
BoxRatios -> {8, 8, 1.5},
Mesh -> None,
ImageSize -> {400, 300},
PlotRange -> {{-4, 4}, {0, 8}},
PlotRangePadding -> {{0, 1.4}, {0, 0}},
PlotStyle -> Directive[Opacity[0.5]]],
Graphics3D[({Red, PointSize[.025],
Point#f[pt[[1, 1]], pt[[1, 2]]], Black,
Arrow[{f[pt[[1, 1]], pt[[1, 2]]],
f[pt[[1, 1]], pt[[1, 2]]] + D[f[t, pt[[1, 2]]], t] /.
t -> pt[[1, 1]]}]}]],
Grid[{{
LocatorPane[Dynamic[pt],
Dynamic[Graphics[{},
PlotRange -> {{-4, 4}, {0, 8}},
Frame -> True,
ImageSize -> 160,
FrameTicks -> {Range[-4, 4], Range[0, 8], None, None},
FrameLabel -> {"u", "v"},
GridLines -> {Range[-4, 4], Range[0, 8]},
GridLinesStyle -> Directive[LightGray]]],
{{-4, 0}, {4, 8}}]}}],
{{pt, {{1, 2}}}, ControlType -> None},
Initialization :> {f[u_, v_] := {u, v, (Cos[u] + Cos[v])/3};}]
For the Arrows
f[u_, v_] := {u, v, (Cos[u] + Cos[v])/3};
Show[ParametricPlot3D[{f[u, v]}, {u, -4, 4}, {v, 0, 8},
Axes -> False, Mesh -> None, Boxed -> False, BoxRatios -> {8, 8, 1.5},
PlotStyle -> Directive[Opacity[0.5]]],
Graphics3D#
Table[{Red, PointSize[.025], Point#f[u, v],
Black, Arrow[{f[u, v], f[u, v] + D[f[t, v], t] /. t -> u}]},
{u, -4, 4, 2}, {v, 0, 8, 2}]]
For getting the arrows in any direction a = { a1, a2 } instead of x, you may do:
Dot[{a1,a2}.#] & /# D[f[u, v], {{u, v}}]
(*
-> {a1, a2, -(1/3) a1 Sin[u] - 1/3 a2 Sin[v]}
*)
Edit
Both derivatives and normal:
f[u_, v_] := {u, v, (Cos[u] + Cos[v])/3};
Show[
Graphics3D#
Table[{Red, PointSize[.025], Point#f[u, v], Black, Arrowheads[.02],
Arrow[{f[u, v], f[u, v] + D[f[t, v], t] /. t -> u}],
Arrow[{f[u, v], f[u, v] + D[f[u, t], t] /. t -> v}],
Arrow[{f[u, v], f[u, v] +
Cross[D[f[t, v], t] /. t -> u,
D[f[u, t], t] /. t -> v]}]},
{u, -4, 4, 2}, {v, 0, 8, 2}],
ParametricPlot3D[{f[u, v]}, {u, -4, 4}, {v, 0, 8},
Axes -> False, Mesh -> 3, MeshStyle -> {{Opacity[0.1], LightBlue}},
Boxed -> False, BoxRatios -> {8, 8, 1.5},
PlotStyle -> Directive[Opacity[0.5]]]]
You can combine the plot with points using Graphics3D[listofpoints], where listofpoints is a T*3 matrix list, and the arrows using constructs like Graphics3D[Arrow[{{1, 1, -1}, {2, 2, 0}, {3, 3, -1}, {4, 4, 0}}]]. If they are all Graphics3D objects, you should be able to combine them with Show.
Sorry, I am not near a Mathematica installation to provide you with an example just now.

Labeling vertices of a polygon in Mathematica

Given a set of points in the plane T={a1,a2,...,an} then Graphics[Polygon[T]] will plot the polygon generated by the points. How can I add labels to the polygon's vertices? Have merely the index as a label would be better then nothing. Any ideas?
pts = {{1, 0}, {0, Sqrt[3]}, {-1, 0}};
Graphics[
{{LightGray, Polygon[pts]},
{pts /. {x_, y_} :> Text[Style[{x, y}, Red], {x, y}]}}
]
To add point also
pts = {{1, 0}, {0, Sqrt[3]}, {-1, 0}};
Graphics[
{{LightGray, Polygon[pts]},
{pts /. {x_, y_} :> Text[Style[{x, y}, Red], {x, y}, {0, -1}]},
{pts /. {x_, y_} :> {Blue, PointSize[0.02], Point[{x, y}]}}
}
]
update:
Use the index:
pts = {{1, 0}, {0, Sqrt[3]}, {-1, 0}};
Graphics[
{{LightGray, Polygon[pts]},
{pts /. {x_, y_} :>
Text[Style[Position[pts, {x, y}], Red], {x, y}, {0, -1}]}
}
]
Nasser's version (update) uses pattern matching. This one uses functional programming. MapIndexed gives you both the coordinates and their index without the need for Position to find it.
pts = {{1, 0}, {0, Sqrt[3]}, {-1, 0}};
Graphics[
{
{LightGray, Polygon[pts]},
MapIndexed[Text[Style[#2[[1]], Red], #1, {0, -1}] &, pts]
}
]
or, if you don't like MapIndexed, here's a version with Apply (at level 1, infix notation ###).
pts = {{1, 0}, {0, Sqrt[3]}, {-1, 0}};
idx = Range[Length[pts]];
Graphics[
{
{LightGray, Polygon[pts]},
Text[Style[#2, Red], #1, {0, -1}] & ### ({pts, idx}\[Transpose])
}
]
This can be expanded to arbitrary labels as follows:
pts = {{1, 0}, {0, Sqrt[3]}, {-1, 0}};
idx = {"One", "Two", "Three"};
Graphics[
{
{LightGray, Polygon[pts]},
Text[Style[#2, Red], #1, {0, -1}] & ### ({pts, idx}\[Transpose])
}
]
You can leverage the options of GraphPlot for this. Example:
c = RandomReal[1, {3, 2}]
g = GraphPlot[c, VertexLabeling -> True, VertexCoordinateRules -> c];
Graphics[{Polygon#c, g[[1]]}]
This way you can also make use of VertexLabeling -> Tooltip, or VertexRenderingFunction if you want to. If you do not want the edges overlaid, you may add EdgeRenderingFunction -> None to the GraphPlot function. Example:
c = RandomReal[1, {3, 2}]
g = GraphPlot[c, VertexLabeling -> All, VertexCoordinateRules -> c,
EdgeRenderingFunction -> None,
VertexRenderingFunction -> ({White, EdgeForm[Black], Disk[#, .02],
Black, Text[#2, #1]} &)];
Graphics[{Brown, Polygon#c, g[[1]]}]

Resources