Transcript:
Welcome to core basics coding tutorial today. We are covering bar chart in matplotlib. If you Google bar chart and look at some of the sample images, you see a chart like this where each of the values are represented as a vertical bar in finance world bar charts are used to represent measures like revenue and we are going to create chart similar to this in matplotlib today as usual. I have launched Jupiter, not book and Imported Matplotlib and Numpy module initially, and now we are going to use some static data. Say I have revenues for some of the major. US. Tech companies in these two lists. OK, and now I want to create a bar chart out of it, so the way you do it. Is you just call? Plt Dot bar function, OK? And in argument, you, you want to pass company and revenue, but the thing is, it won’t honor string values here, so what you have to do? Is you have to create a vibe position? Alright, and you would do that by doing. Np Dot a range length of your company list. So let me put this into a separate cell and let’s see what Y pose. Looks like. This is simple Numpy array with 0 1 2 3 value. Now you can use a simple list also, but there is a reason. I am using Numpy array and we’ll cover that little later on now in my bar function, I can use Y pose because it is a list of numbers and not the strings. And when I plot a chart. It looks something like this now. These bars represents revenues of each of these companies, but here. I have values so now to place these values with these labels. You have to use X ticks function, so you will just call P and T Dot X px. And there are two arguments to it, okay. The first argument is your white by position and your second argument. Is your company list? What this will do is it will replace these values, 0 1 2 3 4 with the corresponding elements in this second list, so 0 will be replaced by. Google 1 will be replaced by Amazon and so on. Can you run that you now? See Google, Amazon? Msft and Facebook’s revenue plotted as a bar chart. Now let’s add some X and y label just to make this chart a little fancier. So these are revenues in US billion dollars and actually viable is revenues and X level is the company name, So we already have company name, so I do not need to write it. I will set it title. US tech stocks. And now my chart looks better. OK, now. What if I want to plot two different measures so here? This particular measure is revenue, So let me add a label for it so revenue and Alt Dot Legion. Now, let’s say I have profit numbers for each of these companies. Okay, so I have profit number also, and I want to plot profit on a same chart, so in finance, people typically plot side by side bar chart for revenue and profit so that they get a comparison of how much company is earning and how much they are how much revenues they are generating and how much is their actual profit after deducting expenses So you can simply call? Plt dot bar once again with y position and profit. This time the label is profit. Now, when you do this, what is happening is it is plotting the profit, but it is being plotted in a same chart. You can see that same. Bar contains profit and revenue both. I want to generate side by side bars for this. The reason this is happening is because the Y position that the sorry the X position is actually the same for both of these. I should have named this as X position. Actually, okay, so let me just quickly change it. Okay, So the X position is same. I need to make it a little different by subtracting some number from it. So what I’m doing is now X position for my revenue is 1 minus 0.2 so it will be like point eight like here to BC minus 0.2 then point eight and one point eight and so on. Okay, now that is one reason why. I created Numpy array because if you are simple list, then it this will be hard, but if your numpy array then expose minus zero point, two is going to create a new array with four elements and zero point. Two will be subtracted from two of these elements. Okay, when I run this, I see a little better shot, though, not perfect, but at least the bars are separate to control the width of this birth. You can call a width function, so I’m just going to set width of these bars to be 0.4 and when I do that now, I see a better chart so here. I have Google’s revenue and their profit Amazon’s revenue, you see? Amazon is a weird company. By the way they don’t generate any profit they keep on increasing the revenue, but the profit is like really small, and I never understood Amazon, Msft and Facebook. Okay now! The last thing we want to cover is horizontal bar chart. Sometimes people want to draw the bar chart, not in a vertical fashion, but in a horizontal fashion. So for that, there is a function called bar. H bar H means horizontal bar. Okay, and lets. Just run this and see what happens here. I need to change this to White X. So that company names appear here and you can see that you can generate a horizontal charge-charge like this. Okay, so that’s all we have for this tutorial. I want to mention the same thing again that by just watching this tutorial, you are not going to learn anything you have to download this. Jupiter, not book, which I have provided in the video description below and just play with it play with different properties, Try to use this in any sample project, and that would be the real way of learning programming. Okay, and you can look at. Matt brought lips documentation for your bar chart. Check out different properties and just play with it just to get a feel of it. Alright, thank you very much for watching goodbye.