Stacked barplot
Here we will create a stacked barplot through dataframe by passing the stacked parameter as True.
Dataframe.plot(kind=”bar”, stacked = True, rot=0)
Code:
Python
# importing the pandas library import pandas as pd # Reading csv file df = pd.read_csv( 'Data.csv' ) # Creating crosstab crosstb = pd.crosstab(df.Nationality, df.Handedness) # Creating barplot pl = crosstb.plot(kind = "bar" , stacked = True , rot = 0 ) |
Output:
Using pandas crosstab to create a bar plot
In this article, we will discuss how to create a bar plot by using pandas crosstab in Python. First Lets us know more about the crosstab, It is a simple cross-tabulation of two or more variables.