Pandas DataFrame DataFrame.plot.bar() Function
Python Pandas DataFrame.plot.bar() function draws a bar chart along the specified axis. It plots the graph categorically. The categories X
are given on the axis and the values Y
are given on the axis.
pandas.DataFrame.plot.bar()
grammar
DataFrame.sample(x=None, y=None, **kwds)
parameter
x |
This is the axis on which the categories are plotted. If not specified, DataFrame the index of |
y |
It represents the numerical values plotted against the categories. If not specified, then DataFrame all numeric columns will be plotted against the categories. |
**kwds |
These are additional keyword arguments used to customize the plotted graphics. You can refer to this |
Return Value
It returns an N-dimensional array, if subplots=True
, then it returns an N-dimensional array with each column having matplotlib.axes.Axes
. If subplots=True
, then it returns an N-dimensional array with each column having matplotlib.axes.Axes
.
Sample code:DataFrame.plot.bar()
Let's first use a simple DataFrame
to understand this function.
import pandas as pd
dataframe = pd.DataFrame({'Value':[100, 200, 300]})
print(dataframe)
Ours DataFrame
is,
Value
0 100
1 200
2 300
All the parameters of this function are optional. If we do not pass any parameters while executing this function, then it takes the index as X
the axis and the numeric data columns as Y
the axes.
import pandas as pd
import matplotlib.pyplot as plt
dataframe = pd.DataFrame({"Value": [100, 200, 300]})
axis = dataframe.plot.bar(rot=0)
print(axis)
plt.show()
Output:
AxesSubplot(0.125,0.125;0.775x0.755)
The parameter rot
is an additional keyword argument that changes the rotation of the category names on the x-axis.
If we don't set it rot
, the following situation will appear in the figure.
Example code: DataFrame.plot.bar()
Relationships with multiple columns of data
Now, we will change our DataFrame
to a complex data.
import pandas as pd
dataframe = pd.DataFrame(
{
"Age": [23, 17, 40, 38, 24, 12, 45],
"Avg Age in Family": [70, 65, 80, 55, 60, 63, 90],
},
index=["Olivia", "John", "Laura", "Ben", "Kevin", "Robin", "Elsa"],
)
print(dataframe)
Ours DataFrame
is,
Age Avg Age in Family
Olivia 23 70
John 17 65
Laura 40 80
Ben 38 55
Kevin 24 60
Robin 12 63
Elsa 45 90
Use DataFrame.plot.bar()
the function to plot this DataFrame
.
import pandas as pd
import matplotlib.pyplot as plt
dataframe = pd.DataFrame(
{
"Age": [23, 17, 40, 38, 24, 12, 45],
"Avg Age in Family": [70, 65, 80, 55, 60, 63, 90],
},
index=["Olivia", "John", "Laura", "Ben", "Kevin", "Robin", "Elsa"],
)
axis = dataframe.plot.bar(rot=0)
print(axis)
plt.show()
Output:
AxesSubplot(0.125,0.125;0.775x0.755)
It generates a bar chart of numerical data containing two bars for each category. It helps in analyzing data effectively.
Example code: Creating a subgraph DataFrame.plot.bar()
withsubplots=True
If subplots=True
, then the function returns an N-dimensional array with for each column matplotlib.axes.Axes
. Using this function, we can separate the data columns into different sub-graphs instead of a single graph.
import pandas as pd
import matplotlib.pyplot as plt
dataframe = pd.DataFrame(
{
"Age": [23, 17, 40, 38, 24, 12, 45],
"Avg Age in Family": [70, 65, 80, 55, 60, 63, 90],
},
index=["Olivia", "John", "Laura", "Ben", "Kevin", "Robin", "Elsa"],
)
axes = dataframe.plot.bar(rot=0, subplots=True)
print(axes)
plt.show()
Output:
[<matplotlib.axes._subplots.AxesSubplot object at 0x0000029A89B06DC8>
<matplotlib.axes._subplots.AxesSubplot object at 0x0000029A89B4B2C8>]
Example code: DataFrame.plot.bar()
To plot a single column of data
import pandas as pd
import matplotlib.pyplot as plt
dataframe = pd.DataFrame(
{
"Age": [23, 17, 40, 38, 24, 12, 45],
"Avg Age in Family": [70, 65, 80, 55, 60, 63, 90],
},
index=["Olivia", "John", "Laura", "Ben", "Kevin", "Robin", "Elsa"],
)
axis = dataframe.plot.bar(y="Age", rot=0)
print(axis)
plt.show()
Output:
AxesSubplot(0.125,0.125;0.775x0.755)
We can also plot any data column against any other column instead of plotting the index as categories.
import pandas as pd
import matplotlib.pyplot as plt
dataframe = pd.DataFrame(
{
"Age": [23, 17, 40, 38, 24, 12, 45],
"Avg Age in Family": [70, 65, 80, 55, 60, 63, 90],
},
index=["Olivia", "John", "Laura", "Ben", "Kevin", "Robin", "Elsa"],
)
axis = dataframe.plot.bar(x="Age", rot=0)
print(axis)
plt.show()
Output:
AxesSubplot(0.125,0.125;0.775x0.755)
Example code: DataFrame.plot.bar()
with specified colors
import pandas as pd
import matplotlib.pyplot as plt
dataframe = pd.DataFrame(
{
"Age": [23, 17, 40, 38, 24, 12, 45],
"Avg Age in Family": [70, 65, 80, 55, 60, 63, 90],
},
index=["Olivia", "John", "Laura", "Ben", "Kevin", "Robin", "Elsa"],
)
axis = dataframe.plot.bar(rot=0, color="m")
plt.show()
It DataFrame
assigns colors to all columns in m
.
import pandas as pd
import matplotlib.pyplot as plt
dataframe = pd.DataFrame(
{
"Age": [23, 17, 40, 38, 24, 12, 45],
"Avg Age in Family": [70, 65, 80, 55, 60, 63, 90],
},
index=["Olivia", "John", "Laura", "Ben", "Kevin", "Robin", "Elsa"],
)
axis = dataframe.plot.bar(rot=0, color=["r", "b"])
print(axis)
plt.show()
We can also specify different colors color
for different columns in by providing a list of colors to the parameter .DataFrame
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Convert Pandas to CSV without index
Publish Date:2025/05/01 Views:159 Category:Python
-
As you know, an index can be thought of as a reference point used to store and access records in a DataFrame. They are unique for each row and usually range from 0 to the last row of the DataFrame, but we can also have serial numbers, dates
Convert Pandas DataFrame to Dictionary
Publish Date:2025/05/01 Views:197 Category:Python
-
This tutorial will show you how to convert a Pandas DataFrame into a dictionary with the index column elements as keys and the corresponding elements of other columns as values. We will use the following DataFrame in the article. import pan
Convert Pandas DataFrame columns to lists
Publish Date:2025/05/01 Views:191 Category:Python
-
When working with Pandas DataFrames in Python, you often need to convert the columns of the DataFrame into Python lists. This process is very important for various data manipulation and analysis tasks. Fortunately, Pandas provides several m
Subtracting Two Columns in Pandas DataFrame
Publish Date:2025/05/01 Views:120 Category:Python
-
Pandas can handle very large data sets and has a variety of functions and operations that can be applied to the data. One of the simple operations is to subtract two columns and store the result in a new column, which we will discuss in thi
Dropping columns by index in Pandas DataFrame
Publish Date:2025/05/01 Views:99 Category:Python
-
DataFrames can be very large and can contain hundreds of rows and columns. It is necessary to master the basic maintenance operations of DataFrames, such as deleting multiple columns. We can use dataframe.drop() the method to delete columns
Pandas Copy DataFrame
Publish Date:2025/05/01 Views:53 Category:Python
-
This tutorial will show you how to DataFrame.copy() copy a DataFrame object using the copy method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 ], "Cost" : [ "300" , "400" , "350" ], } ) print (items_df) Output:
Pandas DataFrame.ix[] Function
Publish Date:2025/05/01 Views:168 Category:Python
-
Python Pandas DataFrame.ix[] function slices rows or columns based on the value of the argument. pandas.DataFrame.ix[] grammar DataFrame . ix[index = None , label = None ] parameter index Integer or list of integers used to slice row indice
Pandas DataFrame.describe() Function
Publish Date:2025/05/01 Views:120 Category:Python
-
Python Pandas DataFrame.describe() function returns the statistics of a DataFrame. pandas.DataFrame.describe() grammar DataFrame . describe( percentiles = None , include = None , exclude = None , datetime_is_numeric = False ) parameter perc
Pandas DataFrame.astype() Function
Publish Date:2025/05/01 Views:160 Category:Python
-
Python Pandas DataFrame.astype() function changes the data type of an object to the specified data type. pandas.DataFrame.astype() grammar DataFrame . astype(dtype, copy = True , errors = "raise" ) parameter dtype The data type we want to a