Creating bar plot using more than two variables from the crosstab
In the above example, we found the relationship between nationality and the handedness of the people. We can also create a crosstab with more than two values. We will implement this in the following example.
Python3
# importing the module import pandas as pd # reading the csv file df = pd.read_csv( 'Data.csv' ) # Crosstab with three variables crosstb = pd.crosstab(df.Sex, [df.Nationality, df.Handedness]) # Bar plotting a = crosstb.plot(kind = 'bar' , rot = 0 ) a.legend(title = 'Handedness' , bbox_to_anchor = ( 1 , 1.02 ), loc = 'upper left' ) |
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.