📌  相关文章
📜  如何使用 Pandas 绘制数据框?

📅  最后修改于: 2022-05-13 01:54:23.765000             🧑  作者: Mango

如何使用 Pandas 绘制数据框?

Pandas是数据科学中最流行的Python包之一。 Pandas 提供了强大且灵活的数据结构( Dataframe & Series )来操作和分析数据。可视化是解释数据的最佳方式。

Python有许多流行的绘图库,使可视化变得容易。其中一些是matplotlibseabornplotly 。它与 matplotlib 有很好的集成。我们可以使用plot()方法绘制数据框。但是我们需要一个数据框来绘制。我们可以通过将字典传递给 pandas 库的DataFrame()方法来创建数据帧。

让我们创建一个简单的数据框:

Python
# importing required library
# In case pandas is not installed on your machine
# use the command 'pip install pandas'. 
import pandas as pd
import matplotlib.pyplot as plt
  
# A dictionary which represents data
data_dict = { 'name':['p1','p2','p3','p4','p5','p6'],
              'age':[20,20,21,20,21,20],
              'math_marks':[100,90,91,98,92,95],
              'physics_marks':[90,100,91,92,98,95],
              'chem_marks' :[93,89,99,92,94,92]
              }
  
# creating a data frame object
df = pd.DataFrame(data_dict)
  
# show the dataframe
# bydefault head() show 
# first five rows from top
df.head()


Python3
# scatter plot
df.plot(kind = 'scatter',
        x = 'math_marks',
        y = 'physics_marks',
        color = 'red')
  
# set the title
plt.title('ScatterPlot')
  
# show the plot
plt.show()


Python3
# bar plot
df.plot(kind = 'bar',
        x = 'name',
        y = 'physics_marks',
        color = 'green')
  
# set the title
plt.title('BarPlot')
  
# show the plot
plt.show()


Python3
#Get current axis
ax = plt.gca() 
  
# line plot for math marks
df.plot(kind = 'line',
        x = 'name',
        y = 'math_marks',
        color = 'green',ax = ax)
  
# line plot for physics marks
df.plot(kind = 'line',x = 'name',
        y = 'physics_marks',
        color = 'blue',ax = ax)
  
# line plot for chemistry marks
df.plot(kind = 'line',x = 'name',
        y = 'chem_marks',
        color = 'black',ax = ax)
  
# set the title
plt.title('LinePlots')
  
# show the plot
plt.show()


输出:

情节

有许多图表可用于解释数据。每个图表用于一个目的。其中一些图是条形图、散点图和直方图等。

散点图:

要获得数据帧的散点图,我们所要做的就是通过指定一些参数来调用plot()方法。

kind='scatter',x= 'some_column',y='some_colum',color='somecolor'

Python3

# scatter plot
df.plot(kind = 'scatter',
        x = 'math_marks',
        y = 'physics_marks',
        color = 'red')
  
# set the title
plt.title('ScatterPlot')
  
# show the plot
plt.show()

输出:

有很多方法可以自定义地块,这是基本的一种。

条形图:

同样,我们必须为plot()方法指定一些参数来获取条形图。

kind='bar',x= 'some_column',y='some_colum',color='somecolor'

Python3

# bar plot
df.plot(kind = 'bar',
        x = 'name',
        y = 'physics_marks',
        color = 'green')
  
# set the title
plt.title('BarPlot')
  
# show the plot
plt.show()

输出:

线图:

单列的线图并不总是有用的,为了获得更多的见解,我们必须在同一个图表上绘制多列。为此,我们必须重用轴。

Python3

#Get current axis
ax = plt.gca() 
  
# line plot for math marks
df.plot(kind = 'line',
        x = 'name',
        y = 'math_marks',
        color = 'green',ax = ax)
  
# line plot for physics marks
df.plot(kind = 'line',x = 'name',
        y = 'physics_marks',
        color = 'blue',ax = ax)
  
# line plot for chemistry marks
df.plot(kind = 'line',x = 'name',
        y = 'chem_marks',
        color = 'black',ax = ax)
  
# set the title
plt.title('LinePlots')
  
# show the plot
plt.show()

输出: