# Python数据可视化之用Matplotlib绘制常用图形

## 一、散点图

plt.scatter(x,y) # 以默认的形状颜色绘制散点图

```# 绘制图形所需的数据
y_5 = [11,17,16,11,12,11,12,13,10,14,8,13,12,15,14,17,18,21,16,17,30,14,15,15,15,19,21,22,22,22,23]
y_10 = [26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,21,20,22,15,11,15,5,13,15,10,11,13,12,13,6]
```

```import matplotlib.pyplot as plt

# 设置字体
plt.rcParams['font.sans-serif']=['SimHei'] # 替换sans_serif字体
plt.rcParams['axes.unicode_minus']=False # 解决坐标轴负数的负号显示问题

plt.figure(figsize=(12,5), dpi=100)

y_5 = [11,17,16,11,12,11,12,13,10,14,8,13,12,15,14,17,18,21,16,17,30,14,15,15,15,19,21,22,22,22,23]
y_10 = [26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,21,20,22,15,11,15,5,13,15,10,11,13,12,13,6]
x = ran编程客栈ge(1, 32)

plt.scatter(x, y_5, label='五月份')
plt.scatter(x, y_10, label='十月份')

# 增加标题，坐标描述
plt.xlabel("时间")
plt.ylabel("日期")
plt.title("气温和随时间变化的某种规律")

# 添加图形注释
plt.legend(loc='best')

plt.show()
```

```import matplotlib.pyplot as plt

# 设置字体
plt.rcParams['font.sans-serif']=['SimHei'] # 替换sans_serif字体
plt.rcParams['axes.unicode_minus']=False # 解决坐标轴负数的负号显示问题

plt.figure(figsize=(12,5), dpi=100)

y_5 = [11,17,16,11,12,11,12,13,10,14,8,13,12,15,14,17,18,21,16,17,30,14,15,15,15,19,21,22,22,22,23]
y_10 = [26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,21,20,22,15,11,15,5,13,15,10,11,13,12,13,6]
x_5 = range(1, 32)
x_10 = range(33, 64)

plt.scatter(x_5, y_5, label='五月份')
plt.scatterwww.cppcns.com(x_10, y_10, label='十月份')

# 刻度显示
x_t = list(x_4)+list(x_10)
x_l = ["5月{}号".format(i) for i in x_4]
x_l += ["10月{}号".format(i-31) for i in x_10]
plt.xticks(x_t[::3], x_l[::3], rotation=45)

# 添加图形注释
plt.legend(loc='best')

plt.show()
```

## 二、柱状图

plt.bar(x, width, align=‘center', **kwargs)

```Parameters:
x : sequence of scalars.

width : scalar or array-like, optional, 柱状图的宽度

align: {‘center', ‘edge'}, optional, default: ‘center'
Alignment of the bars to the x coordinates:
‘center': Center the base on the x positions.
‘edge': Align the left edges of the bars with the x positions.

**kwargs:
color: 选择柱状图的颜色
```

plt.barh(y, width) # 绘制以y为y轴位置的水平条形图

```import matplotlib.pyplot as plt
# 设置字体
plt.rcParams['font.sans-serif']=['SimHei'] # 替换sans_serif字体
plt.rcParams['axes.unicode_minus']=False # 解决坐标轴负数的负号显示问题

plt.figure(figsize=(15,10),dpi=100)
# 准备电影的名字以及电影的票房数据
movie_name = ["流浪地球","复仇者联盟4:终局之战","哪吒之魔童降世","疯狂的外星人",
"飞驰人生","蜘蛛侠:英雄远征","扫毒2天地对决","烈火英雄","大黄蜂",
"惊奇队长","比悲伤更悲伤的故事","哥斯拉2:怪兽之王","阿丽塔:战斗天使",
"银河补习班","狮子王","反贪风暴4","熊出没","大侦探皮卡丘","新喜剧之王",
"使徒行者2:谍影行动","千与千寻"]
y = [56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,
11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23,5.22]
# 放进横坐标的数字列表
x = range(len(movie_name))

# 画出条形图
# 颜色color参数表示条形图的顺序颜色，可循环
plt.bar(x, y, width=0.5, color=['b','r','g','y','c','m','y','k','c','g','g'])

# 修改刻度名称，并旋转90度
plt.xticks(x, movie_name, rotation=90)

# 展示图形
plt.show()
```

```# 水平条形图
plt.figure(figsize=(15,10),dpi=100)
plt.barh(movie_name, y, color=['b','r','g','y','c','m','k'])
plt.show()```

```movie_name = ['雷神3：诸神黄昏','正义联盟','寻梦环游记']
first_day = [10587.6,10062.5,1275.7] # 首日票房
first_weekend=[36224.9,34479.6,11830] # 首周票房```

```import matplotlib.pyplot as plt
# 设置字体
plt.rcParams['font.sans-serif']=['SimHei'] # 替换sans_serif字体
plt.rcParams['axes.unicode_minus']=False # 解决坐标轴负数的负号显示问题

plt.figure(figsize=(15,10),dpi=100)
# 准备数据
movie_name = ['雷神3：诸神黄昏','正义联盟','寻梦环游记']

# 构造首日票房，首周票房数据
first_day = [10587.6,10062.5,1275.7]
first_weekend=[36224.9,34479.6,11830]
x = range(len(movie_name))

# 绘制第一个柱状图
plt.bar(x, first_day, width=0.25)
# 接着绘制第二个柱状图
plt.bar([i+0.25 for i in x], first_weekend, width=0.25)

# 修改刻度名称
# 显示x轴中文，固定在首日和首周的中间位置
plt.xticks([i+0.125 for i in x], movie_name)

plt.show()
```

x轴坐标刻度显示位置的调整，既可以通过绘制图形时调整x数据，也可以通过绘制x轴刻度时调整刻度的方法来实现x轴刻度显示在两个柱状图中间。如果不做任何调整，它是默认与第一个柱状图中间对齐的。

## 三、直方图

plt.hist(x,bins=None)

```import matplotlib.pyplot as plt
# 设置字体
plt.rcParams['font.sans-serif']=['SimHei'] # 替换sans_serif字体
plt.rcParams['axes.unicode_minus']=False # 解决坐标轴负数的负号显示问题

plt.figure(figsize=(15,10),dpi=100)
# 准备数据，电影时长
time =[131,  98, 125, 131, 124, 139, 131, 117, 128, 108,
135, 138, 131, 102, 107, 114, 119, 128, 121, 142,
127, 130, 124, 101, 110, 116, 117, 110, 128, 128,
115,  99, 136, 126, 134,  95, 138, 117, 111, 78,
132, 124, 113, 150, 110, 117,  86,  95, 144, 105,
126, 130,126, 130, 126, 116, 123, 106, 112, 138,
123,  86, 101,  99, 136,123, 117, 119, 105, 137,
123, 128, 125, 104, 109, 134, 125, 127,105, 120,
107, 129, 116, 108, 132, 103, 136, 118, 102, 120,
114,105, 115, 132, 145, 119, 121, 112, 139, 125,
138, 109, 132, 134,156, 106, 117, 127, 144, 139,
139, 119, 140,  83, 110, 102,123,107, 143, 115,
136, 118, 139, 123, 112, 118, 125, 109, 119, 133,
112, 114, 122, 109, 106, 123, 116, 131, 127, 115,
118, 112, 135,115, 146, 137, 116, 103, 144,  83,
编程客栈  123, 111, 110, 111, 100, 154,136, 100, 118, 119,
133, 134, 106, 129, 126, 110, 111, 109, 141, 120,
117, 106, 149, 122, 122, 110, 118, 127, 121, 114,
125, 126,114, 140, 103, 130, 141, 117, 106, 114,
121, 114, 133, 137,  92,121, 112, 146,  97, 137,
105,  98, 117, 112,  81,  97, 139, 113,134, 编程客栈106,
144, 110, 137, 137, 111, 104, 117, 100, 111, 101,
110, 105, 129, 137, 112, 120, 113, 133, 112,  83,
94, 146, 133, 101, 131, 116, 111, 84, 137, 115,
122, 106, 144, 109, 123, 116, 111,111, 133, 150]

# 组距
bins = 2
# 组数，极差/组距
groups = int((max(time) - min(time)) / 2)

# 绘制直方图
plt.hist(time, groups)

# 指定x轴刻度的范围
plt.xticks(list(range(min(time), max(time)))[::2])

# 添加坐标描述
plt.xlabel('电影时长大小')
plt.ylabel('电影的数量')

# 增加网格显示
plt.grid(True, linestyle='--', alpha=0.5)
plt.show()
```

## 四、扇形图

plt.pie(x, explode=None, labels=None)

x：扇形数据

explode：设置某几个分块是否要分离饼图

labels：每块扇形标签

autopct：百分比数据标签

plt.pie()有3个返回值：

patches：绘制饼图每一块的对象

texts：文本的列表

autotexts：百分比的文本列表

```import matplotlib.pyplot as plt
# 设置字体
plt.rcParams['font.sans-serif']=['SimHei'] # 替换sans_serif字体
plt.rcParams['axes.unicode_minus']=False # 解决坐标轴负数的负号显示问题

# 饼图绘制
plt.figure(figsize=(15, 10), dpi=100)
# 电影排片数据
movie_name = ['雷神3：诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴','降魔传','追捕','七十七天','密战','狂兽','其它']
place_count = [60605,54546,45819,28243,13270,9945,7679,6799,6101,4621,20105]

# 绘制扇形图
plt.pie(place_count, labels=movie_name, autopct="%1.2f%%")

# 指定显示的pie是正圆
plt.axis('equal')

# 设置图例和标题
plt.legend(loc="best")
plt.title("排片占比情况")

plt.show()
```

```import matplotlib.pyplot as plt
# 设置字体
pltwww.cppcns.com.rcParams['font.sans-serif']=['SimHei'] # 替换sans_serif字体
plt.rcParams['axes.unicode_minus']=False # 解决坐标轴负数的负号显示问题

plt.figure(figsize=(15, 10), dpi=100)
movie_name = ['雷神3：诸神黄昏','正义联盟','东方快车谋杀案','寻梦环游记','全球风暴','降魔传','追捕','七十七天','密战','狂兽','其它']
place_count = [60605,54546,45819,28243,13270,9945,7679,6799,6101,4621,20105]

# 绘制扇形图，添加阴影效果,并让占比最大的雷神3分离
explode = [0.1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
res = plt.pie(place_count, labels=movie_name, explode=explode, autopct="%1.2f%%", shadow=True)
patches, texts, autotexts = res
print(patches)  # 表示每个扇形的对象
print(texts)  # 每个文本对象
print(autotexts)  # 每个百分比对象

# 指定显示的pie是正圆
plt.axis('equal')

# 设置图例和标题
plt.legend(loc="best")
plt.title("排片占比情况")

plt.show()
```