一. 绘制基本图表
颜色color和简写:
- 红色 r: red
- 绿色 g: green
- 蓝色 b: blue
- 青色 c: cyan
- 洋红色 m: magenta
- 黄色 y: yellow
- 黑色 k: black
- 白色 w: white
1.1 绘制柱状图
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 6, 7, 8]
y = [60, 45, 49, 36, 42, 67, 40, 50]
plt.bar(x, y)
plt.show()
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 6, 7, 8]
y = [60, 45, 49, 36, 42, 67, 40, 50]
plt.bar(x, y, width = 0.5, color = 'r')
plt.show()
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
x = ['上海', '成都', '重庆', '深圳', '北京', '长沙', '南京', '青岛']
y = [60, 45, 49, 36, 42, 67, 40, 50]
plt.bar(x, y, width = 0.5, color = 'r')
plt.show()
1.2 绘制条形图
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
x = ['上海', '成都', '重庆', '深圳', '北京', '长沙', '南京', '青岛']
y = [60, 45, 49, 36, 42, 67, 40, 50]
plt.barh(x, y, height = 0.5, color = 'r')
plt.show()
1.3 绘制折线图
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
x = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
y = [50, 45, 65, 76, 75, 85, 55, 78, 86, 89, 94, 90]
plt.plot(x, y, color = 'r', linestyle = 'dashdot', linewidth = 2)
plt.show()
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
x = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
y = [50, 45, 65, 76, 75, 85, 55, 78, 86, 89, 94, 90]
plt.plot(x, y, color = 'r', linestyle = 'dashdot', linewidth = 2, marker = '*', markersize = 10)
plt.show()
1.4 绘制面积图
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
x = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
y = [50, 45, 65, 76, 75, 85, 55, 78, 86, 89, 94, 90]
plt.stackplot(x, y, color = 'r')
# plt.show()
plt.savefig('1_4绘制面积图.png')
1.5 绘制散点图
import pandas as pd
import matplotlib.pyplot as plt
data = pd.read_excel('汽车速度和刹车距离表.xlsx')
x = data['汽车速度(km/h)']
y = data['刹车距离(m)']
plt.scatter(x, y, s = 100, color = 'r', marker = 'o', edgecolor = 'k')
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import linear_model
data = pd.read_excel('汽车速度和刹车距离表.xlsx')
x = data['汽车速度(km/h)']
y = data['刹车距离(m)']
plt.scatter(x, y, s = 100, color = 'r', marker = 'o', edgecolor = 'k')
model = linear_model.LinearRegression().fit(x.values.reshape(-1,1), y)
pred = model.predict(x.values.reshape(-1,1))
plt.plot(x, pred, color = 'k', linewidth = 3, linestyle = 'solid')
plt.show()
1.6 绘制饼图和圆环图
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
x = ['上海', '成都', '重庆', '深圳', '北京', '青岛', '南京']
y = [10, 45, 25, 36, 45, 56, 78]
plt.pie(y, labels = x, labeldistance = 1.1, autopct = '%.2f%%', pctdistance = 1.5)
plt.show()
plt.pie(y, labels = x, labeldistance = 1.1, autopct = '%.2f%%', pctdistance = 1.5, explode = [0,0,0,0,0,0.3,0], startangle = 90, counterclock = False)
plt.show()
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
x = ['上海', '成都', '重庆', '深圳', '北京', '青岛', '南京']
y = [10, 45, 25, 36, 45, 56, 78]
plt.pie(y, labels = x, labeldistance = 1.1, autopct = '%.2f%%', pctdistance = 1.5, wedgeprops = {
'width' : 0.3, 'linewidth' : 2, 'edgecolor' : 'white'})
plt.show()
二. 图表的绘制和美化技巧
2.1 在一张画布中绘制多张图表
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
x = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
y = [50, 45, 65, 76, 75, 85, 55, 78, 86, 89, 94, 90]
plt.subplot(2, 2, 1)
plt.pie(y, labels = x, labeldistance = 1.1, startangle = 90, counterclock = False)
plt.subplot(2, 2, 2)
plt.bar(x, y, width = 0.5, color = 'r')
plt.subplot(2, 2, 3)
plt.stackplot(x, y, color = 'r')
plt.subplot(2, 2, 4)
plt.plot(x, y, color = 'r', linestyle = 'solid', linewidth = 2, marker = 'o', markersize = 10)
plt.show()
2.2 添加图表元素
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
x = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
y = [50, 45, 65, 76, 75, 85, 55, 78, 86, 89, 94, 90]
plt.bar(x, y, width=0.6, color='r', label='销售额(万元)')
plt.title(label='销售额对比图', fontdict={
'family': 'KaiTi', 'color': 'k', 'size': 30}, loc='center')
plt.xlabel('月份', fontdict={
'family': 'SimSun', 'color': 'k', 'size': 20}, labelpad=20)
plt.ylabel('销售额', fontdict={
'family': 'SimSun', 'color': 'k', 'size': 20}, labelpad=20)
plt.legend(loc='upper left', fontsize=15)
for a,b in zip(x, y):
plt.text(x=a, y=b, s=b, ha='center', va='bottom', fontdict={
'family': 'KaiTi', 'color': 'k', 'size': 20})
plt.show()
2.3 添加并设置网格线
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
x = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
y = [50, 45, 65, 76, 75, 85, 55, 78, 86, 89, 94, 90]
plt.plot(x, y, color = 'r', linestyle = 'solid', linewidth = 2)
plt.title(label = '销售额趋势图',fontdict = {
'family' : 'KaiTi', 'color' : 'k', 'size' : 30}, loc = 'center')
plt.xlabel('月份', fontdict = {
'family' : 'SimSun', 'color' : 'k', 'size' : 20}, labelpad = 20)
plt.ylabel('销售额(万元)', fontdict = {
'family' : 'SimSun', 'color' : 'k', 'size' : 20}, labelpad = 20)
plt.grid(b = True, color = 'r', linestyle = 'dotted', linewidth = 1)
plt.show()
plt.grid(b = True, axis = 'y', color = 'r', linestyle = 'dotted', linewidth = 1)
plt.show()
2.4 调整坐标轴的刻度范围
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
x = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
y = [50, 45, 65, 76, 75, 85, 55, 78, 86, 89, 94, 90]
plt.plot(x, y, color = 'r', linestyle = 'solid', linewidth = 2, label = '销售额(万元)')
plt.title(label = '销售额趋势图',fontdict = {
'family' : 'KaiTi', 'color' : 'k', 'size' : 30}, loc = 'center')
plt.legend(loc = 'upper left', fontsize = 15)
for a,b in zip(x, y):
plt.text(a, b, b, ha = 'center', va = 'bottom', fontdict = {
'family' : 'KaiTi', 'color' : 'k', 'size': 20})
plt.ylim(40, 100)
plt.show()
plt.axis('off')
三. 绘制高级图表
3.1 绘制气泡图
import matplotlib.pyplot as plt
import pandas as pd
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
data = pd.read_excel('产品销售统计.xlsx')
n = data['产品名称']
x = data['销售量(件)']
y = data['销售额(元)']
z = data['毛利率(%)']
plt.scatter(x, y, s=z * 300, color='r', marker='o')
plt.xlabel('销售量(件)', fontdict={
'family': 'Microsoft YaHei', 'color': 'k', 'size': 20}, labelpad=20)
plt.ylabel('销售额(元)', fontdict={
'family': 'Microsoft YaHei', 'color': 'k', 'size': 20}, labelpad=20)
plt.title('销售量、销售额与毛利率关系图', fontdict={
'family': 'Microsoft YaHei', 'color': 'k', 'size': 30}, loc='center')
for a, b, c in zip(x, y, n):
plt.text(x=a, y=b, s=c, ha='center', va='center', fontsize=15, color='w')
plt.xlim(50, 600)
plt.ylim(2900, 11000)
plt.show()
3.2 绘制组合图
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
data = pd.read_excel('销售业绩表.xlsx')
x = data['月份']
y1 = data['销售额(万元)']
y2 = data['同比增长率']
plt.bar(x, y1, color = 'c', label = '销售额(万元)')
plt.plot(x, y2, color = 'r', linewidth = 3, label = '同比增长率')
plt.legend(loc = 'upper left', fontsize = 15)
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
data = pd.read_excel('销售业绩表.xlsx')
x = data['月份']
y1 = data['销售额(万元)']
y2 = data['同比增长率']
plt.bar(x, y1, color = 'c', label = '销售额(万元)')
plt.legend(loc = 'upper left', fontsize = 15)
plt.twinx()
plt.plot(x, y2, color = 'r', linewidth = 3, label = '同比增长率')
plt.legend(loc = 'upper right', fontsize = 15)
plt.show()
3.3 绘制直方图
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
data = pd.read_excel('客户年龄统计表.xlsx')
x = data['年龄']
plt.hist(x, bins = 9)
plt.xlim(15, 60)
plt.ylim(0, 40)
plt.title('年龄分布直方图', fontsize = 20)
plt.xlabel('年龄')
plt.ylabel('人数')
plt.grid(b = True, linestyle = 'dotted', linewidth = 1)
plt.show()
3.4 绘制雷达图
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
data = pd.read_excel('汽车性能指标分值统计表.xlsx')
data = data.set_index('性能评价指标')
data = data.T
data.index.name = '品牌'
def plot_radar(data, feature):
columns = ['动力性', '燃油经济性', '制动性', '操控稳定性', '行驶平顺性', '通过性', '安全性', '环保性', '方便性', '舒适性', '经济性', '容量性']
colors = ['r', 'g', 'y']
angles = np.linspace(0.1 * np.pi, 2.1 * np.pi, len(columns), endpoint = False)
angles = np.concatenate((angles, [angles[0]]))
figure = plt.figure(figsize = (6, 6))
ax = figure.add_subplot(1, 1, 1, projection = 'polar')
for i, c in enumerate(feature):
stats = data.loc[c]
stats = np.concatenate((stats, [stats[0]]))
ax.plot(angles, stats, '-', linewidth = 2, c = colors[i], label = str(c))
ax.fill(angles, stats, color = colors[i], alpha = 0.75)
ax.legend(loc = 4, bbox_to_anchor = (1.15, -0.07))
ax.set_yticklabels([2, 4, 6, 8, 10])
ax.set_thetagrids(angles * 180 / np.pi, columns, fontsize = 12)
plt.show()
return figure
figure = plot_radar(data, ['A品牌', 'B品牌', 'C品牌'])
figure = plot_radar(data, ['B品牌'])
3.5 绘制树状图
import squarify as sf
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
x = ['上海', '北京', '重庆', '成都', '南京', '青岛', '长沙', '武汉', '深圳']
y = [260, 45, 69, 800, 290, 360, 450, 120, 50]
colors = ['lightgreen', 'pink', 'yellow', 'skyblue', 'cyan', 'silver', 'lightcoral', 'orange', 'violet']
percent = ['11%', '2%', '3%', '33%', '12%', '15%', '18%', '5%', '2%']
chart = sf.plot(sizes = y, label = x, color = colors, value = percent, edgecolor = 'white', linewidth = 2)
plt.title(label = '城市销售额分布及占比图',fontdict = {
'family' : 'KaiTi', 'color' : 'k', 'size' : 25})
plt.axis('off')
# plt.show()
plt.savefig('3_5树状图.png')
3.6 绘制箱型图
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
data = pd.read_excel('1月销售统计表.xlsx')
x1 = data['成都']
x2 = data['上海']
x3 = data['北京']
x4 = data['重庆']
x5 = data['南京']
x = [x1, x2, x3, x4, x5]
labels = ['成都', '上海', '北京', '重庆', '南京']
plt.boxplot(x, vert = True, widths = 0.5, labels = labels, showmeans = True )
plt.title('各地区1月销售额箱形图', fontsize=20)
plt.ylabel('销售额(万元)')
plt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
# plt.show()
plt.savefig("3_6绘制箱型图.png")
3.7 绘制玫瑰图
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
index = ['0~0.5', '0.6~2.0', '2.1~4.0', '4.1~6.0']
columns = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW']
np.random.seed(0)
data = pd.DataFrame(np.random.randint(30, 300, (4, 16)), index = index, columns = columns)
N = 16
theta = np.linspace(0, 2 * np.pi, N, endpoint = False)
width = np.pi / N
labels = list(data.columns)
plt.figure(figsize = (6, 6))
ax = plt.subplot(1, 1, 1, projection = 'polar')
for i in data.index:
radius = data.loc[i]
ax.bar(theta, radius, width = width, bottom = 0.0, label = i, tick_label = labels)
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
plt.title('各方向风速频数玫瑰图', fontsize = 20)
plt.legend(loc = 4, bbox_to_anchor = (1.3, 0.2))
plt.show()
代码在git上:https://gitee.com/cuiyonghua/Deep-Learning-Cases/tree/master/机器学习模块使用/matplotlib模块