Plotting a step function in Mathematica - wolfram-mathematica

I'm trying to plot a couple of UnitStep functions, but for some
reason, Mathematica won't plot the whole function - Just the top (it doesn't look like a step, more like a line).
How do I tell mathematica to plot it all?

An alternative to setting Exclusions -> None is to set ExclusionsStyle -> {style} if you want to draw the line segments connecting the discontinuities in a different style from the rest of the curve. For example
Plot[Round[n], {n, 0, 5}, ExclusionsStyle -> {Dashed}]

Related

Why is the plot changing when I change the range?

cdf[s_] := Piecewise[{{1 - (10/s), s > 10}}]
Plot[cdf[s], {s, 0, 20}]
And it plots correctly.
correct graph
When I modify the range to, say
Plot[cdf[s], {s, 0, 200}]
It plots incorrectly.
incorrect graph
Is this expected behavior? Am I misunderstanding something?
I played around with different ranges. The graph presents correctly up to around 180, then it starts displaying incorrectly.
The plot changes because the PlotRange changes.
Try to add, e.g., PlotRange -> All
See the documentation at https://reference.wolfram.com/language/ref/PlotRange.html

get points of listplot in mathematica

I have a few graphs which took quite some time for calculations. I would like to extract the data of those plots instead of recalculing a list. I found the command pts=Cases[(*I paste graph here*),x_Line:>First#x,Infinity];, which works fine for my line plots but not for listplots. How can I do the same for listplots?
It is essentially the same except for a scatter plot you use point:
Cases[(*pasted graphic*), x_Point :> First#x, Infinity]
It may be instructive to do
(*paste*)//FullForm
Of course you really ought to just save the data before generating the plot..

Assembling multiple Graphics objects

Consider the following simple example:
f[x_] = Sin[5 x] + Sin[3 x];
p1 = PolarPlot[f[x], {x, 0, 2 Pi}, ImageSize -> 50 {5, 2.5},
Frame -> True]
p2 = PolarPlot[f'[x], {x, 0, 2 Pi}, ImageSize -> 50 {5, 3},
Frame -> True]
which produces the following two figures
Now I'd like to align the two of them, one on top of each other, without any additional gaps. Doing FullGraphics#GraphicsColumn[{p1, p2}, Frame -> All], returns an object with additional white space around the shorter figure (I've turned the frames on in all the objects to illustrate this)
I couldn't find a way to force each of the column "cells" to fit vertically to the image size (NOTE: I'm aware that in my example, there's additional horizontal whitespace in the second figure, and that is intentional. I'm only trying to fit it snugly vertically). I tried playing around with the Spacings option, but that's still not satisfactory. So, my first question would be: "Is there a way I can do this using GraphicsGrid or GraphicsColumn or by any other means?"
Another way would be to use ImageAssemble like so:
ImageAssemble[Map[FullGraphics, {{p1}, {p2}}, {2}]]
which does what I want, but gives a rasterized image (the following is a screen grab, as the saved file had much poorer resolution).
My actual figures are neat vector plots and I don't even want to think about rasterizing them, no matter how high the resolution. So, my second question would be: "Is there a way to get a vector graphics output from ImageAssemble?" If it helps, I'm using Mma7 and saving my graphics as Export["filename.pdf",expr].
I know that this is possible using LevelScheme for the example shown. However, not all plotting functions are compatible with LevelScheme and will not work in all cases.
Does the result have to be a graphic? If not you can just use a regular column:
Column[{p1, p2}, Spacings -> 0]
Building on Brett's answer, this might be what you want:
FullGraphics#GraphicsGrid[{{p1}, {p2}}, Spacings -> 0]
Method 1:
Use ImageAssemble[{image1},{image2}....].
Can use ImagePad to add some space around each image:
ImageAssemble[ {ImagePad[image1,m1]}, {ImagePad[image2,m2]},...]
Note - all images must be of same size (Use ImageDimensions, and appropriate choice of m1, m2...).
Method 2:
define an array of images of dimension rows x cols, say "myphotos"
ImageAssemble[myphotos]

Two quick Mathematica questions

How do I remove the numbers on the x-axis only not the y-axis?
Is it possible to shift the y-axis without shifting the functions? What I mean is, instead of having the y-axis at x = 0, could I have it at x = -5?
With most options controlling axes, you can use a list of two values to specify different behavior for the two axes. How are you currently removing the numbers? The only way I can think of off the top of my head is to manually specify tick locations, without labels, something like this:
Ticks -> {{{0, ""}, {1, ""}, {2, ""}}, Automatic}
Otherwise you'd have to start munging around with the graphics output, I guess? In any case, whatever you do, you can use a two-element list like that to apply different options to each dimension.
To relocate the axes, you can use the option AxesOrigin; in your example, you'd use AxesOrigin->{-5,0}.
The tick marks are controlled by either Ticks, or FrameTicks if Frame -> True. Note, plots like ContourPlot and DensityPlot have a frame by default (i.e. Frame -> True). The specification for Ticks is
Ticks -> {<x ticks>, <y ticks>, <z ticks>}
where the z ticks are only present for 3D plots. The FrameTicks specification is
FrameTicks -> { {left, right}, {bottom, top} }
Both Ticks and FrameTicks accept both Automatic and None to use the default algorithms or have no ticks, respectively. So, to eliminate only the ticks on the x-axis you do
Ticks -> { None, Automatic }
and for frames
FrameTicks -> { {Automatic, Automatic}, {None, None} }
If you want to have more control of the exact placement of the ticks, then #Jefromi is correct, you'll need to specify a list. The Ticks and FrameTicks documentation are worth the read to get a better feel of how to do this. Be aware, though, that doing anything more complex than listing a few points is something of a black art in Mathematica, and leads to a lot of frustration.
As to your second question, you use AxesOrigin -> {-5, 0} as #Jefromi pointed out.

Possible to put equation's expression near its graphic representation?

Is it possible that when I Plot a function in Mathematica, it will automatically put near it it's equation(i.e. y = 2x) or even some other text?
At first glance I don't find any option for it, but if there is one I'd like to know.
Thanks
Using Mathematica 6 or higher, I often use Tooltip to help me identify plot curves:
Plot[Tooltip[Sin[x]], {x, 0, 8 Pi}]
Alas, this is only useful when using the graph interactively since you must hover the mouse cursor over the curve. It doesn't work so well on paper or on a static image.
You can use the Epilog option to manually place some text on the plot, as in this example:
Plot[
Sin[x], {x, 0, 8 Pi},
Epilog -> Text["My Text", Offset[{32, 0}, {14, Sin[14]}]]
]
Tweak the arguments of Offset to taste.
This works if you do not mind manual placement. Automatic placement poses some challenges, depending upon the kinds of functions that you wish to plot. But if you know something of the general characteristics of the functions of interest, you could write a function that calculates nice looking values for the Offset arguments. For example, if I knew I was going to plot lots of exponential decline functions, I might define something like the function myPlot in this example:
SetAttributes[myPlot, HoldAll]
myPlot[function_, {var_, min_, max_}] :=
Plot[
function, {var, min, max},
Epilog -> Text[function, Offset[{40, 0}, {var, function} /. var -> min + (max - min)/20]],
PlotRange -> All, AxesOrigin -> {0, 0}
]
... where the arguments to Offset are computed automatically using some arbitrary constants that work reasonably well for these kinds of plots:
Manipulate[
myPlot[1000 E^(-d t), {t, 0, 100}, "My Label"],
{d, 0.01, .2}
]
Since all of these options are programmable, the sky's limit as to how much sophistication you could code up for the label placement. Of course, such programming drifts farther and farther away from the ideal of a built-in option to Plot that just magically drops on some text next to the function. Mathematica 8 or 9 maybe :)
One way to do this, which automatically associates the expression with the style used to plot it, is to use the PlotLegends standard add-on package. The output doesn't look very good by default; I recommend setting the LegendShadow -> None option and using Style on the expressions you stick in the legend to make them look better. Also, loading the package inflicts some funny redefinitions on Plot and related functions which can break some other things if you're not careful.
"Near its equation" is the problem. This isn't an easy problem to solve, and it becomes somewhat impossible when you start getting "busy" graphs with overlapping plots and so on.
I don't have a good example to show, but usually I'll define a "labelling function" that takes the same input as the function being plotted, which places a dot on the graph and writes some text nearby. This has the advantage of being able to easily vary the location of the text but still have it tied to the function.

Resources