文本
本文我们主要介绍利用Python中的Matplotlib模块进行几种散点图的画法,包括整张图片只有一种颜色的不分组散点图、整张图片有好几种颜色的不分组散点图、整张图片有好几种颜色的分组散点图等。
主要利用Python中的Matplotlib模块完成该功能。
表格如下(示例):
班别 | 语文成绩(分/100分制) |
---|---|
甲班 | 80 |
90 | |
75 | |
65 | |
85 | |
95 | |
100 | |
100 | |
80 | |
70 | |
90 | |
95 | |
85 | |
86 | |
92 | |
90 | |
95 | |
90 | |
85 | |
100 | |
乙班 | 60 |
70 | |
80 | |
65 | |
75 | |
80 | |
73 | |
75 | |
85 | |
90 | |
95 | |
65 | |
70 | |
75 | |
80 | |
85 | |
95 | |
85 | |
80 | |
70 | |
丙班 | 60 |
80 | |
100 | |
100 | |
100 | |
100 | |
90 | |
95 | |
95 | |
95 | |
85 | |
95 | |
95 | |
95 | |
95 | |
80 | |
95 | |
90 | |
90 | |
90 |
现在需要把表格中的数据绘制成散点图,从而进一步分析每个班级的学生成绩情况。
import matplotlib.pyplot as plt
# 设置字体, 解决中文乱码问题
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
# 解决图像中的'-'负号的乱码问题
plt.rcParams['axes.unicode_minus'] = False
# A班的语文成绩
ClassA_C = [80, 90, 75, 65, 85, 95, 100, 100, 80, 70, 90, 95, 85, 86, 92, 90, 95, 90, 85, 100]
# B班的语文成绩
ClassB_C = [60, 70, 80, 65, 75, 80, 73, 75, 85, 90, 95, 65, 70, 75, 80, 85, 95, 85, 80, 70]
# C班的语文成绩
ClassC_C = [60, 80, 100, 100, 100, 100, 90, 95, 95, 95, 85, 95, 95, 95, 95, 80, 95, 90, 90, 90]
fig = plt.figure(figsize=(8, 6), facecolor='#B0C4DE')
ax = fig.add_subplot(facecolor='white')
# 橙绿蓝
color_list = ['#FF8C00', '#00FF00', '#0000FF']
x_labels = ['甲班', '乙班', '丙班']
x_loc = [1, 2, 3]
scatter_plot_data = [ClassA_C, ClassB_C, ClassC_C]
# 每个数据(scatter_plot_data里面的每个数据)对应的x轴的坐标值
spotx = []
for i, j in zip(x_loc, range(len(scatter_plot_data))):
spotx_tmp = [i] * len(scatter_plot_data[j])
spotx.append(spotx_tmp)
ax.scatter(spotx, scatter_plot_data, c=color_list[0], s=30)
ax.grid(True, ls=':', color='b', alpha=0.3)
plt.title('甲乙丙各班语文成绩Scatter_chart分析', fontweight='bold')
ax.set_xticks(x_loc)
ax.set_xticklabels(x_labels, rotation=90)
ax.set_ylabel('分数/百分制', fontweight='bold')
# 设置x, y坐标轴的刻度标签字体加粗
plt.xticks(weight='bold')
plt.yticks(weight='bold')
fig.tight_layout()
plt.show()
输出结果如下:
注意:如果想保存这种画布带背景颜色且轴域也带背景颜色的图片(在此图中,画布[fig]背景颜色为#B0C4DE,轴域[ax]背景颜色为white),需要使用以下语句保存到本地,具体语句如下:
plt.savefig(picture_name + '.jpg', facecolor=self.fig.get_facecolor())
# 其中picture_name为给图片命的名字
表格如下(示例):
班别 | 语文成绩(分/100分制) |
---|---|
甲班 | 80 |
90 | |
75 | |
65 | |
85 | |
95 | |
100 | |
100 | |
80 | |
70 | |
90 | |
95 | |
85 | |
86 | |
92 | |
90 | |
95 | |
90 | |
85 | |
100 | |
乙班 | 60 |
70 | |
80 | |
65 | |
75 | |
80 | |
73 | |
75 | |
85 | |
90 | |
95 | |
65 | |
70 | |
75 | |
80 | |
85 | |
95 | |
85 | |
80 | |
30 | |
丙班 | 60 |
80 | |
100 | |
100 | |
100 | |
100 | |
90 | |
95 | |
95 | |
95 | |
85 | |
95 | |
95 | |
95 | |
95 | |
80 | |
95 | |
90 | |
90 | |
90 |
现在需要把表格中的数据绘制成散点图,从而进一步分析每个班级的学生成绩情况。
import matplotlib.pyplot as plt
# 设置字体, 解决中文乱码问题
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
# 解决图像中的'-'负号的乱码问题
plt.rcParams['axes.unicode_minus'] = False
# A班的语文成绩
ClassA_C = [80, 90, 75, 65, 85, 95, 100, 100, 80, 70, 90, 95, 85, 86, 92, 90, 95, 90, 85, 100]
# B班的语文成绩
ClassB_C = [60, 70, 80, 65, 75, 80, 73, 75, 85, 90, 95, 65, 70, 75, 80, 85, 95, 85, 80, 30]
# C班的语文成绩
ClassC_C = [60, 80, 100, 100, 100, 100, 90, 95, 95, 95, 85, 95, 95, 95, 95, 80, 95, 90, 90, 90]
fig = plt.figure(figsize=(8, 6), facecolor='#B0C4DE')
ax = fig.add_subplot(facecolor='white')
# 橙绿蓝
color_list = ['#FF8C00', '#00FF00', '#0000FF']
# 'o':圆圈; 'D':钻石; 's':正方形
marker_list = ['o', 'D', 's']
x_labels = ['甲班', '乙班', '丙班']
x_loc = [1, 2, 3]
scatter_plot_data = [ClassA_C, ClassB_C, ClassC_C]
# 每个数据(scatter_plot_data里面的每个数据)对应的x轴的坐标值
spotx = []
for i, j in zip(x_loc, range(len(scatter_plot_data))):
spotx_tmp = [i] * len(scatter_plot_data[j])
spotx.append(spotx_tmp)
# 每个数据(scatter_plot_data里面的每个数据)对应的marker的颜色
color_list_x = []
color_list_x_final = []
for i, j in zip(color_list, range(len(scatter_plot_data))):
color_list_x_tmp = [i] * len(scatter_plot_data[j])
color_list_x.append(color_list_x_tmp)
for i in color_list_x:
for j in i:
color_list_x_final.append(j)
ax.scatter(spotx, scatter_plot_data, c=color_list_x_final, s=30, marker='o')
ax.grid(True, ls=':', color='b', alpha=0.3)
plt.title('甲乙丙各班语文成绩Scatter_chart分析', fontweight='bold')
ax.set_xticks(x_loc)
ax.set_xticklabels(x_labels, rotation=90)
ax.set_ylabel('分数/百分制', fontweight='bold')
# 设置x, y坐标轴的刻度标签字体加粗
plt.xticks(weight='bold')
plt.yticks(weight='bold')
fig.tight_layout()
plt.show()
输出结果如下:
注意:如果想保存这种画布带背景颜色且轴域也带背景颜色的图片(在此图中,画布[fig]背景颜色为#B0C4DE,轴域[ax]背景颜色为white),需要使用以下语句保存到本地,具体语句如下:
plt.savefig(picture_name + '.jpg', facecolor=self.fig.get_facecolor())
# 其中picture_name为给图片命的名字
import matplotlib.pyplot as plt
# 设置字体, 解决中文乱码问题
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
# 解决图像中的'-'负号的乱码问题
plt.rcParams['axes.unicode_minus'] = False
# A班的语文成绩
ClassA_C = [80, 90, 75, 65, 85, 95, 100, 100, 80, 70, 90, 95, 85, 86, 92, 90, 95, 90, 85, 100]
# B班的语文成绩
ClassB_C = [60, 70, 80, 65, 75, 80, 73, 75, 85, 90, 95, 65, 70, 75, 80, 85, 95, 85, 80, 30]
# C班的语文成绩
ClassC_C = [60, 80, 100, 100, 100, 100, 90, 95, 95, 95, 85, 95, 95, 95, 95, 80, 95, 90, 90, 90]
fig = plt.figure(figsize=(8, 6), facecolor='#B0C4DE')
ax = fig.add_subplot(facecolor='white')
# 橙绿蓝
color_list = ['#FF8C00', '#00FF00', '#0000FF']
# 'o':圆圈; 'D':钻石; 's':正方形
marker_list = ['o', 'D', 's']
x_labels = ['甲班', '乙班', '丙班']
x_loc = [1, 2, 3]
scatter_plot_data = [ClassA_C, ClassB_C, ClassC_C]
# 每个数据(scatter_plot_data里面的每个数据)对应的x轴的坐标值
spotx = []
for i, j in zip(x_loc, range(len(scatter_plot_data))):
spotx_tmp = [i] * len(scatter_plot_data[j])
spotx.append(spotx_tmp)
#############################################################
#############################################################
# 每个数据(scatter_plot_data里面的每个数据)对应的marker的形状
marker_list_x = []
marker_list_x_final = []
for i, j in zip(marker_list, range(len(scatter_plot_data))):
marker_list_x_tmp = [i] * len(scatter_plot_data[j])
marker_list_x.append(marker_list_x_tmp)
for i in marker_list_x:
for j in i:
marker_list_x_final.append(j)
#############################################################
#############################################################
# 每个数据(scatter_plot_data里面的每个数据)对应的marker的颜色
color_list_x = []
color_list_x_final = []
for i, j in zip(color_list, range(len(scatter_plot_data))):
color_list_x_tmp = [i] * len(scatter_plot_data[j])
color_list_x.append(color_list_x_tmp)
for i in color_list_x:
for j in i:
color_list_x_final.append(j)
ax.scatter(spotx, scatter_plot_data, c=color_list_x_final, s=30, marker=marker_list_x_final)
ax.grid(True, ls=':', color='b', alpha=0.3)
plt.title('甲乙丙各班语文成绩Scatter_chart分析', fontweight='bold')
ax.set_xticks(x_loc)
ax.set_xticklabels(x_labels, rotation=90)
ax.set_ylabel('分数/百分制', fontweight='bold')
# 设置x, y坐标轴的刻度标签字体加粗
plt.xticks(weight='bold')
plt.yticks(weight='bold')
fig.tight_layout()
plt.show()
输出结果如下:
注意:这里我们可以看到此时程序运行的时候报错,而2.1.2 Case2 与2.1.1 Case1 相比仅仅只是多了如下几行代码:
#############################################################
#############################################################
# 每个数据(scatter_plot_data里面的每个数据)对应的marker的形状
marker_list_x = []
marker_list_x_final = []
for i, j in zip(marker_list, range(len(scatter_plot_data))):
marker_list_x_tmp = [i] * len(scatter_plot_data[j])
marker_list_x.append(marker_list_x_tmp)
for i in marker_list_x:
for j in i:
marker_list_x_final.append(j)
#############################################################
#############################################################
这几行代码的目的本来是想给每组数据(scatter_plot_data里面包含三组数据:[ClassA_C, ClassB_C, ClassC_C])赋予不同的marker形状类型,但是这样行不通,原因是ax.scatter()在一次绘图中只能接受一种marker类型,若想赋予多种marker类型,需要多次调用ax.scatter(),但这显然不符合我们的本次绘图的主要意图。这种情况的绘图详见本文中第四部分所讲内容。
注意:如果想保存这种画布带背景颜色且轴域也带背景颜色的图片(在此图中,画布[fig]背景颜色为#B0C4DE,轴域[ax]背景颜色为white),需要使用以下语句保存到本地,具体语句如下:
plt.savefig(picture_name + '.jpg', facecolor=self.fig.get_facecolor())
# 其中picture_name为给图片命的名字
表格如下(示例):
班别 | 语文成绩(分/100分制) | 数学成绩(分/100分制) | 英语成绩(分/100分制) |
---|---|---|---|
甲班 | 80 | 70 | 90 |
90 | 90 | 100 | |
75 | 95 | 100 | |
65 | 85 | 85 | |
85 | 75 | 75 | |
95 | 85 | 90 | |
100 | 90 | 100 | |
100 | 100 | 100 | |
80 | 100 | 75 | |
70 | 85 | 70 | |
90 | 90 | 85 | |
95 | 95 | 90 | |
85 | 98 | 95 | |
86 | 99 | 95 | |
92 | 85 | 90 | |
90 | 88 | 80 | |
95 | 86 | 70 | |
90 | 75 | 80 | |
85 | 78 | 70 | |
100 | 90 | 75 | |
乙班 | 60 | 60 | 70 |
70 | 70 | 75 | |
80 | 75 | 75 | |
65 | 80 | 70 | |
75 | 75 | 60 | |
80 | 75 | 90 | |
73 | 65 | 98 | |
75 | 80 | 95 | |
85 | 60 | 85 | |
90 | 80 | 75 | |
95 | 90 | 70 | |
65 | 95 | 60 | |
70 | 95 | 65 | |
75 | 90 | 70 | |
80 | 80 | 75 | |
85 | 85 | 75 | |
95 | 75 | 80 | |
85 | 75 | 75 | |
80 | 60 | 70 | |
70 | 65 | 80 | |
丙班 | 60 | 100 | 80 |
80 | 100 | 90 | |
100 | 100 | 100 | |
100 | 95 | 100 | |
100 | 95 | 100 | |
100 | 95 | 90 | |
90 | 95 | 95 | |
95 | 95 | 95 | |
95 | 90 | 95 | |
95 | 85 | 90 | |
85 | 90 | 95 | |
95 | 90 | 90 | |
95 | 90 | 95 | |
95 | 95 | 90 | |
95 | 90 | 95 | |
80 | 95 | 90 | |
95 | 95 | 95 | |
90 | 95 | 90 | |
90 | 95 | 95 | |
90 | 90 | 85 |
现在需要把表格中的数据绘制成散点图,从而进一步分析每个班级的学生成绩情况。
import matplotlib.pyplot as plt
import numpy as np
# 设置字体, 解决中文乱码问题
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
# 解决图像中的'-'负号的乱码问题
plt.rcParams['axes.unicode_minus'] = False
ClassA_C = [80, 90, 75, 65, 85, 95, 100, 100, 80, 70, 90, 95, 85, 86, 92, 90, 95, 90, 85, 100]
ClassA_M = [70, 90, 95, 85, 75, 85, 90, 100, 100, 85, 90, 95, 98, 99, 85, 88, 86, 75, 78, 90]
ClassA_E = [90, 100, 100, 85, 75, 90, 100, 100, 75, 70, 85, 90, 95, 95, 90, 80, 70, 80, 70, 75]
ClassB_C = [60, 70, 80, 65, 75, 80, 73, 75, 85, 90, 95, 65, 70, 75, 80, 85, 95, 85, 80, 70]
ClassB_M = [60, 70, 75, 80, 75, 75, 65, 80, 60, 80, 90, 95, 95, 90, 80, 85, 75, 75, 60, 65]
ClassB_E = [70, 75, 75, 70, 60, 90, 98, 95, 85, 75, 70, 60, 65, 70, 75, 75, 80, 75, 70, 80]
ClassC_C = [60, 80, 100, 100, 100, 100, 90, 95, 95, 95, 85, 95, 95, 95, 95, 80, 95, 90, 90, 90]
ClassC_M = [100, 100, 100, 95, 95, 95, 95, 95, 90, 85, 90, 90, 90, 95, 90, 95, 95, 95, 95, 90]
ClassC_E = [80, 90, 100, 100, 100, 90, 95, 95, 95, 90, 95, 90, 95, 90, 95, 90, 95, 90, 95, 85]
fig = plt.figure(figsize=(8, 6), facecolor='#B0C4DE')
ax = fig.add_subplot(facecolor='white')
group_dataA = [ClassA_C, ClassA_M, ClassA_E]
scatter_dataABC_C = [ClassA_C, ClassB_C, ClassC_C]
scatter_dataABC_M = [ClassA_M, ClassB_M, ClassC_M]
scatter_dataABC_E = [ClassA_E, ClassB_E, ClassC_E]
# 橙绿蓝
color_list = ['#FF8C00', '#00FF00', '#0000FF']
# 'o':圆圈; 'D':钻石; 's':正方形
marker_list = ['o', 'D', 's']
x_labels = ['甲班', '乙班', '丙班']
legend_labels = ['语文', '数学', '英语']
length = len(x_labels)
x_loc = np.arange(length)
# 统计每个刻度标签下有几个group
group_number = len(group_dataA)
total_width = 0.5
scatter_total_width = total_width * 0.65
interval_total_width = total_width * 0.35
scatter_width = scatter_total_width / group_number
###################################################
if group_number == 1:
interval_width = interval_total_width
else:
interval_width = interval_total_width / (group_number - 1)
###################################################
if group_number % 2 == 0:
x1_scatter = x_loc - (group_number / 2 - 1) * scatter_width - scatter_width / 2 - (group_number / 2 - 1) * interval_width - interval_width / 2
else:
x1_scatter = x_loc - ((group_number - 1) / 2) * scatter_width - ((group_number - 1) / 2) * interval_width
x_list_scatter = [x1_scatter + scatter_width * i + interval_width * i for i in range(group_number)]
###################################################
scatter_data = [scatter_dataABC_C, scatter_dataABC_M, scatter_dataABC_E]
for i in range(len(scatter_data)):
##################################################################
# scatter_data_num用来统计每组数据的长度, 画scatter图时会用到
scatter_data_num = []
for j in scatter_data[i]:
scatter_data_num_tmp = len(j)
scatter_data_num.append(scatter_data_num_tmp)
##################################################################
# 画scatter
# spotx是每一组scatter所有的点的横坐标
spotx = []
for j_spotx, k_spotx in zip(x_list_scatter[i], scatter_data_num):
spotx_tmp = [j_spotx] * k_spotx
spotx.append(spotx_tmp)
# print('$$$spotx:', spotx)
ax.scatter(spotx, scatter_data[i], c=color_list[i], s=30, label=legend_labels[i], marker=marker_list[i])
##################################################################
ax.grid(True, ls=':', color='b', alpha=0.3)
plt.title('甲乙丙各班语文/数学/英语成绩Scatter_chart分析', fontweight='bold')
ax.set_xticks(x_loc)
ax.set_xticklabels(x_labels, rotation=90)
ax.set_ylabel('分数/百分制', fontweight='bold')
################################################################################################################
################################################################################################################
plt.legend(title='学科', loc='center left', bbox_to_anchor=(1.02, 0.5), facecolor='None', edgecolor='#000000',
frameon=True, ncol=1, markerscale=3, borderaxespad=0, handletextpad=0.1, fontsize='x-large', title_fontsize='x-large')
################################################################################################################
################################################################################################################
plt.xticks(weight='bold')
plt.yticks(weight='bold')
fig.tight_layout()
plt.show()
输出结果如下:
注意:如果想保存这种画布带背景颜色且轴域也带背景颜色的图片(在此图中,画布[fig]背景颜色为#B0C4DE,轴域[ax]背景颜色为white),需要使用以下语句保存到本地,具体语句如下:
plt.savefig(picture_name + '.jpg', facecolor=self.fig.get_facecolor())
# 其中picture_name为给图片命的名字
以上就是今天要讲的内容,本文详细介绍了各种散点图的绘制过程和方法,并用实例给大家演示了具体的实现代码和实现逻辑,希望对大家学习画散点图有帮助。