Bar Plot using Plotly | Python | Data Visualization | Plotly

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video you will learn how to make bar plot using plotly Library let's import plotly import plotly dot Express as PX import pandas as PD we are going to use this Express from plotly and we will import it as PX so plotly Express is the easy to use high level interface to plotly which operates on a variety of types of data and produce easy to style figures that is why we are going to use express and for all the charts that we are going to see in the coming tutorial also we will be using this plotly Express now I am going to import one data set and save it in a variable DF I'll write PD dot read underscore CSV and then sample dot CSV I'll print the head DF dot head and we have this data frame here in this data frame we have columns such as country gender age color and pay now we are going to make our bar plot so first we will make a very simple bar plot using the default parameters and to make a bar plot we have to call the method bar and as you see the air we have imported plotly dot Express as PX so we will use this PX and then we will call the method bar I'll type here PX dot bar inside this we have to give the data frame name and our data frame name is DF then we have to define the X label X is going to be country and let's plot it and this will plot the bar plot this will make a bar plot and here in the output you can see we got a nice bar plot here and this is the default one we have countries such as USA Canada Germany and Italy these are the five countries present in this variable country and you can also verify it and I will write here DF of country dot value underscore counts and here you can see in the output we have countries such as USA Germany Canada and Italy and that's why we got here four bar plot and for each country now we can change the color of this bar for that we have to pass on parameter and here I will put a comma then the parameter name is color and then color what we want to give we want to type Here country only and if I execute the code here in the output you can see now we got a bar plot with different colors we have USA Canada Germany and Italy and this time also this time we got a legend also here you can see these are the legends we have USA is denoted by the color blue Canada is uh orange or I think it's red Germany is green and it lays purple and there are various other options here so we have such as Auto scale reset access zoom in zoom out if I click here zoom in so this will see we get a bigger picture of this bar plot and we can then zoom out also so we have to click here zoom out then we will get the default one this is also Zoom so there are these different parameters that you can explore now let's say if you want to pass color of your choice here when we have typed this color is equal to continue we got this default colors but we can also pass our own color and for that we need to pass one more parameter I'll copy this code from here I'll paste it here and then I'll put a comma here and I'll pass this parameter that is color underscore discrete sequence color underscore discrete sequence and then here we have to pass the color as a list and since we have four countries here one two three and four we will pass here four different colors let's say red then blue then green and let's say purple let me put here now if I execute the code here in the output you can see now we got a color of our own choice here first one is red color the second is blue third is green fourth one is purple this is how you can pass your own color and we can also plot the count of eight cities uh each countries in each bar for that first we need to create a new column count using Group by and save it in a new data frame so here we will plot the count of these countries here but for that we need to make one more column in our data frame and here you will see in this data frame we don't have the count column let's make a count column a column that we that would look similar to this one where we will have count of each countries and let's make that I'll make here new data frame and that would be named as data and then DF dot Group by and we want to group by on country so I'll write Here country because we want to get the count of countries dot size Dot rename and then this column would be named as count then we will reset the index reset underscore index now let us execute our code now if I type here data dot head here in the output you can see we got a new data frame here where we have the count of each country now when we will make a new plot we will get the count on top of this bar here now let's do that now for that what I will do I'll copy this code from here and I will paste it here now this time our data frame is going to be this one data here so I will replace this DF with data X is going to be country Y is going to be now we have to put here y variable because now we have two variables and the y1 is going to be count in X we have this country in y we have count and then we will have color color is going to be country and then to place these numbers on top of this bar we have to use one parameter and that parameter name is text and what we want to get we want to get the count so we will write here count here we will select the counter variable now let us execute our code and here in the output you can see that now we got a bar plot along with its count it says that in this country Canada there are total 26 samples for this country Canada for Germany we have 27 Italy 20 and USA we have 27 and we can also confirm here here you can see Canada is 26 J1 is 27 Italy is 20 and USA is 27. now let's work with some inbuilt data set so in this plotly Express in this plotly Express we have some inbuilt data set as well and let us print one of the inbuilt data set I will write here PX dot data and from data there are different data set and we will use Gap minder I will write here Gap minder and if I execute the code so here in the output you can see we got a data frame here and this is the inbuilt data set which we can access with the help of this broadly Express now we are going to make a plot of this data set we will use this continent or we can use this variable Country Now let us make a bar plot of Country Canada in this country Afghanistan in this country this is one of the country is Afghanistan then we are seeing Zimbabwe there are many more countries in this data frame we are going to select the country as Canada and then make a bar plot and before that we need to save this data frame in a new variable so let's do the filter and then save it in a new data frame I will write here data underscore Canada I am making a new variable and in this I will have data from Canada only so I will write here PX dot data Dot gapminder Gap finder then dot query so we have this query and then inside this we will pass our filter condition so here we will write this country query country is equal to Canada so we want results from this country Canada only I have to put here y now let me execute the code I think I have made some mistake must be a string to be evaluated PX dot data Dot gapminder dot query okay so what I have to do I have to remove this string from here and then I have to put a single quote here and if I try it now so I got a data frame here let me print here data unders for Canada dot head we should have results for all the countries for the Country Canada only not for all the country now here in the output you can see in country we are getting only Canada so we have this data frame for this country Canada only now what we are going to do uh we are going to make a plot here let's say we will plot the uh earwise population so we have this population variable and we have this year variable we are going to plot the year wise increase in the population of the Country Canada so let's do that I will write here PX dot bar and the data that we are going to use is data underscore Canada data underscore Canada and then in X we will have on the x-axis we will have ER and then on the y-axis we will have population so we have this P of P now let us execute our code so I got error here I have to type here Y is equal to now let's execute it again and here in the output you can see we got the bar plot here so it shows us the year-wise population of Canada and we can see that the population of Canada in the year 1915 was very low which is around 15 million then here in 1960 it was around I guess uh 70 or 50 of 17 or 18 million then you know as the number of years increase the population also increased so here in this 2000 and this is around 2010 you can see that the population increased from 15 million to around 32 million it got doubled now here also we can give the legend so here on on the top of These Bars we can uh actually print the ear so let us do that what I will do I'll copy this code from here and then I will paste it here and I'll put a comma here and I will pass one parameter which is text is equal to here and here in the output you can see for each bar plot so for each bar now for each bar we have a label so this is 1952 1957 1962 1967 and so on this is how you can also put the label on the top of each bar now let's make this bar plot more colorful and interactive so here we see that all the bars are with the same color which is blue and this doesn't look so good here so let's make this more beautiful here I'll copy this code from here again then I will paste it here and let us pass some more parameters here let me first remove this text parameter now I'll pass one parameter over underscore data and in this so in this bar plot we are going to see more information and now we will see the information about let me print this data underscore Canada so let us let me just print to yeah so in our new board plot let's also see the uh the life expectancy for each year so we will write here life underscore expectancy in list and then we can also look at uh let's say GDP per capita for each of this year so I'll put a comma here and I can just copy paste from here I'll copy it from here and I'll paste it here I did not get copy I'll copy from here and then I will paste it here now we will give label so what we are doing is we are also getting the information of this life expectancy and GDP per capita and then we will make labels labels is going to be let's say for this population we're creating a dictionary for level pop is nothing but population of Canada so label is going to be population of Canada and then we can give the height of this bar plot let's say 400 now let us execute our code so here in the output you can see that now we got more information if I click here so what I see year 1977 and population 25.2019 million so here we are getting just two information but if I hover over this chart I am getting more information for more information I'm getting I'm getting here then population of uh then life expectancy and GDP per capita I am getting a three information so this is more information this is not just uh one information now let's pass color here so to make it more beautiful what I will do color is going to be we will color each bar plot with this variable life expectancy now let us execute our code and here in the output you can see now we get a colored bar plot here we have different colors for different bar so this is for 1952 this is 1957 1962 and so on so now we got different colors for different part different bars there are two types of bar charts we can make from two different data types so we have two different data types such as long data and white data and let me show you an example of long data uh so what I will do I'll write here PX from this plotly Express I will call data and from that I will write here medals underscore long this is a long data frame and here in the output you can see so we have this uh country names here Nation then metal what metal that country has won and the number of medals they have won so this is a wide data because this is a long data sorry not wide data long because we are seeing all of this in a column and in our column we have so many observation not just few 5 to 10 observations so and here we are having this you know category for gold they are all in this column metal but if we have these categories here instead of in row then that will become a white data set now let us make a bar plot of this one so I'll make a new data frame and save this result in that so that I can make a bar plot I'll make a new variable let's say white underscore data and then I will save it here now if I print white data dot head so I have this data frame here and I have Nation medal and count now let's make a bar plot I will write here PX dot bar white underscore data on our x-axis we will have this Nation here then on our y-axis we will have count and then color is going to be we will color this bar plot with this column medal so it will be colored with gold silver and bronze then we will give a title title is going to be long format data let us execute our code and here in the output you can see that we got a long format data here and we see three different colors in each bar so we have this country here South Korea and now this blue represent how many gold medal South Korea won silver how many silver medals South Korea won and bronze eight is in green how many Bronze South Korea won and we see that most of the cold uh gold was won by South Korea because they have so much of blue color here then the least number of bronze is won by China we have few number of greens here so this way you can plot the long data or this is long data and and by mistake I have written it here white data so I'll replace is as long data it will remain same just I have to change the name and I have changed it here also now we will look what is white data I'll write here PX Dot data and Dot medals underscore White and now this is a white data because we see that we had these values in one column in our long data frame in this column middle but now we have these three categories in a three different column that is why this is a wide data and let us save this so I will write here wide underscore data and then I will paste it and wide underscore data dot head so we have these values here we have these columns Nation gold silver and bronze now let us plot this one I will write here PX dot bar wide underscore data X is going to be Nation and in y we will have a list because we have to pass these three variables so we will pass them as a list I'll write here gold silver and then bronze then we will have a title title is going to be log format and data and here this is sorry white format data I am getting confused between wide and long and here I have to write yeah I've written long format data that's correct here but the variable name was not correct now if I execute this code so we will have this white data here so the data frame looks different but the output would be same because we have the same observations but the data frame output is different now let's look at the last plot which is uh which is a pattern fill so we can make a bar plot with pattern fills and let's do that I will write here PX dot bar and let us say we will consider this data frame here long data this one so I will pass there long underscore data X is going to be medals medal Y is going to be count so let me put print it here first long underscore data dot head so we have this x middle Y is count and then we have to pass the parameter pattern underscore shape underscore sequence and then we have to define the sequence of first is going to be Dot full stop second is going to be X and third is going to be plus now let us execute our code here in the output you can see we got a fill plot here a pattern fills so these are the pattern fill plot and we can also give here color so I will write here color is equal to Nation and I have to put a comma here now here in the output you can see we got a different color here with respect to the nation so for South Korea we have this blue China we have red and for Canada we have green these are the bar plots that you can make in plot leaf and you can try it on your own side as well I hope you enjoyed this video if you like my video you can subscribe to my channel thank you for watching
Info
Channel: Stats Wire
Views: 9,070
Rating: undefined out of 5
Keywords: datascience, machinelearning, python, artificialintelligence, ai, data, dataanalytics, bigdata, programming, coding, deeplearning, datascientist, computerscience, datavisualization, analytics, pythonprogramming, dataanalysis, ml, statistics, stats wire, statswire, algorithm, machine learning, data mining, data science course, data science project, machine learning tutorial, machine learning projects, machine learning python, python tutorial
Id: oz7sg5NW4WU
Channel Id: undefined
Length: 25min 28sec (1528 seconds)
Published: Mon Nov 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.