Python | Панды Dataframe.plot.bar

Опубликовано: 28 Марта, 2022

Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.

Pandas DataFrame.plot.bar() plots the graph vertically in form of rectangular bars.

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

Parameters:
x : (label or position, optional) Allows plotting of one column versus another. If not specified, the index of the DataFrame is used.
y : (label or position, optional) Allows plotting of one column versus another. If not specified, all numerical columns are used.
**kwds : Additional keyword arguments

Returns: matplotlib.axes.Axes or np.ndarray of them

Example #1: Using DataFrame.plot.bar() to plot the graph vertically in form of rectangular bars

# importing matplotlib
import matplotlib.pyplot
  
# importing pandas as pd
import pandas as pd
  
# importing numpy as np
import numpy as np
  
# creating a dataframe 
df = pd.DataFrame(np.random.rand(10, 3), columns =["a", "b", "c"])
  
print(df)


Now we will use a function DataFrame.plot.bar() to plot a graph vertically in form of rectangular bars

# using a function df.plot.bar()
df.plot.bar()

Output:

 
Example #2: Using DataFrame.plot.bar() to plot the graph vertically in form of rectangular bars.

# importing matplotlib
import matplotlib.pyplot
  
# importing pandas as pd
import pandas as pd
  
# importing numpy as np
import numpy as np
  
# creating a dataframe
df = pd.DataFrame(np.random.rand(10, 10),
                  columns =["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"])
  
df


Now we will use a function DataFrame.plot.bar() to plot a graph vertically in form of rectangular bars

# using a function df.plot.bar()
df.plot.bar()

Выход :

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.