Create Pandas Plot Bar Explained with Examples - Spark By {Examples} (2023)

Python PandasDataFrame.plot.bar()is used to plot the graph vertically in the form of rectangular bars. A bar plot is a plot in which, the categorical data with rectangular bars with lengths proportional to the values that they represent. A bar plot shows comparisons among discrete categories. In this article, I will explain DataFrame.plot.bar() function and using this how we can plot the Pandas DataFrame in a bar chart with several examples.

Key Points :

  • A vertical bar chart is often called a column chart and it is referred to as the king of charts as it is most used.
  • Abar chartis used to display a collection of categories on one axis and another axis represents measured value.
  • Theheight of the baris depending upon the measured value.
  • In avertical bar chart,the X-axis will represent categories and Y-axis will represent measured values. In ahorizontal bar chart,it is the inverse
  • Bar charts can be plotted vertically or horizontally.
  • For negative values, in a vertical bar chart, the bars growdownwardsbelow the X-axis.
  • For negative values, in ahorizontal bar Chart, the bars growleftwardsfrom the Y-axis.

1. Quick Examples of Plot Pandas DataFrame in Bar Chart

If you are in a hurry below are some quick python examples of how to plot the pandas DataFrame in a bar chart by using DataFrame.plot.bar().

# Following are the quick examples# Example 1: Draw a plot bar chartdf.plot.bar()# Example 2: Get the individual column as a bardf['death rate'].plot(kind="bar") # Example 3: Set the labels and titledf['death rate'].plot(kind="bar")plot.title("Death rate if corona virus")plot.xlabel("Country")plot.ylabel("Death Rate")# Example 4: Rotate the x-labels by 30 degrees, #and keep the text aligned horizontallydf['death rate'].plot(kind="bar")plot.xticks(rotation=45, horizontalalignment="center")plot.title("Death rate if corona virus")plot.xlabel("Country")plot.ylabel("Death Rate")# Example 5: Get the Horizontal bardf['death rate'].plot(kind="barh")plot.title("Death rate of corona virus")plot.ylabel("Country")plot.xlabel("Death Rate")# Example 6: Create unstacked bardf.plot(kind="bar")plot.title("Death rate if corona virus")plot.xlabel("Country")plot.ylabel("Death Rate")# Example 7: Adding the stacked=True option to plot() # creates a stacked bar plotdf.plot(kind="bar", stacked = True )plot.title("Death rate if corona virus")plot.xlabel("Country")plot.ylabel("Death Rate")

2. Syntax of Pandas plot.bar()

Following is the syntax of the plot.bar().

# Syntax of plot.bar()DataFrame.plot.bar(x=None, y=None, **kwds)

2.1 Parameters of the plot.bar()

  • x :(label or position, optional) Is used to allow the plotting of categorical data versus measured values. If not specified, the index of the DataFrame is used.
  • y :(label or position, optional)Is used to allow the plotting of categorical data versus measured values. If not specified, all numerical columns are used.
  • **kwds :Additional keyword arguments.

2.2 Return Value

  • matplotlib.axes.Axes or np.ndarray.

3. How to use Plot Bar Chart in Pandas?

The pandasDataFrameclass in Python has a member plot() that is used to draw various diagrams for visualization including the Bar Chart. Pandas provides different representations for showing the data in the form of graphs. One of the important diagrams is a Bar Plot which is rapidly used in many applications and presentations.

We can make bar charts quickly and easily with the data fromPandas DataFrames. The bar graph is one of the best for fast data exploration and comparison of variable values between different groups. The main purpose of bar charts or bar plots is to attract user’s eyes by providing a clear look for the ability to compare the length of the objects.

Use plot.bar() to create vertical bar charts and use plot.barh() to create horizontal bar charts. These both methods take XandYas parameters. By default,Xis the index of the DataFrame and y is all the numeric columns.

(Video) Spark df to Pandas df with plotting

In order to customize the bar chart, we have to pass any keyword argument to the bar() or barh() methods. Any keyword arguments supported by the methodDatFrame.plot().Suppose, the keyword argument title places a title on top of the bar chart.

By default, theindex of the DataFrame or Seriesis placed on the x-axis and the values in the selected column are placed as bars. Every Pandas bar chart works this way and any additional columns become new sets of bars on the chart.

4. Create Plot Bar with Labels from Pandas DataFrame

We can create a bar graph by calling a plot.bar() on the pandas DataFrame, so let’s create Pandas DataFrame. Here I have created a single column DataFrame with the sample data of the worldwide death rate of covid-19 in the pandemic. I have taken a list of country names as an index, it is set on an x-axis label and death rate as a measured value, is set on a y-axis label.

# Create DataFrameimport pandas as pddf = pd.DataFrame({"death rate":[316.3, 321.3, 117.2, 38.25, 302.2 ]}, index = ["USA", "Brazil", "Germany", "India", "Uk"])print(df)# Draw a plot bar chartdf.plot.bar()

Yields below output.

# Output death rateUSA 316.30Brazil 321.30Germany 117.20India 38.25Uk 302.20
Create Pandas Plot Bar Explained with Examples - Spark By {Examples} (1)

We can use the below syntax and get the individual columns on a plot bar of a given DataFrame.

# Get the individual column as a bardf['death rate'].plot(kind="bar")

5. Set the Axes Labeling & Set Plot Titles

Let’s give labeling of the x and y axis and set the title in a bar graph, this gives us a better readable bar plot. In order to do so, I will use the Pandasplot() method. By using this generic plot() method we can customize the graph. Labeling of the axis is done by using theMatplotlibobject imported from pyplot.

(Video) Data Visualization with Spark Dataframes & Python Matplotlib | From Scratch

  • xlabel : It is used to set the label of the x-axis.
  • ylabel : It is used to set the label of the y-axis.
  • title : Using this we can set the title of bars
# Set the Bar Plot Labels and Titleimport matplotlib.pyplot as plotdf['death rate'].plot(kind="bar")plot.title("Death rate of corona virus")plot.xlabel("Country")plot.ylabel("Death Rate")

Yields below output.

Create Pandas Plot Bar Explained with Examples - Spark By {Examples} (2)

6. Rotate the x-axis Labels

If we have long labels, it will be too clumsy to see, to reduce this problem plot bars provide two options. The first option is by rotating the labels to make them more specious and another option is rotating the entire chart to end up with a horizontal bar chart.

# Rotate the x-labels by 30 degrees, and # keep the text aligned horizontallydf['death rate'].plot(kind="bar")plot.xticks(rotation=45, horizontalalignment="center")plot.title("Death rate of corona virus")plot.xlabel("Country")plot.ylabel("Death Rate")

Yields below output.

Create Pandas Plot Bar Explained with Examples - Spark By {Examples} (3)

7. Horizontal Bar Charts in Pandas

Using horizontal bars we are able to give an extra long bar title. Horizontal bar charts are reversed to vertical bars. Here,categories are drawn inx-axisand the measures values are drawn iny-axis whereas horizontal bars categories in y-axis and measured valuers are in x-axis. Horizontal chartsare allowed in Pandas by set the kind parameter to barh.

Here, I will plot the given pandas DataFrame in the form of a horizontal bar by using DataFrame.plot(kind="barh"). Since I am labeling axes I have used the plot() method otherwise you should be able to use DataFrame.plot.barh() method.

(Video) Pandas Melt | pd.melt() | df.melt()

# Get the Horizontal bardf['death rate'].plot(kind="barh")plot.title("Death rate of corona virus")plot.ylabel("Country")plot.xlabel("Death Rate")

Yields below output.

Create Pandas Plot Bar Explained with Examples - Spark By {Examples} (4)

This plot is also called the horizontal bar plot and here we can see Brazil has got height death rate.

8. Stacked & Unstacked Bar Graphs

Using stacked and unstacked bars we can also compare columns of a given DataFrame and present them in a bar graph. For that, we need to create multiple columns in a DataFrame. Let’s create DataFrame with multiple columns.

# Create DataFramedf = pd.DataFrame({"1st wave death rate":[316.3, 321.3, 117.2, 38.25, 302.2 ], "2nd wave death rate":[200.1, 127.2, 60.1, 37.6, 230.1], "3rd wave death rate":[20.1, 34.1, 12.1, 4.2, 24.3]}, index = ["USA", "Brazil", "Germany", "India", "Uk"])print(df)

Yields below output.

# Output: 1st wave death rate 2nd wave death rate 3rd wave death rateUSA 316.30 200.1 20.1Brazil 321.30 127.2 34.1Germany 117.20 60.1 12.1India 38.25 37.6 4.2Uk 302.20 230.1 24.3

8.1. Unstacked Bar Plots

Python Pandas un-stacked bar chart. If you select more than one column, pandas by default create an unstacked bar chart with each column forming a bar and the DataFrame index as the x-axis.

Use Unstacked bar plots to compare a particular category with different samples. As we can see from the below, it shows the death rate of coronavirus over the three waves.

(Video) Master Databricks and Apache Spark Step by Step: Lesson 32 - Koalas: Pandas on Spark!

# Create unstacked bardf.plot(kind="bar")plot.title("Death rate if corona virus")plot.xlabel("Country")plot.ylabel("Death Rate")

Yields below output.

Create Pandas Plot Bar Explained with Examples - Spark By {Examples} (5)

8.2. Stacked Bar Plots

Stacked bar charts show the total quantity of each group. Using stacked bar plots we can compare each individual. For, that we need to set the stacked keyword with the value True. Stacked bar plots have each plot stacked one over them.

# Adding the stacked=True option to plot() # creates a stacked bar plotdf.plot(kind="bar", stacked = True )plot.title("Death rate if corona virus")plot.xlabel("Country")plot.ylabel("Death Rate")

Yields below output.

Create Pandas Plot Bar Explained with Examples - Spark By {Examples} (6)

9. Conclusion

In this article, I have explained pandas DataFrame.plot.bar() is used to create a vertical bar plot, plot.barh() is used to create a horizontal bar plot. And also I explained the organization of the bar graph using various keyword arguments and explained stacked and unstacked plots when you have multiple columns on DataFrame.

Happy learning !!

(Video) Koalas: Pandas on Apache Spark

Related Articles

  • Change Pandas Plot Size
  • How to Create Scatter Plot in Pandas?
  • How to Generate Time Series Plot in Pandas?
  • How to Plot the Boxplot from DataFrame?
  • Sum Pandas DataFrame Columns With Examples
  • Create Pandas DataFrame With Working Examples
  • Select Pandas DataFrame Rows Between Two Dates
  • Pandas Convert String Column To DateTime

References

You may also like reading:

  1. Pandas Drop Columns from DataFrame
  2. Pandas Normalize Columns of DataFrame
  3. Pandas Merge DataFrames Explained Examples
  4. pandas reset_index() – Rest Index on DataFrame
  5. Pandas Read Excel with Examples
  6. How to Unpivot DataFrame in Pandas?
  7. Pandas Get Floor or Ceil of Series
  8. Count NaN Values in Pandas DataFrame
  9. Sort Pandas DataFrame by Date (Datetime)
  10. Pandas Rolling Sum

Videos

1. Spark Tutorial | Spark Tutorial for Beginners | Apache Spark Full Course - Learn Apache Spark 2020
(Great Learning)
2. Exploratory Data Analysis (EDA) using Apache Spark and Python
(AIEngineering)
3. Building Spark Applications: Making Sense of Data Summary Statistics and Distributions
(LiveLessons)
4. Spark Basics: DataFrames
(Data Science for Everyone)
5. Koalas: Making an Easy Transition from Pandas to Apache Spark
(Databricks)
6. How to use PySpark and Spark SQL , MatPlotLib and Seaborn in Azure Synapse Analytics and Spark Pool
(SoftWiz Circle)
Top Articles
Latest Posts
Article information

Author: Rev. Porsche Oberbrunner

Last Updated: 05/04/2023

Views: 6129

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.