Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"


0 (0 Likes / 0 Dislikes)
Hello, and welcome to the Wolfram Language tutorials—programming My name is Harry Calkins. I have been with Wolfram Research for almost 20 years now. Most of that time has been spent teaching people how to use Mathematica and what we now call the Wolfram Language. I’m going to guiding you through this session here on creating a function where we will be able to feed in a data set and an image and we will be able to superimpose our bar chart on the image. The advantage here is that if I want to show temperatures in Chicago in a travel pamphlet or in something of that sort for the period of a year, I could take a nice image that is representative of the city, create the bar chart and use that same image as the background as we will see here. This will be a single function and it will take advantage of the built-in features and options of the Wolfram Language. We are going to create a function called the “PictorialBarChart”. The goal would be to produce a bar chart similar to what we see here. We have the image of Millennium Park in the city of Chicago, we have the months clearly laid out down here, we have the temperatures in degrees Fahrenheit indicated on the vertical scale and we see that the image is intense up to the height of the temperatures. But if you look carefully, you can see that the image is completed in the background so we have created a relatively quiet, blurred image in the background and an intense image in the foreground, which conveys the information that we are interested in communicating. So how would we approach a problem like this? Well the best way to program in any language and that includes the Wolfram Language is to sit down and ask yourself, “what do you want to accomplish?” and “what do I need to have to make that work?” So need to have the input, syntax for the function, how are we going to present the thing to the user; we need to figure out in terms of actually working with the function how many bars we need to create, how big is the image that we have fed into it, we need take a look and see if there are any special options we might want to use and if there are how are we going to set those values and pass them into the function. We’re going to construct the bars. There’s going to be two sets— one set for the actual temperature bars and one for the background. But they need to be constructed in such a way that when they get superimposed, one on top of the other, that the image is continuous. We need to construct the foreground and background image. We need to create the user function and then we’re going to create some messages. So let’s get started. So here is going to be our first definition; we’re going to make bars. Now this is always a good way to do things in the Wolfram Language because what we’re going to do is make a small function that performs a particular task and we’ll use that in our final function. So here we’re going to feed in the image and the count—the number of bars that we need— and so we’re going to image partition this as would be expected. Another thing that we want to do is provide a method of introducing opacity and blur into our image so that we can create a background image that doesn’t overpower the bars and that can be done by using an alpha channel, put in the image, put in the opacity that we would like, and then put in a blur factor. The opacity and blur will be arguments to this function so that users can set their own levels wherever it is necessary to do so. Now let’s construct the layers. Well the first thing we’re going to do is realize that we can make more than one function with the same name—more than one version of the same function. So we’re going to create a background function, we’re feeding in the data, the bars, the maximum value of all of the bars, and then we’re going to put in “BG” for background and then we’re going to put in options where we’re going to take the “OptionsPattern” for pictorial bar chart. And the bar chart that this is going to create is going to be a bar chart with a tool tip, with a tool tip delay of infinity to keep the tool tip from popping up, the chart elements are going to be bars, there’s an option for the bar spacing in the bar chart so we’ll set that to zero—automatic will go to zero— and we’re going to put in filter rules for bringing in the options that work in BarChart, not in our function, but in the actual Mathematica or Wolfram Language function. Similarly, we will do the same thing for the foreground. So we change the “BG” to “FG” to represent foreground and what’s going to happen is that we’re going to now use the heights of the bars as the values of the data set. And then we’re going to set the options for this and this is a quick and easy way to do that. We’re going to join the special options that we are going to use and process the background—that is the opacity and blur— and we are going to use the values for the bars spacing as part of the plot so we’ll make sure that we have that in here. We need to make sure that the bar spacing is changed from the default value of the bar chart. So here is our function— “PictorialBarChart”— we put in the data list, an image, and then we put in the options. And we’re going to create a number of localized variables here using the scoping function, “Block”. We can see here that these are all going to be localized values. We choose the maximum and multiply it by 1.02 and then we’re going to process our image here. We’re going to create the background for the thing. We’re going to create the foreground. We’re going to make the layers here and then we’re going to show the two things together. So that’s what happens in our function. Notice that we have used the functions we defined previously in the process—“makeBars”, “makeBars”, “makeLayer”, “makeLayer” and so forth. So now that we have written our function, the next thing that we want to do is tell users how to use this. So, I write a usage message and I put in as the first part of the usage message a template because the program is automatically set up to recognize that as a template. Now here, once I have done that, if I go in and then say “? PictorialBarChart”, it gives me a usage message of the sort that I am used to seeing when I use the program. And furthermore, if I start typing in— so if I do this— you’ll notice that right below is the auto-completion and by hitting that I automatically have picked up the auto-completion. So now if I don’t move my cursor and include the “Shift” key along with the “K” key or normally if you don’t have autocomplete set what you would do is “Control” and “K” and it would give you the auto-completion, toss in the “Shift” “Control” and “K”—“shift” “command” “k” if you’re on a Macintosh— and it gives me the usage message and if I accept that it should autocomplete here. So now we’re ready to test our function. We have the weather for Chicago in 2014, so here’s the image we are going to use and now we are going to check the weather data again to make sure that we have everything ready We have a time series here for this so we’re going to put in the weather values—that’s our dataset. We have to put values in here because we need to get the numbers, the way we have designed the bar chart here. It is possible to make it work with the weather data and the time series that comes back but for now we are going to just go in and get the values, which means we’re going to get the height values for the bars. Here’s the image and here are the chart labels and we’re going to place those below so we do this. We define two variations of the “MakeLayer” function one for the background and one for the foreground. Here we’re going to put in the data and a bars list, which is going to give us the bars as having created from the image as we saw a moment ago. And we’re going to create the background one first so we’re going to take the one where we have made the image fairly pale and blurred. And this is going to give us all of the bars the same heights so it is going to give us a full image. Alright, so now let’s test our function. It’s important to always test your function before you give it to somebody so that you make sure that all of the pieces are in place. So we begin by introducing the image, by the getting the weather data for Chicago, and now we are going to go ahead and evaluate the thing. And we see here that we get a nicely formed image with the bars here. Because we have set the tool tip delay in the background to be infinite, as we mouse over the bars in the front here what we get here are the actual temperatures as we go up and as we mouse over the area in the background we do not see anything— it’s only when we actually have our tool tip over one of bars. Well, we use this to construct this function so we should probably try it with a different image. If you want to see actually building this function you want to look at the tutorial on working with visualization. So here we have now weather data in San Francisco. Let’s go ahead and load that in; it takes a few seconds because we have to communicate with our website to get that information and bring it in. Now we have it. Now let’s go ahead and introduce an image of San Francisco— the famous cable cars running up and down the hills in San Francisco. Now let’s go ahead and put this in to our function and here we see the cable cars and the bars indicating the temperatures over the months and that little extra space that we introduced by using the max value in the function. And again, up here in the background area, we do not see the tool tips interrupting us. In the foreground it actually gives the Fahrenheit temperatures and so on. This, of course, can be made larger by introducing image size and so forth because all of the options that work with the bar chart continue to work with the chart that we have just created. Well, as a little challenge to you, you can download a copy of this notebook and you will notice if you play with this a little bit that there are some limitations. For example, when we got the weather data it gave it to us as a time series but we had to use time series with the argument values in order to get a list of the numerical values to create the bar chart. It is possible to set up the definition of the function so that it will do that automatically—you don’t need to do that. In other words, if the head of the expression, given as the first argument to this is actually “TimeSeries”, it will automatically replace that with time series values and then you can use the definition that we have already created. There are some other things we offer as a challenge to you— the possibility of setting this up with times series or temporal data. Another thing that we changed in here, we had the plot range in setting the size of the foreground plot and we can make various adjustments in that. We already have an example of doing that a little bit in there by multiplying by the 1.02 but one can extend this if one wanted to have a more dramatic spacing in between the bars and the background. Well I hope this has helped you understand how you might create a function of your own to do some special task. Thank you for joining us today.

Video Details

Duration: 15 minutes and 30 seconds
Language: English
License: Dotsub - Standard License
Genre: None
Views: 26
Posted by: wolfram on Apr 20, 2015


Caption and Translate

    Sign In/Register for Dotsub to translate this video.