9 | Interactive Manipulation |
So far, we’ve been using the Wolfram Language in a question-answer way: we type input, and the language responds with output. But the language also lets you set up user interfaces where one can continually manipulate a variable. The function Manipulate works a lot like Table, except that instead of producing a list of results, it gives a slider to interactively choose the value you want.
The slider lets you pick values of n between 1 and 5 (in steps of 1):
Manipulate[Table[Orange, n], {n, 1, 5, 1}]
Here’s the whole sequence of possible results:
Table[Table[Orange, n], {n, 1, 5, 1}]
Here’s a similar interface for displaying a column of powers of a number:
Manipulate[Column[{n, n^2, n^3}], {n, 1, 10, 1}]
And here’s the list of possible results in this case:
Table[Column[{n, n^2, n^3}], {n, 1, 10, 1}]
Unlike Table, Manipulate isn’t limited to a fixed set of possible values. If you simply omit the step size in Manipulate, it’ll assume you want any possible value in the range, not limited to whole numbers.
Without the step size, Manipulate allows any value:
Manipulate[Column[{n, n^2, n^3}], {n, 1, 10}]
It’s very common to have graphics that you want to adjust interactively.
A bar chart that changes as you move the slider:
Manipulate[BarChart[{1, a, 4, 2*a, 4, 3*a, 1}], {a, 0, 5}]
A pie chart of the same list:
Manipulate[PieChart[{1, a, 4, 2*a, 4, 3*a, 1}], {a, 0, 5}]
Manipulate lets you set up any number of controls. You just give the information for each variable in turn, one after another.
Build an interface to vary the number of sides, and the hue, of a polygon:
Manipulate[
Graphics[Style[RegularPolygon[n], Hue[h]]], {n, 5, 20, 1}, {h, 0, 1}]
There are many ways to specify controls for Manipulate. If you give a list of possible values, you’ll get a chooser or menu.
Build an interface that lets you choose between three colors:
Manipulate[
Graphics[
Style[RegularPolygon[5], color]], {color, {Red, Yellow, Blue}}]
Manipulate[
Style[value, color, size], {value, 1, 20,
1}, {color, {Black, Red, Purple}}, {size, Range[12, 96, 12]}]
Manipulate[anything,{n,0,10,1}] | manipulate anything with n varying in steps of 1 | |
Manipulate[anything,{x,0,10}] | manipulate anything with x varying continuously |
9.5Make a Manipulate to show a disk with red, green and blue color components varying from 0 to 1. »
9.8Make a Manipulate that shows a list of a variable number of hexagons (between 1 and 10), and with variable hues. »
9.9Make a Manipulate that lets you show a regular polygon with between 5 and 20 sides, in red, yellow or blue. »
9.10Make a Manipulate that shows a pie chart with a number of equal segments varying from 1 to 10. »
9.13Make a Manipulate to display a column of integer powers with bases from 1 to 25 and exponents from 1 to 10. »
9.14Make a Manipulate of a number line of values of x^n for integer x from 1 to 10, with n varying from 0 to 5. »
+9.1Make a Manipulate to plot numbers from 1 to 100 raised to powers that can vary between −1 and +1. »
+9.3Make a Manipulate to show a bar chart with 4 bars, each with a height that can be between 0 and 10. »
Does Manipulate work the same on web, mobile and desktop?
Ultimately yes. But it can be significantly slower on the web and on some mobile systems, because every time you move a slider it has to communicate over the internet with a server to find out what to do. On desktop and some mobile systems, everything is happening right there, inside your computer or other device—so it’s very fast.
Can I make a standalone app out of a Manipulate?
Yes. To make a web app, for example, you just need to use CloudPublish. We’ll talk about this in Section 36.
Can I use random numbers in Manipulate?
Yes, but unless you “seed” them with SeedRandom, the random numbers will be different every time you move a slider.
- Manipulate supports pretty much all standard user interface control types (checkboxes, menus, input fields, color pickers, etc.).
- Sliders in Manipulate often provide a button which opens out to additional controls—including animation, single stepping and numerical value display.
- Many controls are rendered differently on mouse and touch devices, and some controls only work on one or the other.
- If you’re running natively on your computer, devices like gamepads should immediately work with Manipulate. You can specify which controls should be hooked to what. ControllerInformation[ ] gives information on all your controllers.
The Wolfram Demonstrations Project: more than 12,000 interactive Demonstrations created with Manipulate