matplotlib快速人门: 绘制各种相对复杂的图及系统中文字体设置

Matplotlib组合直方图

当我们一张图需要多个直方我们改怎么做呢?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams["font.family"] = 'Arial Unicode MS'  # mac电脑设置中文字体


plt.figure(figsize=(15, 8))  # 设置画布
x_index = np.arange(6)  # 确定label的位置
x_data = ("北京", "上海", "深圳", "广州", "杭州", "西安")
y1_data = (11, 22, 33, 44, 55, 66)
y2_data = (22, 33, 44, 55, 66, 77)
y3_data = (77, 66, 33, 22, 11, 44)
bar_with = 0.2  # 定义一个数字代表独立柱的宽度
rects1 = plt.bar(x_index, y1_data, width=bar_with, alpha=0.4, color="b", label="前端")
rects2 = plt.bar(x_index+bar_with, y2_data, width=bar_with, alpha=0.4, color="r", label="后端")
rects3 = plt.bar(x_index+(bar_with*2), y3_data, width=bar_with, alpha=0.4, color="g", label="AI")
plt.xticks(x_index + bar_width/2, x_data)  # 设定x轴
plt.legend()  # 显示图例
plt.title('这是我做测试画的一张图')
plt.tight_layout()
# plt.grid(axis="y")
plt.grid(ls='-.')  # 绘制背景线
plt.show()
看看效果吧

在这里插入图片描述

  • 注意事项: 其实这种图不是很难, 我们要实先设置直方的宽度, 然后再使用numpy.arange() 创建一个数组, 这个数组是每个直方的位置 1 ~ n, 当我们每多一个直方时, 我们只需要将数组加上直方的宽度*n 就可以了
rects3 = plt.bar(x_index+(bar_with*2), y3_data, width=bar_with, alpha=0.4, color="g", label="AI")
上面就是一个绘制三条直方的例子, x_index是 1 ~ n 的数组, bar_with 是直方的宽度 x_index + (bar_with * 2) 就是第三条直方图的位置, 然后我们将数据传入即可, 绘制出我们想要的效果图

关于中文显示

# Windows系统设置中文字体
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# mac电脑设置中文字体
plt.rcParams["font.family"] = 'Arial Unicode MS'

绘制饼图

import panadas as pd
import matplotlib.pyplot as plt
plt.rcParams["font.family"] = 'Arial Unicode MS'  # mac电脑设置中文字体


labels = ['北京', '深圳', '上海', '杭州', '南京', '广州', '成都', '济南', '武汉', '西安']
sizes = [125, 108,  89,  78,  78,  46,  26,  26,  21,  19]
colors = ['lightgreen', 'gold', 'lightskyblue', 'lightcoral']
plt.figure(figsize=(15,8))
plt.pie(sizes, labels=labels,
        colors=colors, autopct='%1.1f%%', shadow=True, startangle=50)  # shadow=True 表示阴影
plt.axis('equal')  # 使图居中
# plt.title("物联网职位各城市需求分布")
plt.show()

在这里插入图片描述

  • 具体效果如上图所示, 许多饼图都可以按照这个饼图的模板来绘制

当我一张图需要多条折线

import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams["font.family"] = 'Arial Unicode MS'  # mac电脑设置中文字体


X = ["{}千米".format(x) for x in range(10)]
y = [11, 5, 6, 8, 12, 9, 7, 11, 20, 17]
z = [17, 20, 11, 7, 9, 12, 8, 6, 5, 11]
plt.figure(figsize=(15, 8))
plt.plot(X, y)
plt.plot(X, z, linestyle="--")
plt.title("这是一张测试使用的图")
plt.xlabel("路程/km")
plt.ylabel("消耗的能量/千焦")
plt.grid()
plt.show()

在这里插入图片描述

一张画布需要绘制两张图该怎么办? 参考下面的代码吧

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams["font.family"] = 'Arial Unicode MS'


X = ["{}千米".format(x) for x in range(1, 11)]
y = [11, 5, 6, 8, 12, 9, 7, 11, 20, 17]
plt.figure(figsize=(15, 8))
plt.subplot(121)  # 将画板'1'分为两块现在使用第一个块画图
plt.bar(X, y, color="orange")
plt.title("这是测试的第一张图")
plt.grid()
plt.subplot(122)  # 将画板 '1' 分为两个块现在使用第二块画图
plt.pie(y, labels=X, colors=colors, autopct='%1.1f%%', shadow=True, startangle=50)
plt.title("这是我测试的第二张图")
# plt.tight_layout()
plt.show()

在这里插入图片描述

看到了吧! 我们可以使用 subplot() 来声明我们对画布的操作, subplot 里面的参数是三个数字, 121 中的 ‘1’ 表示对 ‘1’ 号画(默认的画布为 1 可以使用plt.figure(1) 来指定画布名) 121 中的 ‘2’ 表示将画布分为两块 121 中的第二个 ‘1’ 是用来声明现在给第一块区域画图, 122 则声明给第二块画布画图
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章