 In this first video I want to talk to you about bar charts. I'm here in my Jupiter notebook inside of Microsoft Edge, my web browser. I have navigated to the folder that contains the files that I want to work with and in my case it's in my OneDrive file. I've created a folder called YouTube and inside of that data visualization with a plot key for Python. I have a style sheet, a cascading style sheet that will be uploaded to GitHub so that you can use that as well. I have a Python cheat sheet, a PDF which I'll also download. You can get that from the plot key website. There's a style template which I just always save just to open up and it's got the bare bones of which I want to work with. But here's the file we're looking for, the simple bar chart. Let's open that up and there you go. Everything is ready. You'll see the first cell that I have here is just the import of the cascading style sheet. It's ipython.core.display and from that I import HTML. I reference the file, this cascading style sheet file and that lives in the same folder as this notebook and I just use the HTML function there, open the CSS file in read mode and we read it and if I execute that you'd see that I have this new style. I have my h1 color of the text here being blue, the h2 being this nice orange, etc. It just styles the notebook formula. So let's have a look at the simple bar chart. Now first of all I want to set up my plotly library and what we're going to use here is called the notebook mode so I don't want to have these files uploaded to the plotly website to live in the cloud. I just want to use them locally so I'm going to use the notebook mode. So from plotly.offline I'm going to import iplot and init notebook mode as you can see here and I'm just going to call this function init notebook mode. So if we run this that'll just import this iplot and initializing this notebook mode and it'll initialize this notebook mode. So what do we actually want to import from plotly? Well we're going to start off with these high level charts so bar charts, charts, scatter charts, etc. These are high level and I can just use them as is. So I can say import plotly.graph objects so graph underscore objs as go. Let's do that. So let's start off with a very bare bones chart. The first thing we want to set up is just what is called the trace. We call it trace here as just a computer variable but that just creates on on top of the figure that we are creating this blank figure we're going to put on these elements and it's just a norm or to call them trace. So go remember that's my graph objects dot bar. So immediately the arguments here are specifically for a bar chart that's why we call this high level chart. I didn't have to to design every single little element most of it's already designed inside of this bar object and I just have to pass some arguments to it and I'm going to pass x arguments for the x-axis and y arguments for the y-axis. So what is a bar chart for? A bar chart is for categorical variables. So anything that is not necessarily a number although numbers can also be categorical variables but they are not numerical and are such as the difference between them are not set and standard. So I could call January, February, March 1, 2 and 3 for the first, second and third months of the year but that does not make them numerical variables they are they are still in that instance categorical variables. So bar charts are for categorical variables. So my x-axis here I'm going to have the categories January, February and March and on my y-axis I'm going to have numerical values. So how many things whatever the situation might be in my instance here I'm going to use you'll see later on I'm going to call them sales. So in January there were 10 unit sales in February 11 and in March 14. So those are numerical values on the y-axis categorical variables on the x-axis. Now I'm going to introduce a second computer variable I'm going to call it data and I'm going to pass into that a list so it goes inside of square brackets. I'm going to pass a list of all the traces these are these elements that go on top of my blank figure and in this case I only have one trace but I've got to put that inside of the square brackets I'm referencing trace here as a list element an element inside of this list. Third computer variable that I use here is this fig now these are just standard they used implotically all the time so might as well stick with those and I'm going to call another graph object called a figure that's a blank canvas a blank figure and figure with a capital F as you can see there it takes a couple of arguments in this instance we're only going to use one argument and that's the data argument as you can see here and we're going to set that equal to data which is this data which contains a list of trace and the trace is a bar so it just builds one thing on top of the other so inside of this data that I'm referencing there's a list and the first element in the list is trace and in trace is this element called a bar chart. Finally I'm going to call iplot remember which we imported up here I plot that's for plotting directly in the notebook not using online mode so not not going to the cloud and I'm going to plot this figure fig fig that I've created let's hold down shift and hit enter return and there you go your first beautiful your first very beautiful plot chart so again we can see the x-axis here January February March and we can see the y-axis the values that we put in 10 11 and 14 now I'm going to use my mouse on the left hand side look what happens when I hover over the text over this element this this the elements on this blank figure so the go dot figure what's this blank background and I put these elements on top of it which was on a high level was a bar chart I hover over it and you see that January at the bottom it gets highlighted and the number 10 at the top gets highlighted that's very nice I really like that because if I give a presentation and I don't use PowerPoint for presentations I'm not a PowerPoint user fan well I mean you've got to use it sometime but I try to stick with my Jupyter notebooks and it's very nice to have this interactive plots as you do your presentations and look what happened at the top here as well I get quite a few little buttons here to press and that I really find very useful because I can download this plot as a png file directly on my hard drive and I can put that inside of a hard copy if I was writing a report and it's got to go inside of a Word document etc I can just do that I can save this for editing in chart studio that's online in your plot the online account you can zoom you can pan you can select the box and we're going to see all of these because they become very useful I can zoom in I can zoom out and out even further and out even further I can just go back home and that just reset everything for me I can show the closest data on hover so if I do this you're only going to see January being 10 February 11 March 14 and if I click that you see the highlight happens at the bottom on the x-axis as instead of on top at the at the top there and then I can just open plotly the website itself so very nice especially this download as png file so that is fantastic so let's add a little title because you can see at the top my chart has no title so I'm going to introduce a new variable called layout and this layout is going to be a python dictionary so it goes inside of curly braces and it has the key value pair so the key colon the value the key is title and these go inside of quotation marks so the title that's the key and the value for the dictionary for this key is sales for first quarter now I'm going to redo my fig from up here it's still going to have the trace it's still going to have the data all I'm changing is this fig so go dot figure the data still is the data from our first one but the layout is now this dictionary called layout so this data and layout these are the argument names and the argument values well we gave the computer variables the exactly the same name don't worry there won't be any confusion for python it'll understand and plotly this plotly figure will understand what's going on so data this refers to the data that I created with a trace list and layout this layout refers to this layout so let's plot this shift enter shift return and now at the top I have sales for first quarter so a beautiful title now on the top of my figure what about some access labels now with these categories that we put in January February March it's easy to see that these are months but I might want to specify that and more importantly I want to specify what's on this y-axis because what is one two three four you know you need to you need to know what these are so I'm going to just change my layout computer variable still going to be a python dictionary title is still going to be first quarter elements now the x-axis that's the key it holds a value but the value is another dictionary and that dictionary is again a key and value pair so the title is going to be months and the same for y-axis the key is y-axis the value is another dictionary and that dictionary contains a key value pair the key being title and the value being units now I'm going to do something different remember up here we just said data equals data and layout equals layout but we're going to do something a bit different here so instead of creating a blank figure I'm just going to pass everything as a python live as a python dictionary so here we have I plot and inside of that there's this dictionary and the dictionary is just key value pair comma another key value pair so data is just data still the data from upstairs and the layout is just this new layout so let's shift and enter shift in return and now we can see that we have months here as our x-axis title and units as our y-axis title so that's how many units were sold in January February and March so see the difference here between the two so I did not invoke the figure the go dot figure object here I just passed everything to I plot as a dictionary and sometimes it gets confusing because there's many ways in plotly to do something you'll see more ways as we carry on so so when it comes to the layout and and such I try to stick with these just to stick with the python dictionaries when you stick with the dictionaries it becomes slightly less confusing and you can you can find something that's just you know stuck in your mind and it works for you and just carry on with that so I like just to use for layout and then plotting just the this dictionary way of doing things although you don't have to stick with that as we could see here here we created this blank figure and we pass these arguments to it but I can also just use it as pass to I plot just a dictionary and and as I say there are many things I can do with this x-axis this x-axis being a key and the value pair there are many more things than just a title and we'll see some of them just in this in this tutorial so it just it lessens the confusion confusion for me but look at the website the plotly website and you might find other ways and we'll we'll look at some of these in future videos but stick I stick with this but look look what what works for you so let's just rotate these labels at the bottom you know January February March it's quite quite small here so they fit in with this big chart that we have here but sometimes you have long categorical variable names here data point values here and and we've all seen plots where these names actually you know start printing on top of each other and the easiest way to get rid of that is obviously to shorten the data point values these your sample space values but sometimes it's not possible and you just want to rotate them and look at this and that's the reason why I like these dictionaries because look at this I have layout again which is a dictionary key value pair here's another key value pair but now I have two elements here in this value side of the x-axis being the key and the value being another dictionary and inside of that dictionary there are two key value pairs title is months and the tick angle is minus 20 I don't put that inside of quotation marks because it's not this is just a value in a medical value so I'm going to say minus 20 and that rotates at negative 20 degrees and my y-axis is still all the same so let's run that and if we scroll down we see we have this negative 20 degree from from the horizontal it's tilted down negative 20 degrees so if you have these long words and sentences there you know they can all fit in because of that angle now let's color these bars the blue is fantastic I like this color but you are free to do what you want so I'm going to just change my trace here it's still a bar high level bar chart I still have the x-axis values being categorical January February March I still have my y values being a medical 10 11 14 but now I'm going to change the marker I'm going to introduce this marker and again it's a dictionary but this is another way just to do a dictionary so I'm going to call this dict here and the first thing I want to pass is color and the color is going to be a list and this list refers to the elements as you pass them on the x-axis so I have one two three elements January February March I've got to pass three colors here and I'm going to use this format note that I have these quotation marks they can be single or double they're single in this instance but it's RGB a RGB a means I can I can also pass a fourth of value here which would be transparency zero being fully transparent one being completely opaque and anything in between so it's red green and blue channel and then opacity so RGB a and then parentheses 255 that's maximum on the red zero for blue green and zero for blue and full full of paceness so value of one day the second one is 204 204 204 so that's going to be this light gray and it's going to be totally opaque and again totally opaque so January is going to be this red color and March and April is going to be this light gray color and the data is still the trace now I can't just use the ones I used up before because data referred to to a different trace and I've changed the trace here so I've just got to do data equals trace again as a list element and then the layout exactly the same and I'm passing this dictionary to iPod nothing new there and now you can see these beautiful colors because I can now have January being this red and the other colors being this light gray fantastic now why is this one red and these ones gray well I might want to indicate to my audience why this was done so I can actually change this hover text remember if I hit this one show closest data on hover it's going to do both the x-axis and y-axis in one little tooltip there January or hover text there January 10 and if I do that it's just going to highlight them separately now hover text means I can individualize every element that I hover over to have its own text so we've had marker we've seen that so introducing a new argument here to the bar to bar here text and I've got to do it individually so the red was going to be below target above target and above target everything else is the same let's run that and now if I hover I see this extra text appear in this hover so that was I put a 10 and that's below target and that one was above target and that one is above targets you can see you can build in this beautiful narrative here because you can put a lot of information in this text if you really want to to draw attention to what's happening here to inform your audience so that's fantastic so let's move things up just with a grouped bar chart and now I want to have two sets of elements on here and so I've changed my computer variable to trace zero and trace one and with a bar chart be careful now because we want the same sort of space for both I've got January February March and January February March and I've got my y values 10 11 14 as before and the second y is 12 13 17 but now I'm going to add a name because I need to tell properly that these two things are separate so I'm going to say name equals last year and this name equals this year so you can well imagine that I'm just going to take this year's data and compare to last year's data that might be interesting and now look how data is changed I'm now passing two elements to this list still got to be a list still inside of square brackets I've got trace zero and trace one the layout is going to be exactly the same but I'm introducing this new key value pair of bar mode and the bar the bar mode that I want to use here is group so what it's going to do is going to group January and January February and February March and March hence they've got to be the same and then I'm still going to use this dictionary to pass to I plot and now they've been grouped and we see last year in blue and this year in orange that will be the default colors and you see that they are indeed grouped so I've got this year and last year this year and last year this year and last year a beautiful way just to do that now we need to group them like this we can stack them as well and here I have exactly the same thing but instead of bar mode being group I've made bar mode being stacked as the key value pair here and if we run that we see that now we now have this stacked version of it but if I hover you know you can still see that this year's is 12 and last year was 10 you don't have to go to the x-axis in your mind and your audience have to mentally try and see where's that top and subtract from the 10 to see that it actually gets to 12 no no plotly makes it brilliantly easy with your hover you can give a beautiful presentation just to hover over these and explain so that's our first tutorial on plotly my all-time favorite library for plotting inside of python but inside of other languages as well and a nice introduction for you start playing with us and we'll carry on with this playlist on youtube and I'll introduce you to a lot more plotting using plotly for python before you do go there please subscribe to this channel for all the information that I'm trying to get you hit that notification button the little bell there to let you know when new videos are uploaded thanks a lot