{"id":26331,"date":"2021-12-17T08:43:01","date_gmt":"2021-12-17T03:13:01","guid":{"rendered":"https:\/\/python-programs.com\/?p=26331"},"modified":"2021-12-17T08:43:01","modified_gmt":"2021-12-17T03:13:01","slug":"python-bar-plot-visualization-of-categorical-data","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-bar-plot-visualization-of-categorical-data\/","title":{"rendered":"Python Bar Plot: Visualization of Categorical Data"},"content":{"rendered":"
Data visualization allows us to analyze the data and examine the distribution of data in a pictorial way.<\/p>\n
We may use BarPlot<\/strong> to visualize the distribution of categorical data variables. They depict a discrete value distribution. As a result, it reflects a comparison of category values.<\/p>\n The x-axis shows discrete values, whereas the y axis represents numerical values of comparison and vice versa.<\/p>\n The Python matplotlib package includes a number of functions for plotting data and understanding the distribution of data values.<\/p>\n To construct a Bar plot with the matplotlib module, use the matplotlib.pyplot.bar() function.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n x:<\/strong> The barplot’s scalar x-coordinates<\/p>\n height:<\/strong> It is the height of the bars to be plotted.<\/p>\n width:<\/strong> This is optional. It is the width of the bars to be plotted.<\/p>\n bottom:<\/strong> It is the vertical baseline.<\/p>\n align:<\/strong> This is optional. It is the type of bar plot alignment.<\/p>\n Example:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<\/strong><\/p>\n Plots are mostly used to depict the relationship between variables. These variables can be entirely numerical or represent a category such as a group, class, or division. This article discusses categorical variables and how to visualize them using Python’s Seaborn package.<\/p>\n Seaborn, in addition to being a statistical plotting toolkit, includes various default datasets.<\/p>\n The Python Seaborn<\/strong> module is built on top of the Matplotlib module and provides us with some advanced functionalities for better data visualization.<\/p>\n Syntax:<\/strong><\/p>\n Example:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Data visualization allows us to analyze the data and examine the distribution of data in a pictorial way. We may use BarPlot to visualize the distribution of categorical data variables. They depict a discrete value distribution. As a result, it reflects a comparison of category values. The x-axis shows discrete values, whereas the y axis …<\/p>\nBarPlot with <\/strong>Matplotlib<\/h4>\n
matplotlib.pyplot.bar(x, height, width, bottom, align)<\/pre>\n
\n
# Import matplotlib.pyplot module using the import keyword.\r\nimport matplotlib.pyplot as plt\r\n# Give some random list(gadgets) as static input and store it in a variable.\r\ngadgets = ['mobile', 'fridge', 'washingmachine', 'tab', 'headphones']\r\n# Give the other list(cost) as static input and store it in another variable.\r\ncosts = [15000, 20000, 18000, 5000, 2500]\r\n# Pass the given two lists as the arguments to the plt.bar() function\r\n# to get the barplot of those lists.\r\nplt.bar(gadgets, costs)\r\n# Show the barplot of the given two lists using the show() function.\r\nplt.show()\r\n<\/pre>\n
<\/h4>\n
BarPlot with Seaborn Library<\/strong><\/h4>\n
seaborn.barplot(x, y)<\/pre>\n
\n
# Import seaborn module using the import keyword.\r\nimport seaborn\r\n# Import matplotlib.pyplot module as plt using the import keyword.\r\nimport matplotlib.pyplot as plt\r\n# Import dataset using read_csv() function by passing the dataset name as\r\n# an argument to it.\r\n# Store it in a variable.\r\ndummy_dataset = pd.read_csv('dummy_data.csv')\r\n# Pass the id, calories columns and above dataset as the arguments to the \r\n# seaborn.barplot() function to get the barplot of those.\r\nseaborn.barplot(x=\"id\", y=\"calories\", data=dummy_dataset)\r\n# Display the barplot of the using the show() function.\r\nplt.show()\r\n<\/pre>\n