https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.html

pandas的dataframe自带一些绘图语法用以简单的表格数据可视化,适合于数据的初步探索、分析

1
import pandas as pd

1、barplot柱状图

1
2
3
4
5
6
7
8
df = pd.DataFrame({'lab':['A', 'B', 'C'], 
                   'val':[10, 30, 20]})
# 	lab	val
# 0	A	10
# 1	B	30
# 2	C	20

ax = df.plot.bar(x='lab', y='val', rot=0, color="green")
image-20220730145626960
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
speed = [0.1, 17.5, 40, 48]
lifespan = [2, 8, 70, 1.5]
index = ['snail', 'pig', 'elephant', 'rabbit']
# 类别名为index
df = pd.DataFrame({'speed': speed,
                   'lifespan': lifespan}, index=index)
#		  speed	lifespan
#snail	  0.1	2.0
#pig	  17.5	8.0
#elephant 40.0	70.0
#rabbit	  48.0	1.5
ax = df.plot.bar(rot=0)              #所有列(如下图)
ax = df.plot.bar(rot=0,stacked=True) 
ax = df["speed"].plot.bar(rot=45)    #指定列
ax = df.plot.bar(
    rot=0, subplots=True, layout=(1,2),
    color={"speed": "red", "lifespan": "green"}
)
image-20220730153508146

pandas.DataFrame.plot.barh()支持绘制横向的柱状图(Dataframe数据内容需要是横向的)

2、lineplot线图

(类似上面的barplot)

1
2
3
4
5
6
7
df = pd.DataFrame({'lab':['A', 'B', 'C'], 
                   'val':[10, 30, 20]})
# 	lab	val
# 0	A	10
# 1	B	30
# 2	C	20
ax = df.plot.line(x='lab', y='val', rot=0, color="green")
image-20220730153330099
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
speed = [0.1, 17.5, 40, 48]
lifespan = [2, 8, 70, 1.5]
index = ['snail', 'pig', 'elephant', 'rabbit']
# 类别名为index
df = pd.DataFrame({'speed': speed,
                   'lifespan': lifespan}, index=index)
#		  speed	lifespan
#snail	  0.1	2.0
#pig	  17.5	8.0
#elephant 40.0	70.0
#rabbit	  48.0	1.5
ax = df.plot.line(rot=0)              #所有列
image-20220730153423252

3、boxplot箱图

1
2
3
4
5
6
7
8
9
# 宽表格(列名表示分组)
data = np.random.randn(25, 4)
df = pd.DataFrame(data, columns=list('ABCD'))
ax = df.plot.box()

# 长表格(一列表示分组)
age_list = [8, 10, 12, 14, 72, 74, 76, 78, 20, 25, 30, 35, 60, 85]
df = pd.DataFrame({"gender": list("MMMMMMMMFFFFFF"), "age": age_list})
ax = df.plot.box(column="age", by="gender")
image-20220730152124896

4、scatter点图

1
2
3
4
5
df = pd.DataFrame([[5.1, 3.5, 0], [4.9, 3.0, 0], [7.0, 3.2, 1],
                   [6.4, 3.2, 1], [5.9, 3.0, 2]],
                  columns=['length', 'width', 'species'])
ax = df.plot.scatter(x='length', y='width')
ax = df.plot.scatter(x='length', y='width', c="species", colormap='viridis')
image-20220730152611584

5、density密度图

1
2
3
4
5
df = pd.DataFrame([[5.1, 3.5, 0], [4.9, 3.0, 0], [7.0, 3.2, 1],
                   [6.4, 3.2, 1], [5.9, 3.0, 2]],
                  columns=['length', 'width', 'species'])
ax = df["length"].plot.kde()
ax = df.plot.kde()
image-20220730154046387